Created
June 12, 2016 11:30
-
-
Save codenameone/cd227aaca486889f7c940e2e97985426 to your computer and use it in GitHub Desktop.
Demonstrates the usage of the search functionality in the Codename One Toolbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Image duke = null; | |
try { | |
duke = Image.createImage("/duke.png"); | |
} catch(IOException err) { | |
Log.e(err); | |
} | |
int fiveMM = Display.getInstance().convertToPixels(5); | |
final Image finalDuke = duke.scaledWidth(fiveMM); | |
Toolbar.setGlobalToolbar(true); | |
Form hi = new Form("Search", BoxLayout.y()); | |
hi.add(new InfiniteProgress()); | |
Display.getInstance().scheduleBackgroundTask(()-> { | |
// this will take a while... | |
Contact[] cnts = Display.getInstance().getAllContacts(true, true, true, true, false, false); | |
Display.getInstance().callSerially(() -> { | |
hi.removeAll(); | |
for(Contact c : cnts) { | |
MultiButton m = new MultiButton(); | |
m.setTextLine1(c.getDisplayName()); | |
m.setTextLine2(c.getPrimaryPhoneNumber()); | |
Image pic = c.getPhoto(); | |
if(pic != null) { | |
m.setIcon(fill(pic, finalDuke.getWidth(), finalDuke.getHeight())); | |
} else { | |
m.setIcon(finalDuke); | |
} | |
hi.add(m); | |
} | |
hi.revalidate(); | |
}); | |
}); | |
hi.getToolbar().addSearchCommand(e -> { | |
String text = (String)e.getSource(); | |
if(text == null || text.length() == 0) { | |
// clear search | |
for(Component cmp : hi.getContentPane()) { | |
cmp.setHidden(false); | |
cmp.setVisible(true); | |
} | |
hi.getContentPane().animateLayout(150); | |
} else { | |
text = text.toLowerCase(); | |
for(Component cmp : hi.getContentPane()) { | |
MultiButton mb = (MultiButton)cmp; | |
String line1 = mb.getTextLine1(); | |
String line2 = mb.getTextLine2(); | |
boolean show = line1 != null && line1.toLowerCase().indexOf(text) > -1 || | |
line2 != null && line2.toLowerCase().indexOf(text) > -1; | |
mb.setHidden(!show); | |
mb.setVisible(show); | |
} | |
hi.getContentPane().animateLayout(150); | |
} | |
}, 4); | |
hi.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This snippet shows a list of the contacts on your device and allows you to search thru them dynamically using the search functionality in the Toolbar class.
From the Codename One project