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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone

Re: Focus Color of ObjectChoiceField

Can you please tell me how to do it?
Please use plain text.
Developer
BrajeshSanodiya
Posts: 101
Registered: ‎08-03-2011
My Carrier: BlackBerry Developer

Re: Focus Color of ObjectChoiceField

[ Edited ]

Use this CustomChoiceField 

 

		DopDownImgOff = Bitmap.getBitmapResource("btn_back_off.png");
		DopDownImgOn = Bitmap.getBitmapResource("btn_back_on.png");

 

put these images for background onFocus or offFocus.

 

package mypackage;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.component.ObjectChoiceField;

public class CustomChoiceField extends ObjectChoiceField {

	private int _bgWidth;
	private int _bgHeight;
	private boolean _hasFocus = false;
	String[] m_Choices;
	private int m_ChoiceFieldWidth;
	private int m_TextMarginRight = 5;
	private int m_TextMarginLeft = 5;
	private Bitmap DopDownImgOn, DopDownImgOff;
	private Font FONT_PLAIN;
	private String visibleText;
	private int m_MarginTop;

	public CustomChoiceField(String label, String[] choices, int initialIndex) {
		super(label, choices, initialIndex, CustomChoiceField.FIELD_LEFT);
	
		m_Choices = choices;
		
		//  images for background of customechoicefield
		
		DopDownImgOff = Bitmap.getBitmapResource("btn_back_off.png");
		DopDownImgOn = Bitmap.getBitmapResource("btn_back_on.png");
		_bgWidth=DopDownImgOff.getWidth();
		_bgHeight=DopDownImgOff.getHeight();
		
		FONT_PLAIN = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt);

		Font currFont = Font.getDefault().derive(Font.PLAIN, 3, Ui.UNITS_pt);
		for (int i = 1; i < choices.length; i++) {
			if (m_ChoiceFieldWidth < currFont.getAdvance(choices[i]))
				m_ChoiceFieldWidth = currFont.getAdvance(choices[i]);
		}
		m_ChoiceFieldWidth += m_TextMarginLeft + m_TextMarginRight;
	}

	public int getPreferredHeight() {
		return _bgHeight;
	}

	public int getPreferredWidth() {
		return _bgWidth;
	}

	protected void layout(int width, int height) {
		if (m_ChoiceFieldWidth == 0) {
			m_ChoiceFieldWidth = width;
		}
		super.layout(width, height);
		setExtent(_bgWidth, _bgHeight);
	}

	protected void onFocus(int direction) {
		_hasFocus = true;
		invalidate();
	}

	public void setBusiness(String[] choices) {
		this.m_Choices = choices;
		invalidate();
	}

	protected void onUnfocus() {
		_hasFocus = false;
		invalidate();
	}

	protected void paint(Graphics g)
	{
		if (DopDownImgOn instanceof Bitmap) {

			m_MarginTop = (DopDownImgOn.getHeight() - FONT_PLAIN.getHeight()) / 2;
			visibleText = (getSelectedIndex() == -1 || getSelectedIndex() >= m_Choices.length) ? ""	: m_Choices[getSelectedIndex()];

			if (_hasFocus == false)
				g.drawBitmap(0, 0, DopDownImgOff.getWidth(), DopDownImgOff.getHeight(), DopDownImgOff, 0, 0);
			else
				g.drawBitmap(0, 0, DopDownImgOn.getWidth(), DopDownImgOn.getHeight(), DopDownImgOn, 0, 0);

			g.drawText(visibleText, m_TextMarginLeft, m_MarginTop, DrawStyle.ELLIPSIS, DopDownImgOn.getWidth() - 30);
			g.setFont(FONT_PLAIN);
		}
	}

}

 

 

 

 

 

 

-----------------------------------------------------------------------
"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
-----------------------------------------------------------------------
Please use plain text.
Developer
simon_hain
Posts: 13,796
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Focus Color of ObjectChoiceField

calling invalidate in the paint method is a good method to get an infinite loop.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone

Re: Focus Color of ObjectChoiceField

[ Edited ]

Thanx. but I am getting nothing on screen using this code.What are those images?

Simon: i removed invalidate(). Instead of using images,is it possible to use only colors?

Please use plain text.
Developer
BrajeshSanodiya
Posts: 101
Registered: ‎08-03-2011
My Carrier: BlackBerry Developer

Re: Focus Color of ObjectChoiceField

btn_back_off

 

btn_back_off.png

 

btn_back_on

 

btn_back_on.png

 

 

 

-----------------------------------------------------------------------
"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
-----------------------------------------------------------------------
Please use plain text.
Developer
BrajeshSanodiya
Posts: 101
Registered: ‎08-03-2011
My Carrier: BlackBerry Developer

Re: Focus Color of ObjectChoiceField

Thanks simon_hain,

 

To give me attention i have removed invalidate() in previous code.

 

-----------------------------------------------------------------------
"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
-----------------------------------------------------------------------
Please use plain text.
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone

Re: Focus Color of ObjectChoiceField

PFA

I am getting something like this on screen.

Please use plain text.
Developer
BrajeshSanodiya
Posts: 101
Registered: ‎08-03-2011
My Carrier: BlackBerry Developer

Re: Focus Color of ObjectChoiceField

Hi Jovinz,

 

check your image path.

 

this code is running fine in my end.

 

check the screen shot attachted

9800.jpg

-----------------------------------------------------------------------
"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
-----------------------------------------------------------------------
Please use plain text.
New Developer
shivam284
Posts: 100
Registered: ‎01-23-2012
My Carrier: Blackberry

Re: Focus Color of ObjectChoiceField

Hi Jovinz,

 

I have checked the code and it's works fine for me. I think there is some problem in your declaration so better check the code once and Pass the valid path image.

 

Thanks BrajeshSanodiya for the Above code.

 

 


Thanks & Regards,
Kumar Shivam
Please use plain text.
Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Carrier: Vodafone

Re: Focus Color of ObjectChoiceField

Ya its working fine now. but can u tell me when i click on customchoicefield the choicelist is opened. can i change color of that choicelist ?
Please use plain text.