Search code examples
blackberryblackberry-simulator

How i can set Focus in Custom Horizontal Field Manager and Click event in Blackberry?


Hi, I had Generate custom layouts in my screen, i want change color or image of horizontal Field manager when it focus and also perform Click event of selected horizontal Field manager. For More reference see screen shot.

this is my code which i had try..

HorizontalFieldManager hfmrow=new HorizontalFieldManager(USE_ALL_WIDTH|HorizontalFieldManager.FOCUSABLE | Field.FOCUSABLE)
                             {
                                    public void onFocus(int direction) 
                                      {
                                            isFocus  = true;
                                            super.onFocus(direction);
                                            invalidate();

                                      }
                                    public void onUnfocus() 
                                    {
                                        invalidate();
                                        isFocus  = false;
                                        super.onUnfocus();



                                    }
                                    public void paint(Graphics g)
                                    {   
                                        if(isFocus) 
                                        { 
                                            hfmrow.setBackground(BackgroundFactory.createSolidBackground(Color.RED));
                                            invalidate();                                                                                    
                                            super.paint(g);

                                        } 
                                        else 
                                        { 
                                            hfmrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));                                         
                                            invalidate();                                        
                                            super.paint(g); 
                                        }
                                    }

                                protected boolean navigationClick(int status, int time) 
                                {
                                    Dialog.alert("You are in hfm"+id);
                                    return true;
                                }

                             };                         


                            hfmrow.setMargin(0, 10, 0, 10);
                            hfmrow.add(new NullField(Field.FOCUSABLE));

Any help is appreciate.. Thanx in advance

enter image description here


Solution

  • Here how can you do it this code is for changing color when you are selecting hfm

    import net.rim.device.api.system.Display;
    import net.rim.device.api.ui.Color;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.FieldChangeListener;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.Manager;
    import net.rim.device.api.ui.TouchEvent;
    import net.rim.device.api.ui.Touchscreen;
    import net.rim.device.api.ui.UiApplication;
    import net.rim.device.api.ui.component.Dialog;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.component.NullField;
    import net.rim.device.api.ui.container.HorizontalFieldManager;
    import net.rim.device.api.ui.container.MainScreen;
    
    public class sample extends MainScreen implements FieldChangeListener{
    
    HorizontalFieldManager logInDetailManager=null;
    int background_color=0;
    
    public sample() {
    
        logInDetailManager = new HorizontalFieldManager(Manager.USE_ALL_WIDTH|Field.FOCUSABLE){
            protected void sublayout(int maxWidth, int maxHeight) {
                    int height=40;
                super.sublayout(Display.getWidth(), height);
                setExtent(Display.getWidth(), height);
            }
            protected void paint(Graphics graphics) {
    
                graphics.setBackgroundColor(background_color);
                graphics.clear();
                invalidate();
                super.paint(graphics);
            }
            protected void onFocus(int direction) {
                super.onFocus(direction);
                background_color=Color.RED;
                invalidate();
            }
            protected void onUnfocus() {
                invalidate();
                background_color=Color.GREEN;
    
            }
            protected boolean navigationClick(int status, int time) {
    
                if(Touchscreen.isSupported()){
                    return false;
                }else{
                    fieldChangeNotify(1);
                    return true;
                }
    
            }
            protected boolean touchEvent(TouchEvent message)
            {
                if (TouchEvent.CLICK == message.getEvent())
                {
                    FieldChangeListener listener = getChangeListener();
                    if (null != listener)
                        this.setFocus();
                        listener.fieldChanged(this, 1);
                }
                return super.touchEvent(message);
            }
        };
        logInDetailManager.setChangeListener(this);
        logInDetailManager.add(new LabelField("hello"));
        logInDetailManager.add(new NullField(Field.FOCUSABLE));
        add(logInDetailManager);
        add(new LabelField("good",Field.FOCUSABLE));    
    }
    
    public void fieldChanged(Field field, int context) {            
        if(field==logInDetailManager){                  
            UiApplication.getUiApplication().invokeLater(new Runnable() {                          
                public void run() {                                 
                    Dialog.inform("Hi how are you?");                          
                }                    
            });          
        }   
    }
    
    }