Blackberry Listfield highlight color - blackberry

what is the best way to change the highlight (focus) color of a blackberry ListField, I have used the drawFocus method it does highlight thing but it's performance too slow to go on with
Code in drawlistrow
Item item = (Item)this.listData.elementAt(this.getSelectedIndex());
g.drawText (item.getItemNumber(), 2, y, Graphics.LEFT,20);
g.drawText (item.getDescription(), 25, y, Graphics.LEFT,30);
g.drawText (item.getItemType(), 60, y, Graphics.LEFT,15);
g.setColor(0xC4C3C4);
g.drawLine(2, y, 2, 115);

You can set the list field highlight colour using the following code
if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) {
//change focus color
g.setBackgroundColor(MyColors.LIGHT_GRAY);
g.clear();
//draw text
g.setFont(boldTextFont);
g.setColor(MyColors.White);
g.drawText(text, 12, y);
}
Do add your drawlistrow code so we can help you improve the performance.

create one boolean for this screen. boolean _inFocus = false; . Than add two method (onfocus, onunfocus)in your Listfield constructor .protected void onFocus(int direction)
{
_inFocus = true;
super.onFocus(direction);
}
protected void onUnfocus()
{
_inFocus = false;
super.onUnfocus();
} .
Than write down the below method in your drawListRow method . In the below method we will check that row is focusable or not.
if(_inFocus)
{
if(listField.getSelectedIndex() == index)
{
g.setGlobalAlpha(100);
g.setColor(0xff9600);
g.fillRect(0,y,getWidth(),getHeight());
//invalidate();
}
}
This is the simple way to highlight row in Listfield .
i hope it will help you.

Related

Blackberry Remove Highlighted Blue Focus

I have a custom ImageButton in which I have placed a png and the hover Image which is transparent from some parts. So when It gets focused it also takes it self with a blue focus. I want to remove that focus blue color but at the same time I want my hoverImage to work!
This is my ImageButton Class: http://codepad.org/mjtIUKLR
A solution that works for me is to add this functionality to the paint method and track whether focus has been gained:
boolean hasFocus = false;
public void onFocus(int direction) {
invalidate();
hasFocus = true;
}
public void onUnfocus() {
hasFocus = false;
invalidate();
super.onUnfocus();
}
And then use this in your paint method:
public void paint(Graphics graphics) {
if (hasFocus){
graphics.drawShadedFilledPath(xPositions, yPositions, null,
HIGHLIGHTED_GRADIENT, null);
}else{
graphics.drawShadedFilledPath(xPositions, yPositions, null,
GRADIENT, null);
}
super.paint(graphics);
}
In my example above, I am using a custom gradient for highlighting that overrides the default blue colour. In your case you will obviously want to change your image or something.

How to make bitmapfield focusable and clickable

I am doing a blackberry, in my application I have to make a bitmapfield clickable and focusable .This is my samplecode.
bf=new BitmapField(logo,BitmapField.FOCUSABLE){
protected boolean navigationClick(int status, int time) {
Dialog.inform("haaaaaaaaaaaai");
return true;
};
protected void layout(int width, int height) {
// TODO Auto-generated method stub
super.layout(width, height);
setExtent(120, 110);
}
};
And I added this field to a verticalfieldmanager. Now the problem is if I click any where in the screen the action is happening and it is not showing any sign of focus.Please help me friends.
BitmapField caontains Bitmap, so blue focusable rectangle covered by bitmap. If you want to show the BitmapField focusable then you need to:
use low opacity image.
OR
use image with 2 or more pixel transparent padding around the image.
for the second issue use can add NullField before the BitmapField because by default first field have focus in screen. And in your case only one field available on screen.
Check at Advanced UI. And look for BitmapButton. :)
For making the bitmap field focusable and clickable yue just use this code:-
Bitmap myBitmap = Bitmap.getBitmapResource("image.png");
BitmapField icImg = new BitmapField(myBitmap, BitmapField.FOCUSABLE)
{
protected boolean navigationClick(int status, int time)
{
UiApplication.getUiApplication().pushScreen( new SCREENCLASS());
return true;
}
};
add(icImg)

How to change row color background in a ListField after navigation Click?

I'm beginner in BB dev. I create a CustomListField and when I click on a row, the background color of this row must change and a new screen must be displayed (it's done!).
Could any one please help me to get this done?
thx
Below is the code:
protected boolean navigationClick(int status, int time)
{Field field = this.getLeafFieldWithFocus();
if(field instanceof ListField)
{
// listValues is String[] where you store your list elements.
// listField is the ListField instance you are using
UiApplication.getUiApplication().pushScreen(new ReadMsgScreen());
int index= getIndex();
if(index== this.getSelectedIndex())
{
**// I think the code to change the row's background color must be set here!**
}
return true;
}
return super.navigationClick(status, time);
}
use this it will definately work...
int tmpcolor = graphics.getColor();
graphics.setColor(Color.CYAN);
graphics.fillRect(0, y, width, getRowHeight());
graphics.setColor(tmpcolor);
thanks...
onerride the paint() method in blackberry as showed the code below..
_specialNumbers = new LabelField(Constants.SPECIAL_NUMBERS,LabelField.USE_ALL_WIDTH) {
protected void paintBackground(Graphics arg0) {
int color = arg0.getBackgroundColor();
arg0.setBackgroundColor(Color.LIGHTGREY);
arg0.clear();
arg0.setBackgroundColor(color);
}
};
In the drawListRow() method of the ListFieldCallback for your CustomListField draw that selected line differently and call a Transition to display the other Screen slowly.

not display cursor in custom BasicEditField bb

please check it
BasicEditField demo = ew BasicEditField("", number, 15,
BasicEditField.FILTER_NUMERIC
| BasicEditField.FIELD_LEFT) {
public int getPreferredWidth() {
int Width = Graphics.getScreenWidth() - 180;
return Width;
}
public int getPreferredHeight() {
return 30;
}
public void paint(Graphics g) {
g.setColor(Color.LINEN);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLUE);
g.drawText(getText(), 0, 0);
super.paint(g);
}
protected void layout(int arg0, int arg1) {
super.layout(getPreferredWidth(), getPreferredHeight());
super.setExtent(getPreferredWidth(), getPreferredHeight());
}
};
this is my code help me out?
Kalpana, I checked your code. Yes, It is not showing cursor. I suggest you to use EditField instead of BasicEditField. You can override these methods for Editfield also. I tried it and it is showing cursor.
I think I may have solved this by adding another field to the manager before this custom BasicEditField. Add a field that doesn't do anything. Something like this:
BitmapField bugFix = new BitmapField(Bitmap.getBitmapResource("empty_image.png"));
myFieldManager.add(bugFix);
myFieldManager.add(demo);
However, what I found is that the size of the dummy field (BitmapField in this case) matters. If your "empty_image.png" image is only 2px high, only the top 2px of the cursor will display. So, if you can deal with the extra padding, add a field that has at least 20px or so and the entire cursor should display. I should also add that this bug does not seem to show up on any subsequent custom BasicEditFields that you add to the manager... only the first one. Hmmm...

How to customize listfield items?

I am new to Blackberry development.My application contain five list items having strings each item.My requirement is when one cell is selected it should enlarge and other items adjust space automatically among them.
I took two types of images for list items one for general view and one for displaying the selected item when user clicked on it which is of more height than previous.
But the problem is when I selected one item it is enlarging and overlapped on the next item and is not displaying full item and not adjusting the space among them.
can anyone give me the solution for this. I am using the following code snippet....
_listField.setCallback(this);
_listField.setRowHeight(45);
_listField.setChangeListener(this);
public void drawListRow(ListField arg0, Graphics graphics, int index,
int y, int width) {
if (listFieldIndex != index) {
graphics.drawBitmap(0, y, listBkgrnd.getWidth(), listBkgrnd
.getHeight(), listBkgrnd, 0, 0);
graphics.setColor(0xAA0000);
graphics.setFont(this.getFont().derive(Font.BOLD, 17));
graphics.drawText(((Station) _stationList.elementAt(index))
.getStationName(), 15, y + 15, 0,
(listBkgrnd.getWidth() - 70));
graphics.setColor(0x770000);
graphics.setFont(this.getFont().derive(Font.PLAIN, 13));
// graphics.drawText(
// ((Station) _stationList.elementAt(index)).getCT(), 15,
// y + 40, 0, (listBkgrnd.getWidth() - 65));
} else {
graphics.drawBitmap(0, y, playBkgrnd.getWidth(), playBkgrnd
.getHeight(), playBkgrnd, 0, 0);
graphics.setColor(0xAA0000);
graphics.setFont(this.getFont().derive(Font.BOLD, 17));
graphics.drawText(((Station) _stationList.elementAt(index))
.getStationName(), 15, y + 15, 0,
(playBkgrnd.getWidth() - 70));
graphics.setColor(0x770000);
graphics.setFont(this.getFont().derive(Font.PLAIN, 13));
graphics.drawText(
s.getCT(), 15,
y + 40, 0, (playBkgrnd.getWidth() - 65));
}
}
List fields on BlackBerry assume that all rows are the same height.
To get the kind of effect you want, use a bunch of selectable fields in a VerticalFieldManager, you'll get a similar effect to a ListField. Be sure to make your items focusable, and call updateLayout whenever they gain or lose focus, then in your layout method, set the field's height according to whether or not it's focused. Something like the following outline:
public class CustomListFieldItem extends Field {
public boolean isFocusable() {
return true;
}
protected void onFocus(int direction) {
super.onFocus(direction);
updateLayout();
}
protected void onUnfocus() {
super.onUnfocus();
updateLayout();
}
protected void layout(int width, int height) {
if (isFocus()) {
setExtent(width, playBkgrnd.getHeight());
}
else {
setExtent(width, listBkgrnd.getHeight());
}
}
protected void paint(Graphics graphics) {
if (isFocus()) {
// draw selected item
}
else {
// draw unselected item
}
}
}

Resources