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
Developer
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Navigation of LEFT/RIGHT Keys are not updating my customize buttons

Hi

Added my own Customized button (myButton) with Image on top of it.

I am trying to use this myButton along with existing Blackberry Buttons in a Horizontal Field Manager,

I am having problem with LEFT/RIGHT Keys.

 

Added three Blackberry buttons and one myButton.

When I am running on 9000 Simulator and when I am pressing LEFT/RIGHT Keys, Blackberry buttons are selected accordingly and I am able to see the action on Listeners.

 

But I am not able see any action or change of ON/OFF Buttton images (which I provided as parameters)

 

Please help

 

Thanks

Siva

Thanks
Siva
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

I don't have left and right keys on my Bold keyboard.

 

I'm unsure of exactly what the issue is here.  What Listeners are you using?  How are the ON/OFF Button images in your Button actually used?

 

Sounds like your Button is not correctly changing when it is focused.  To help with that I suspect we need to see your code.

Developer
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

[ Edited ]

Here is the code I am using.

In this, I have myButton  as Bitmap Button, but when I use Navigation keys on Blackberry 9000, I am able to move over 3 buttons (Text Buttons), but not on myButton.

 

 

public class myScreen extends MainScreen
{
	HorizontalFieldManager	_fieldManagerButtons;
	HorizontalFieldManager	_fieldManagerContextHeader;
	VerticalFieldManager	_fieldManagerContext;
	VerticalFieldManager	_fieldManagerMessage;
	
	BitmapField _BitMapRegion;
	Bitmap _EmailImage, _TwitterImage, _FaceBookImage;
	LabelField _ContextHeader;
	BasicEditField _MessageEditField, _FromEMailEditField, _ToEMailEditField;
	String _EmailCText, _FaceBookCText, _TwitterCText;
	String _EmailTextValue, _FacebookTextValue, _TwitterTextValue;
	String _FromEMailTextValue, _ToEmailTextValue;
	
	int _ButtonActive = 0;
	int _PreviousButton = 0;

	public myScreen()
	{
		super();
		LabelField title = new LabelField(" Card Details ",LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
		setTitle(title);
		
		_fieldManagerButtons = new HorizontalFieldManager();
		_fieldManagerContextHeader = new HorizontalFieldManager();
		_fieldManagerContext = new VerticalFieldManager();
		_fieldManagerMessage = new VerticalFieldManager();
		
		add(_fieldManagerButtons); 
		add(_fieldManagerContextHeader);
		add(_fieldManagerContext);
		add(_fieldManagerMessage);
		
		myFieldChangeListener changeListenerEMail = new myFieldChangeListener(0);
		myFocusChangeListener focusChangeListenerEMail = new myFocusChangeListener(0);
		
		myFieldChangeListener changeListenerFaceBook = new myFieldChangeListener(1);
		myFocusChangeListener focusChangeListenerFaceBook = new myFocusChangeListener(1);
		
		myFieldChangeListener changeListenerTwitter = new myFieldChangeListener(2);
		myFocusChangeListener focusChangeListenerTwitter = new myFocusChangeListener(2);
		
		myFieldChangeListener changeListenerSend = new myFieldChangeListener(3);
		myFocusChangeListener focusChangeListenerSend = new myFocusChangeListener(3);
		
		_EmailImage	= Bitmap.getBitmapResource("Message.PNG");
		_EmailCText = "Send to a Friend";
		_FaceBookImage	= Bitmap.getBitmapResource("FaceBook.PNG");
		_FaceBookCText = "Share on Facebook";
		_TwitterImage	= Bitmap.getBitmapResource("Twitter.PNG");
		_TwitterCText = "Share on Twitter";

		ButtonField MessageButton = new ButtonField("Email");
		ButtonField FaceBookButton = new ButtonField("Facebook");
		ButtonField TwitterButton = new ButtonField("Twitter");
		sButton myButton = new sButton("Comment1.PNG","Comment2.PNG",0);
		ButtonField SendButton = new ButtonField("Send");
		
		MessageButton.setChangeListener(changeListenerEMail);
		MessageButton.setFocusListener(focusChangeListenerEMail);
		
		FaceBookButton.setChangeListener(changeListenerFaceBook);
		FaceBookButton.setFocusListener(focusChangeListenerFaceBook);

		TwitterButton.setChangeListener(changeListenerTwitter);
		TwitterButton.setFocusListener(focusChangeListenerTwitter);

		SendButton.setChangeListener(changeListenerSend);
		SendButton.setFocusListener(focusChangeListenerSend);

		_fieldManagerButtons.add(MessageButton);
		_fieldManagerButtons.add(FaceBookButton);
		_fieldManagerButtons.add(TwitterButton);
		_fieldManagerButtons.add(myButton);
		
		_ContextHeader = new LabelField(_EmailCText);
		_fieldManagerContextHeader.add(_ContextHeader);
		
		_FromEMailEditField = new AutoTextEditField("Your Email: ", "", 100, BasicEditField.FILTER_EMAIL);
		_ToEMailEditField = new AutoTextEditField("Friend Email: ", "", 100, BasicEditField.FILTER_EMAIL);

	        XYEdges thickPadding = new XYEdges(10,10,10,10);
        	Border roundedBorder = BorderFactory.createRoundedBorder(thickPadding, Border.STYLE_SOLID);
	        Background solidBackground = BackgroundFactory.createSolidBackground(Color.WHITE);
        
		_MessageEditField = new AutoTextEditField("Message: ", "", 255, BasicEditField.SPELLCHECKABLE);
		_MessageEditField.setBorder(roundedBorder);
		_MessageEditField.setBackground(solidBackground);
		_fieldManagerMessage.add(_MessageEditField);
		_fieldManagerMessage.add(SendButton);
	}
	
	protected void updateChangeStatus(int button)
	{
		if(button > 2) return;

		_PreviousButton = _ButtonActive;
		_ButtonActive = button;

		if(_PreviousButton == 0) {
			_ToEmailTextValue = _ToEMailEditField.getText();
			_FromEMailTextValue = _FromEMailEditField.getText();
			_EmailTextValue = _MessageEditField.getText();			
			_fieldManagerContext.deleteAll();
		}else if (_PreviousButton == 1){
			_FacebookTextValue = _MessageEditField.getText();			
		}else if (_PreviousButton == 2){
			_TwitterTextValue = _MessageEditField.getText();			
		}

		if (_ButtonActive == 0)	{
			_ContextHeader.setText(_EmailCText);
			_ToEMailEditField.setText(_ToEmailTextValue);
			_FromEMailEditField.setText(_FromEMailTextValue);
			_MessageEditField.setText(_EmailTextValue);

	        XYEdges thickPadding = new XYEdges(10,10,10,10);
	        Border roundedBorder = BorderFactory.createRoundedBorder(thickPadding, Border.STYLE_SOLID|Border.STYLE_DOTTED);
	        Background solidBackground = BackgroundFactory.createSolidBackground(Color.WHITE);

	        _ToEMailEditField.setBorder(roundedBorder);
	        _ToEMailEditField.setBackground(solidBackground);	        

	        _FromEMailEditField.setBorder(roundedBorder);
	        _FromEMailEditField.setBackground(solidBackground);	        
	        
			_fieldManagerContext.add(_ToEMailEditField);
			_fieldManagerContext.add(_FromEMailEditField);
		}else if (_ButtonActive == 1) {
			_ContextHeader.setText(_FaceBookCText);
			_MessageEditField.setText(_FacebookTextValue);
		}else if (_ButtonActive == 2) {
			_ContextHeader.setText(_TwitterCText);
			_MessageEditField.setText(_TwitterTextValue);
		}
	}

	protected void makeMenu(Menu menu, int instance)
	{
		menu.add(_close);
	}
	
	private MenuItem _close = new MenuItem("Close", 110, 10)
	{
		public void run()
		{
			onClose();
		}
	};

	public boolean onClose()
	{
		Dialog.alert("Goodbye!");
		System.exit(0);
		return true;
	}
	
	private class myFieldChangeListener implements FieldChangeListener {

		private int _updateItem;

		myFieldChangeListener(int inValue) {
			_updateItem = inValue;
		}
		public void fieldChanged(Field field, int context)
		{
			updateChangeStatus(_updateItem);
		}
	}
	
	private class myFocusChangeListener implements FocusChangeListener {
		private int _updateItem;

		myFocusChangeListener(int inValue) {
			_updateItem = inValue;
		}
		public void focusChanged(Field field, int eventType)
		{
			updateChangeStatus(_updateItem);
		}
	}
}

public class sButton extends Field {

	private Bitmap _currentPicture;
	private Bitmap _onPicture;
	private Bitmap _offPicture;
	private int _prefferredHeight, _prefferredWidth;
    
	sButton (String onImage, String offImage, long style){
		super(style);
		_offPicture = Bitmap.getBitmapResource(offImage);
		_onPicture = Bitmap.getBitmapResource(onImage);
		_currentPicture = _onPicture;
    	
		_prefferredHeight = _offPicture.getHeight();
		if(_onPicture.getHeight() > _prefferredHeight) {
			_prefferredHeight = _onPicture.getHeight();
		} else if(45 > _prefferredHeight) {
			_prefferredHeight = 45;
		}
    	
		_prefferredWidth = _offPicture.getWidth();
		if(_onPicture.getWidth() > _prefferredWidth) {
			_prefferredWidth = _onPicture.getWidth();
		} else if(100 > _prefferredWidth) {
			_prefferredHeight = 100;
		}
	}
    
	public int getPreferredHeight(){
		return _prefferredHeight;
	}

	public int getPreferredWidth(){
		return _prefferredWidth;
	}

	protected void onFocus(int direction){
		_currentPicture = _offPicture;
		invalidate();
	}

	protected void onUnfocus(){
		_currentPicture = _onPicture;
		invalidate();
	}
    
	protected void layout(int width, int height) {
		 setExtent(Math.min( width, getPreferredWidth()), Math.min( height, getPreferredHeight()));
	}

	protected void paint(Graphics graphics) {
		graphics.setBackgroundColor(Color.RED);
		graphics.clear();
		graphics.fillRect(0, 0, getWidth(), getHeight());
		graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
	}
	
	protected boolean navigationClick(int status, int time){
		fieldChangeNotify(1);
		return super.navigationClick(status, time);
	}
}

 

 

 

Thanks
Siva
Developer
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

Change the inheritance of my sButton class from "Field" to "BitmapField".

Now I can highlight myButton with Navigation Keys, but I am seeing one problem

 

Bitmap is drawn on top of Buttom with some margins around the Margin even though I draw with 0 offset on Left and Right

i.e.

graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);

 

Can any one help to solve this problem.

 

Thanks

Siva

Thanks
Siva
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

Try calling super.onFocus and super.onUnfocus in your sButton Field.

 

Just as a suggestion, I would have a serious look at this code.  I might be wrong, but I think you can achieve what you are trying to with about 1/2 as many lines and significantly fewer objects.  Are you sure you need to create a new object for each Change and Focus Listener for every button?  Just a thought.

Developer
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

Hi Peter

 

I tried two ways

 

1. sButton inherited from Field Class.

called super.onFocus and super.onUnFocus at repective places

I don't see any thing new from old behaviour, i.e, I am not able to Navigate to the Customized button.

 

2. sButton inheritend from ButtonField Class.

called super.onFocus and super.onUnFocus at repective places

Now I can see that highlight around the button, i.e. Grey when unhighlighted and Blue when highlighted.

but I have two problems

2.1. I am not getting any action Listerners, i.e. FieldChangeListener & FocusChangeListener.

2.2. Button Image is drawn inside Button with some pixels of native button.

        Background of button is shown Grey when un-highlighted and Blue when highlighted.

        my Button Image is changed correctly with onPicture and offPicture accordingly.

Here is the snippet of my button class extending from ButtonField

 

public class sButton extends ButtonField {

	private Bitmap _currentPicture;
	private Bitmap _onPicture;
	private Bitmap _offPicture;
	private int _prefferredHeight, _prefferredWidth;

	sButton (String onImage, String offImage, long style){
		super(style);
	    	_offPicture = Bitmap.getBitmapResource(offImage);
    		_onPicture = Bitmap.getBitmapResource(onImage);
	    	_currentPicture = _onPicture;
    	
    		_prefferredHeight = _offPicture.getHeight();
	    	if(_onPicture.getHeight() > _prefferredHeight) {
    			_prefferredHeight = _onPicture.getHeight();
	    	} else if(45 > _prefferredHeight) {
    			_prefferredHeight = 45;
	    	}

    		_prefferredWidth = _offPicture.getWidth();
	    	if(_onPicture.getWidth() > _prefferredWidth) {
    			_prefferredWidth = _onPicture.getWidth();
	    	} else if(100 > _prefferredWidth) {
    			_prefferredHeight = 100;
	    	}
    	}

	public int getPreferredHeight(){
		return _prefferredHeight;
	}

	public int getPreferredWidth(){
		return _prefferredWidth;
	}

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

	protected void onUnfocus(){
		_currentPicture = _onPicture;
		invalidate();
		super.onUnfocus();
	}

	protected void layout(int width, int height) {
		setExtent(Math.min( width, getPreferredWidth()), Math.min( height, getPreferredHeight()));
	}

	protected void paint(Graphics graphics) {
		graphics.setBackgroundColor(Color.RED);
		graphics.clear();
		graphics.fillRect(0, 0, getWidth(), getHeight());
		graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
	}

	protected boolean navigationClick(int status, int time){
		fieldChangeNotify(1);
		return super.navigationClick(status, time);
	}
}

 

Thanks

Siva

 

 

Thanks
Siva
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

Will need to look at his in more detail which I don't have time for right now, hopefully someone else has.

 

But a minor comment. re:

" I am not able to Navigate to the Customized button"

 

Is the Button actually focusable?  You might have to set this as a style, or override isFocusable().

 

Making your customized button focusable might get over this issue. 

Developer
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

With following function overridingI am able to higlight the myButton with Navigational keys, thanks for the same.

 

 

    public boolean isFocusable() {
    	return true;
    }

 

I am still seeing the problem mentioned in 2.1

i.e.

"2.1. I am not getting any action Listerners, i.e. FieldChangeListener & FocusChangeListener."

 

Can you please help.

 

Thanks
Siva
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

Maybe I'm missing something, but do you actually set the Change and Focus listener for myButton?

 

I don't see this in the code you originally supplied.

Developer
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: Navigation of LEFT/RIGHT Keys are not updating my customize buttons

Hi Peter

I got what is the problem for 2.1 and is resolved by changing following "if" Condition.

 

	protected void updateChangeStatus(int button)
{
if(button > 2) return;

 

With this I would say I got the solution for this post.

 

Thanks

Siva

Thanks
Siva