Search This Blog

Thursday, 1 November 2012

CustomChoiceField for blackberry

Hello ,
  Here is the Class for custom choice field in blackberry.Credits to perumal_n on blackberry forums:

CODE:
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.component.ObjectChoiceField;
import java.lang.String;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.Font;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;

//import org.utility.DSConstants;

public class CustomChoiceField extends ObjectChoiceField {

private int _bgWidth = 110;
private int _bgHeight = 30;
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,CustomLongChoiceField.FIELD_RIGHT);
m_Choices = choices;

DopDownImgOff = Bitmap.getBitmapResource("dropdown_s.png");
DopDownImgOn = Bitmap.getBitmapResource("dropdown_off_s.png");
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;
_bgHeight = height;
}
super.layout(width, height);
setExtent(_bgWidth, _bgHeight);
}

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

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

protected void onUnfocus() {

_hasFocus = false;
// super.onUnfocus();
invalidate();
}

protected void paint(Graphics g)

{
// DopDownImg = Bitmap.getBitmapResource("scroll.PNG");

if (DopDownImgOn instanceof Bitmap) {

m_MarginTop = (DopDownImgOn.getHeight() - FONT_PLAIN.getHeight()) / 4;
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_TextMarginRight, m_MarginTop,
DrawStyle.ELLIPSIS, DopDownImgOn.getWidth() - 30);

g.setFont(FONT_PLAIN);
// g.pushContext(clip, xOffset, yOffset);

// g.popContext();
}
invalidate();
}

}



Use your own images ....and youmay find the problems in DSConstants.Just used to know the device model.....omit this if you want.

Regards.

No comments:

Post a Comment