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
SumiGhosh
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone
Accepted Solution

How to get this to work? -Setting a focus to a particular element

Hi all,

I having a horizontal scrollable menu at top of the application. Below that i am having ListField, so after navigating list when i come back to my menu i am not able to focus directly to the selected menu item. Element is focused either on the right or left of the menu in the field Manager. I have attaching a sample image....  here i am getting focus to one instead of three0.pnglist

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

Re: How to get this to work? -Setting a focus to a particular element

[ Edited ]

I presume your "horizontal scrollable menu" is in a HorizontalFieldManager.  The trick with HFMs in this circumstance, is that when you scroll to them, they will focus either on the first Field if you scroll down, or the last Field if you scroll up. 

 

To do you what you want, in brief, you have to remember which Field had focus when the focus left the Manager (onUnfocus) and when it returns (onFocus), and instead of letting the Manager set the focus, you have to do it to the last selected Field.  So override these two for the HFM and see how you get on. 

 

Edit:

 

From the following you will see that onFocus gets called in other circumstances.  What the code I suggested does is make sure the setFocus is only used when focus is moved to the HFM (i.e. direction != 0).  The other test makes sure that we are not wasting cycles seting focus on something that already has it...

Please use plain text.
Developer
SumiGhosh
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: How to get this to work? -Setting a focus to a particular element

I overrided the horizontalFieldManager with onFocus and onUnFocus, now after navigating back from the list to menu i am able to setFocus on selected element but after that im not able to focus anywhere, it get stucked. Found the onFocus is called recursively.
Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to get this to work? -Setting a focus to a particular element

Show us your onFocus code?

Please use plain text.
Developer
SumiGhosh
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: How to get this to work? -Setting a focus to a particular element

[ Edited ]

menuList = new NavigationMenu(HORIZONTAL_SCROLL | NO_VERTICAL_SCROLL ){

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

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


public class NavigationMenu extends HorizontalFieldManager
{
public NavigationMenu(long style){
super(style);
}

public int getPreferredWidth() {
return Display.getWidth()-70;
}

protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth()-70, maxHeight);
}
}

settingFocus() is the function where i'm using setFocus()
public void settingFocus(){
if(focusedMenu=="news"){
news.setFocus();
}else if(...){
}else if(...){
}else if(...){
}else if(...){
}else if(...){

}
}

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

Re: How to get this to work? -Setting a focus to a particular element

[ Edited ]

Try making the following two changes.

 

1) Replace this:

 

if(focusedMenu=="news"){
news.setFocus();
}else if(...){

with

 

if(focusedMenu=="news"){

if ( !news.isFocus() ) {
news.setFocus();

}
}else if(...){

 

2. Replace this:

 

super.onFocus(direction);
settingFocus();

}

 

with

 

super.onFocus(direction);

if ( direction != 0 ) {
settingFocus();

}

}

 

Please use plain text.
Developer
SumiGhosh
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380
My Carrier: Vodafone

Re: How to get this to work? -Setting a focus to a particular element

Thanks peter... it worked.
Please use plain text.