I created a sort of a grid with multiple vertical and horizontal fieldmanagers. The code to create the grid is:
public class CategoriasScreen extends MainScreen {
private int colores [] = {Color.BLUE, Color.ALICEBLUE, Color.CHOCOLATE, Color.DARKORANGE, Color.YELLOW};
private int rows;
public CategoriasScreen(){
VerticalFieldManager row;
CategoriaFieldManager cat;
HorizontalFieldManager par;
EncodedImage eImage;
LabelField lf;
rows = 1;
Font font = getFont().derive(Font.PLAIN, Font.getDefault().getHeight() - 8);
setTitle("Categorias");
for(int i = 0; i < rows; i++){
row = new VerticalFieldManager(Field.FIELD_VCENTER | Manager.USE_ALL_WIDTH | Field.NON_FOCUSABLE);
par = new HorizontalFieldManager(Field.FIELD_HCENTER | Field.NON_FOCUSABLE);
cat = new CategoriaFieldManager(i);
eImage = EncodedImage.getEncodedImageResource("img/categoria" + i +".png");
eImage = LoaderScreen.resize(eImage, (int) Display.getWidth() / 5, (int) Display.getHeight() / 5, true);
cat.add(new BitmapField(eImage.getBitmap()));
lf = new LabelField("Categoria " + i, Field.FIELD_HCENTER);
lf.setFont(font);
cat.add(lf);
par.add(cat);
cat = new CategoriaFieldManager(i + 2);
eImage = EncodedImage.getEncodedImageResource("img/categoria" + (i + 2) +".png");
eImage = LoaderScreen.resize(eImage, (int) Display.getWidth() / 5, (int) Display.getHeight() / 5, true);
cat.add(new BitmapField(eImage.getBitmap()));
lf = new LabelField("Categoria " + (i + 2), Field.FIELD_HCENTER);
lf.setFont(font);
cat.add(lf);
par.add(cat);
row.add(par);
add(row);
//add(cat);
}
}
}
For the element inside the grid i extended a VerticalFieldManager and implemented the FieldChangeListener:
public class CategoriaFieldManager extends VerticalFieldManager implements FieldChangeListener{
private int colores [] = {Color.BLUE, Color.ALICEBLUE, Color.CHOCOLATE, Color.DARKORANGE, Color.YELLOW};
private int _idCategoria;
public CategoriaFieldManager(int idCategoria){
super(Field.FOCUSABLE);
_idCategoria = idCategoria;
setBackground(BackgroundFactory.createSolidBackground(colores[0]));
this.setPadding(new XYEdges(15,30,10,30));
this.setBorder(BorderFactory.createSimpleBorder(new XYEdges(20,20,20,20), Border.STYLE_TRANSPARENT));
//cat.setCookie(new Integer(i));
this.setChangeListener(this);
}
protected void sublayout(int width, int height){
super.sublayout((int) Display.getWidth() / 5 , (int) Display.getWidth() / 5);
setExtent((int) Display.getWidth() / 5, (int) Display.getWidth() / 5);
}
protected void onFocus(int direction){
setBackground(BackgroundFactory.createSolidBackground(colores[1]));
}
protected void onUnfocus(){
setBackground(BackgroundFactory.createSolidBackground(colores[0]));
}
public boolean isFocusable(){
return true;
}
protected boolean touchEvent( TouchEvent message ) {
int x = message.getX( 1 );
int y = message.getY( 1 );
if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
// Outside the field
return false;
}
switch( message.getEvent() ) {
case TouchEvent.UNCLICK:
fieldChangeNotify(0);
return true;
}
return super.touchEvent( message );
}
protected boolean navigationClick(int status, int time) {
if (status != 0) { // you did not have this check
fieldChangeNotify(0);
}
return true;
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen( new ListadoEstablecimientosScreen(_idCategoria) );
/*UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run() {
Dialog.alert("hola mundo");
}
});*/
}
}
The problem that I've found is that when I touch the screen (anywhere) and then use the trackpad the phone freezes, I have to restart it to use it again. Then comes a notification that says the application was using too many resources. If it helps I've been testing in a BlackBerry Bold 9900