08-30-2012 07:34 AM
08-30-2012 07:40 AM - edited 08-30-2012 08:18 AM
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);
}
}
}
08-30-2012 07:49 AM
08-30-2012 07:51 AM - edited 08-30-2012 08:19 AM
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?
08-30-2012 08:16 AM
btn_back_off
btn_back_on
08-30-2012 08:20 AM
Thanks simon_hain,
To give me attention i have removed invalidate() in previous code.
08-30-2012 08:24 AM
PFA
I am getting something like this on screen.
08-30-2012 10:33 AM
Hi Jovinz,
check your image path.
this code is running fine in my end.
check the screen shot attachted
08-31-2012 03:03 AM
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.
08-31-2012 06:33 AM