If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
nordavinder
Posts: 23
Registered: ‎12-02-2012
My Device: BlackBerry 8300
My Carrier: Personal
Accepted Solution

BlackBerry - How to properly handle a TAP/CLICK in a custom Manager

I need to set up a click/tap event for a custom Manager.

 

The Manager contains a couple of elements, but I need that the whole Manager respond to the event, so if the user taps/clicks any element inside the Manager, the event handler should be called too. What I have done is to attach a changeListener to a NullField that takes all width and height, but the fieldChanged method is never called

public class TestManager extends Manager{
    private BitmapField _icon;
    private LabelField _lblTitle;
    private NullField nullField;
    public TestManager(Bitmap pIcon){
        super(Manager.FOCUSABLE);
        _icon = new BitmapField(pIcon);
        _lblTitle = new LabelField("This is the title");
        nullField = new NullField(NullField.FOCUSABLE);
        add(_icon);
        add(_lblTitle);
        add(nullField);
            nullField.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            doSomething();
        }
    });

    }

    public void doSomething(){
    System.out.println("do something!!");
}
    public void paint(Graphics graphics)
    {
       graphics.setColor(Color.DELIMITER_COLOR);
       graphics.fillRect(0, 20, Display.getWidth(), 50);

       super.paint(graphics);
    }

    protected void paintBackground(Graphics arg0) {
       arg0.setColor(Color.LIGHTGRAY);
       arg0.fillRect(0, 0, Display.getWidth(), Display.getHeight());
       super.paint(arg0);
    }

    protected void sublayout(int width, int height) {
        setPositionChild(_icon, UIFactory.getBestX(10), UIFactory.getBestX(8));
        setPositionChild(_lblTitle, UIFactory.getBestX(60), _topMarginTitle!=0?UIFactory.getBestX(_topMarginTitle):UIFactory.getBestX(15));
        setPositionChild(nullField, 0, 0);
        layoutChild(_icon, width, UIFactory.getBestX(50));
        layoutChild(_lblTitle, width, UIFactory.getBestX(50));
        layoutChild(nullField, width, height);
        setExtent(width, height);
    }

      protected boolean navigationClick(int status, int time){ 
            invalidate();
            doSomething();
            return super.navigationClick(status, time);
        }

}

 

 

 

 

 

How should I make this whole Manager to respond a click/tap?

I'm using the API 5.0

Thanks in advance!

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: BlackBerry - How to properly handle a TAP/CLICK in a custom Manager

The trouble with using the NullField to do this, is that NullField is not programmed to handle any interaction except focus.

 

So you need to override navigationClick, as you have done for the Manager.  So that it connects in to your FieldChangeListener plumbing (which is the correct thing to do in my opinion. you should invoke fieldChangeNotify, for example:

 

        protected boolean navigationClick(int status, int time) {
                this.fieldChangeNotify(2);
                return true;
        }

 

Now you will override this in the NullFeld because it gets focus.  You need to make sure that nothing else in the Manager is focusable. 

 

But you will want some indication in the Manager that is has focus, so the user knows - especially for when the user is using the trackpad.  To do this, make the Manager a focusChangeListener for the NullField and have the listener code set a flag according whether focus has been gained or not and then invalidate() itself.  In paint() check the flag and if set, paint for focus, if not, paint for no focus. 

 

Come back if this is not clear. 

 

Byt the way, the sublayout in your Manager looks a little ropey to me, especially this:

setExtent(width, height);

 

You really want to set the extent to be large enough to accomodate all your Field, and no bigger. 

 

There are other things I am not sure about, but if this is working for you, then I guess it is OK. 

 

Good luck

Please use plain text.
Contributor
nordavinder
Posts: 23
Registered: ‎12-02-2012
My Device: BlackBerry 8300
My Carrier: Personal

Re: BlackBerry - How to properly handle a TAP/CLICK in a custom Manager

thanks Peter!

Following your suggestion, this is what I did:

public class BaseTouchManager extends Manager {
	public interface BaseTouchManagerCallBack {
		public void doSomething();
	}

	private BaseTouchManagerCallBack _callBack;

	public BaseTouchManager(BaseTouchManagerCallBack pCallBack) {
		super(0);
		_callBack = pCallBack;
		setChangeListener(new FieldChangeListener() {
			
			public void fieldChanged(Field field, int context) {
				_callBack.doSomething();
			}
		});
		
		add(new NullField(Field.FOCUSABLE));
	}
	
	

	
	protected void sublayout(int width, int height) {
		setExtent(UIFactory.getBestX(500), UIFactory.getBestX(150));
	}



    protected void drawFocus(Graphics graphics, boolean on) {        // Don't draw the default focus  
    }

    protected void onFocus(int direction) {
        super.onFocus(direction);
        invalidate();
    }

    protected void onUnfocus() {
        super.onUnfocus();
        invalidate();
    }

    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())
        {
        	_callBack.doSomething();
        }
        return super.touchEvent(message);
    }
	public void paint(Graphics graphics)
    {
		if(_callBack!=null){
			if(isFocus())
				graphics.setColor(Color.LIGHTBLUE);
			else
				graphics.setColor(Color.WHITE);
			graphics.fillRect(getExtent().width - UIFactory.getBestX(40), 
							  getExtent().height - UIFactory.getBestX(30), 
							  Display.getWidth(), Display.getHeight());
			
		}
		super.paint(graphics);	
    }
}

It works now!
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: BlackBerry - How to properly handle a TAP/CLICK in a custom Manager

[ Edited ]

Glad you have this solved. 

 

Hope you don't mind, but I have looked at your code and have some suggestions for further improvement.

 

1) On touch screen devices, I wonder if in fact your code will fail to detect trackpad clicks - these are normally detected in navigationClick().   

 

2) navigationClick can be used on touchscreen devices to receive touchscreen clicks, so it is all you have to override to handle both touch screen clicks and track pad clicks. 

 

3) I think you have redundant code handling focus movement onto the Manager.  

 

4) I wonder what the advantage is of using BaseTouchManagerCallBack, when you can use FieldChangeListener which is effectively what you are doing anyway. 

 

5) Your paint method is bounded by the extent of the Manager, so in your current code, you are attempting to paint over an area that isn't in your Manager.  It won't do anything bad, because you are not given access to that part of the screen, but it might waste a few cycles and it looks wrong. 

 

Note that because I didn't have some of your classes, I have had to improvise....

 

Anyway, here is suggested replacement code:

 

public class BaseTouchManager extends Manager {

	private FieldChangeListener _callBack;

	public BaseTouchManager(FieldChangeListener pCallBack) {
		super(0);
		_callBack = pCallBack;
		setChangeListener(new FieldChangeListener() {
			
			public void fieldChanged(Field field, int context) {
				_callBack.fieldChanged(field, context);
			}
		});
		
		add(new NullField(Field.FOCUSABLE));
	}
	
	protected void sublayout(int width, int height) {
		setExtent(Display.getHeight()/2, 100);
	}

    protected boolean navigationClick(int status, int time) {
            fieldChangeNotify(1);
            return true;
    }
	public void paint(Graphics graphics)
    {
		if(_callBack!=null){
			if(isFocus())
				graphics.setColor(Color.LIGHTBLUE);
			else
				graphics.setColor(Color.WHITE);
			graphics.fillRect(this.getWidth() - 10, 
					this.getHeight() - 10, 
					10, 10);
			
		}
		super.paint(graphics);	
    }
}

 

Here is some test code to use this Manager:

 

public final class MyScreen extends MainScreen implements FieldChangeListener
{
    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {        

        add(new BaseTouchManager(this));

    }

    public void fieldChanged(Field field, int context) {
    	Dialog.alert("Doing something");
    }
    
}

 

 

 

Please use plain text.
Contributor
nordavinder
Posts: 23
Registered: ‎12-02-2012
My Device: BlackBerry 8300
My Carrier: Personal

Re: BlackBerry - How to properly handle a TAP/CLICK in a custom Manager

[ Edited ]

Many thanks Peter!

I'll replace the wrong parts of my code with your example!

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: BlackBerry - How to properly handle a TAP/CLICK in a custom Manager

Test to make sure they work for you first!

 

This is a good learning exercise - you now know a lot more about how the BB Managers work!

Please use plain text.