user interface - Blackberry UI different devices -
i'm working on blackberry application. have header can see in following picture, low resolution takes space more width, testing on bb bold 9900 simulator. tried using following code prevent ui destruction.
questions:
- is correct code prevent ui destruction?
- if yes using code how can align button right , logo left.
which design/ui should follow prevent ui destruction in different resolution devices?
imagebutton login = new imagebutton(configmodel.getloginbutton(), focusable, "login.png", "plogin.png",0x9cbe95); horizontalfieldmanager hfm = new horizontalfieldmanager(field.field_hcenter); horizontalfieldmanager kenexalogohfm = new horizontalfieldmanager(hfm.field_left); horizontalfieldmanager loginbuttonhfm = new horizontalfieldmanager(hfm.field_right); bitmap logo = bitmap.getbitmapresource("logo.png"); nullfield nullfield = new nullfield(); bitmapfield kenexalogo = new bitmapfield(logo); kenexalogohfm.add(new labelfield("", non_focusable)); kenexalogohfm.add(kenexalogo); kenexalogohfm.add(nullfield); loginbuttonhfm.add(login); hfm.setpadding(0, 5, 0, 5); hfm.add(kenexalogohfm); hfm.add(loginbuttonhfm) add(hfm); 
following code imagebutton
public class imagebutton extends field{ //image button class private string _label; private int _labelheight; private int _labelwidth; private font _font; private bitmap _currentpicture; private bitmap _onpicture; private bitmap _offpicture; int color; public imagebutton(string text, long style ,string img, string img_hvr, int color){ super(style); _offpicture = bitmap.getbitmapresource(img); _onpicture = bitmap.getbitmapresource(img_hvr); _font = font.getdefault().derive(font.bold, 7, ui.units_pt); _label = text; _labelheight = _onpicture.getheight(); _labelwidth = _onpicture.getwidth(); this.color = color; _currentpicture = _offpicture; } public void setimage(string img){ _offpicture = bitmap.getbitmapresource(img); _currentpicture = _offpicture; } /** * @return text on button */ public void settext(string text){ _label = text; } string gettext(){ return _label; } /** * field implementation. * @see net.rim.device.api.ui.field#getpreferredheight() */ public int getpreferredheight(){ return _labelheight; } /** * field implementation. * @see net.rim.device.api.ui.field#getpreferredwidth() */ public int getpreferredwidth(){ return _labelwidth; } /** * field implementation. changes picture when focus gained. * @see net.rim.device.api.ui.field#onfocus(int) */ protected void onfocus(int direction) { _currentpicture = _onpicture; // invalidate(); super.onfocus(direction); } /** * field implementation. changes picture when focus lost. * @see net.rim.device.api.ui.field#onunfocus() */ protected void onunfocus() { _currentpicture = _offpicture; invalidate(); super.onunfocus(); } /** * field implementation. * @see net.rim.device.api.ui.field#drawfocus(graphics, boolean) */ // protected void drawfocus(graphics graphics, boolean on) { // // nothing // } protected void drawfocus(graphics graphics, boolean on) { if (on) { //draw own custom focus. } } /** * field implementation. * @see net.rim.device.api.ui.field#layout(int, int) */ protected void layout(int width, int height) { setextent(math.min( width, getpreferredwidth()), math.min( height, getpreferredheight())); } /** * field implementation. * @see net.rim.device.api.ui.field#paint(graphics) */ protected void paint(graphics graphics){ // first draw background colour , picture graphics.setcolor(this.color); graphics.fillrect(0, 0, getwidth(), getheight()); graphics.drawbitmap(0, 0, getwidth(), getheight(), _currentpicture, 0, 0); // draw text graphics.setcolor(color.white); graphics.setfont(_font); graphics.setfont(graphics.getfont().derive(font.bold)); graphics.drawtext(_label, 5,9, (int)( getstyle() & drawstyle.ellipsis | drawstyle.valign_mask | drawstyle.halign_mask), getwidth() - 6 ); } /** * overridden event dispatch thread can catch event * instead of having caught here.. * @see net.rim.device.api.ui.field#navigationclick(int, int) */ protected boolean navigationclick(int status, int time){ fieldchangenotify(1); return true; } }
i recommend getting rid of couple of unnecessary horizontalfieldmanager objects, , using code:
horizontalfieldmanager hfm = new horizontalfieldmanager(field.use_all_width); verticalfieldmanager vfm = new verticalfieldmanager(field.use_all_width); bitmap logo = bitmap.getbitmapresource("logo.png"); bitmapfield kenexalogo = new bitmapfield(logo, field.field_left); hfm.add(new nullfield()); // take focus login button hfm.add(kenexalogo); vfm.add(new buttonfield("login", field.field_right)); hfm.add(vfm); hfm.setpadding(0, 5, 0, 5); add(hfm); note: had create new buttonfield, because didn't show how login button created (in original question), , it's important, here.
the problem horizontalfieldmanager tries efficient space uses. so, if add 2 real fields (the logo, , button), , not enough fill width, it's not going put button on right.
you need add verticalfieldmanager horiztonalfieldmanager, tell use available width, , pass button, which has been created field_right flag. flags fields tell parent containers lay them out. verticalfieldmanager respect field_right , put login button @ right, wish.
more
i might suggest instead of hard coding 5 pixel right , left padding, set padding constant that's given percentage of screen width:
int pad = display.getwidth() / 100; hfm.setpadding(0, pad, 0, pad); but, that's separate issue.
another thing find useful when trying debug layout problems set different background color on each manager or field in layout. helps see , understand what's happening. use this:
hfm.setbackground(backgroundfactory.createsolidbackground(color.red)); // todo: remove!
Comments
Post a Comment