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
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone
Accepted Solution

Set focus color on clickable array of HorizontalFieldManager

I have an array of horizontal fields which contains a bitmap and a labelfield each. The whole row should be clickable which is working so far, but how can I set the focus color properly? At the moment the onFocus and onUnfocus functions are being completely ignored.

 

This is the definition of my array:

 

for (int i = 0; i < listSize; i++) {
        logInDetailManager[i] = new HorizontalFieldManager(
                Manager.USE_ALL_WIDTH | Field.FOCUSABLE) {

            protected void onFocus(int direction) {
                super.onFocus(direction);
                background_color = Color.RED;
                invalidate();
            }

            protected void onUnfocus() {
                invalidate();
                background_color = Color.GREEN;
            }

 

And this is how I add my horizontal fields:

 

logInDetailManager[i].setChangeListener(this);
logInDetailManager[i].add(dummyIcon[i]);
logInDetailManager[i].add(new LabelField("hello"));
logInDetailManager[i].add(new NullField(Field.FOCUSABLE));
add(logInDetailManager[i]);

 

Thanks for your help!!

Please use plain text.
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Set focus color on clickable array of HorizontalFieldManager

Aah got it already..

If anyone is struggling with that too: I removed onFocus() and onUnfocus and set the background color in the paint method so the whole row color is changed when focused:

 

protected void paint(Graphics graphics) {

	graphics.setBackgroundColor(isFocus() ? Color.RED : Color.GREEN);
	graphics.clear();
	invalidate();
	super.paint(graphics);
}

 

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

Re: Set focus color on clickable array of HorizontalFieldManager

As is pointed out in the other Thread you have added to here:

http://supportforums.blackberry.com/t5/Java-Development/Focusable-HorizontalFieldManager/m-p/2248171...

you have not actually got focus on your HFM.  What you have done is give the user a visible indication that the currently focused Field is contained within the HFM. 

Please use plain text.
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Set focus color on clickable array of HorizontalFieldManager

You're right, that's what I was trying to say ;-)  Sorry for confusing.

 

There's just one more problem: this method just accepts int colors, but I wanted to paint the background of the horizontalfields and the focus color with a gradient. How can I do that?

 

I already tried it with createLinearGradientBackground but the 'focus indication' never shows up.

 

           list[i].setBackground(isFocus() ? BackgroundFactory.
			createLinearGradientBackground(Color.GREEN,
			Color.LIGHTGREEN, Color.LIGHTGREEN,
			Color.GREEN) : BackgroundFactory.
			createLinearGradientBackground(Color.GREEN,
			Color.LIGHTGREEN, Color.LIGHTGREEN, Color.GREEN));

 

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

Re: Set focus color on clickable array of HorizontalFieldManager

Don't do a setBackground inside a paint method like that, it has to be done as part of the Field creation. 

 

Look at the setBackground methods available and not that you can set different backgrounds depending on the visual state.  Try doing that. 

 

Otherwise you will have to paint a gradient background yourself inside paintBackground.

Please use plain text.
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Set focus color on clickable array of HorizontalFieldManager

Yeah I already did it outide of the paint method. So I define my list, override sublayout, navigationclick and so on and after that I'm trying to set the background as shown before, depending on the state of isFocus().

None of these two backgrounds are being set, what am I doing wrong then?

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

Re: Set focus color on clickable array of HorizontalFieldManager

"I'm trying to set the background as shown before, depending on the state of isFocus()." 

Don't do that. 

 

You have these 2 choices

a) set the background when you create the HFM - use the visual states to specify that different backgrounds are used depending on the state of the Field - see the

setBackground(int visual, Background background)

method

b) Overrride paintBakcground and paint the gradient yourself,

 

Any other approach is likely to be flakey, by that I mean it may or may not work.  And it may cause performance issues. 

Please use plain text.
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Set focus color on clickable array of HorizontalFieldManager

Alright I tried the setBackground method, somehow it's still being ignored and the background remains white..?

 

Background bg_focus = (BackgroundFactory
		.createLinearGradientBackground(Color.GREEN, Color.LIGHTGREEN,
		Color.LIGHTGREEN, Color.GREEN));
Background bg_unfocus = (BackgroundFactory
		.createLinearGradientBackground(Color.RED, Color.ORANGE,
		Color.ORANGE, Color.RED));

list[i] = new HorizontalFieldManager(Manager.USE_ALL_WIDTH | Field.FOCUSABLE) { protected void sublayout(int maxWidth, int maxHeight) { int height = 110; super.sublayout(Display.getWidth(), height); setExtent(Display.getWidth(), height); } protected boolean navigationClick(int status, int time) { 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); } }; list[i].setBackground(VISUAL_STATE_FOCUS, bg_focus); list[i].setBackground(VISUAL_STATE_NORMAL, bg_unfocus);

 

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

Re: Set focus color on clickable array of HorizontalFieldManager

OK, so perhaps the HFM does not work with the visual states.  Shame.

 

I just looked for some code to give you to paint a Gradient in paintBackground and found that, in fact, I actually do what I suggested you didn't do earlier, i.e. I set a Background in onFocus and onUnfocus.  This is for some normal Fields I have extended, I have not done this for a Manager. 

 

So perhaps the approach you were trying above does work!  I would test that again, use the debugger to make sure that you actually go through the setBackground code.

 

One thing through, I think the Background is actually painted in paintBackground().  If you have overridden paint() and/or paintBackground for the HFM, you probably don't go through the standard paintbackground() and this may be the reason your code is not working - and would also explain why it is not working using visual states. 

 

So the first thing to do is create a standard HFM, with a focusable Field in it and try setting the Background using the 'visual states'.  Then test using onFocus and onUnfocus overrides. 

 

I would try this myself, but I'm a bit busy atm.  Let us know how you get on. 

 

Hope this makes sense.   

Please use plain text.
Contributor
Musipoo
Posts: 41
Registered: ‎02-04-2013
My Device: Bold 9900
My Carrier: Vodafone

Re: Set focus color on clickable array of HorizontalFieldManager

[ Edited ]

I finally got it, I first tried using the visual states with some usual LabelFields and it worked, so I transfered it to my HFM array. I found out that the problem was that the LabelFields which I added to the HFM array were overriding the paint method, although the paint method didn't explicitly change the background it disabled the setbackground function I guess? As soon as I deleted the whole paint method the example of my last comment worked!

 

Thank you so much for your help, you gave me some gread ideas to solve this!

Please use plain text.