BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-10-2010, 04:32 AM   #1 (permalink)
New Member
 
Join Date: Oct 2010
Model: 9700
PIN: N/A
Carrier: DTAC
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default How to customize the ObjectChoiceField

Please Login to Remove!

Hi everyone,
I try to custom the ObjectChoiceField like

customize its color,Background color ,focus color and arrow

I search an example for all day but I dont get anything work.



Do you guys have any solution to do this ?

Thanks ,
Regards
Offline  
Old 11-11-2010, 01:29 PM   #2 (permalink)
New Member
 
Join Date: Jul 2010
Model: 9530
PIN: N/A
Carrier: in photon as a S/W engineer
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi this is a sample CustomChoiceField please go through the code i used images for the object choice field and another class DSConstants is for some static values so if u need any more information i will do the need fill things :



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 = 245;
private int _bgHeight = 25;
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;

if (DSConstants.isBoldDevice) {
DopDownImgOff = Bitmap.getBitmapResource("dropdown.png");
DopDownImgOn = Bitmap.getBitmapResource("dropdown_off.png");
FONT_PLAIN = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt);

// g.drawText(visibleText, m_TextMarginRight, m_MarginTop,
// DrawStyle.ELLIPSIS, 220);
} else if (DSConstants.isPearlDevice) {
DopDownImgOff = Bitmap.getBitmapResource("droppearlon.png");
DopDownImgOn = Bitmap.getBitmapResource("droppearloff.png");
FONT_PLAIN = Font.getDefault().derive(Font.PLAIN, 3, Ui.UNITS_pt);
// g.drawText(visibleText, m_TextMarginRight, m_MarginTop,
// DrawStyle.ELLIPSIS, 125);
} else {
DopDownImgOff = Bitmap.getBitmapResource("dropdown_small.png");
DopDownImgOn = Bitmap.getBitmapResource("dropdown_off_small.png") ;
FONT_PLAIN = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt);
// g.drawText(visibleText, m_TextMarginRight, m_MarginTop,
// DrawStyle.ELLIPSIS, 155);

}

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();
}

}
Offline  
Old 11-11-2010, 09:14 PM   #3 (permalink)
New Member
 
Join Date: Oct 2010
Model: 9700
PIN: N/A
Carrier: DTAC
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: How to customize the ObjectChoiceField

Hi perumal_n ,
That is a really useful code.

Thank you very much.
Regards
Offline  
Old 11-11-2010, 09:25 PM   #4 (permalink)
New Member
 
Join Date: Oct 2010
Model: 9700
PIN: N/A
Carrier: DTAC
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: How to customize the ObjectChoiceField

Hi perumal_n ,
I just use this code an I got a few problems.
First the String Label is disappeared and kind of lag (in simulator).
Btw I will try to configure out .

Thanks ,
Regards

Last edited by jutikorn : 11-11-2010 at 10:18 PM.
Offline  




Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.