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.

inside custom component

Java Development

Reply
Developer
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

ObjectListField proper visuals

Hello and good day everybody,

Please i am encountering an issue with my customlistfield class , it is perfectly ok but my problem is this i want the background to be orange red and i want the font to be scalable for all blackberry devices , i simply mean that once i deploy the font size should look ok for all the blackberry smartphones such as torch , bold e.t.c , thanks .

here is my custom class 

 

package coremobile.ui;

import java.util.Vector;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.ObjectListField;
import coremobile.model.Transaction;


public class TransactionBoxListField extends ObjectListField
{
private Vector _listData = null;
Font font = null;

// vector for holding your objects that will be rendered
public TransactionBoxListField(Vector vector)
{
this._listData = vector ;
font = this.getFont().derive(Font.PLAIN,13);
//font = this.getFont().derive(Font.PLAIN, 12);
this.setFont(font);
this.setRowHeight(-2);
this.setSize(_listData.size());// I think this is an important point for it to work: set the height of the row outside the draw method.
this.setCallback(new TransactionCallback(_listData));

}
public boolean[] getSelected(){
int elementLength = _listData.size();
boolean[] selected= new boolean[elementLength];
//Populate the ListField & Vector with data.
for(int count = 0; count < elementLength; ++count)
{
//Get the ChecklistData for this row.
Transaction data = (Transaction)_listData.elementAt(count);
selected[count]= data.isChecked();
}
return selected;
}

//return the focused comment
public String getFocusedElement() {
int selectedIndex = getSelectedIndex();
if (selectedIndex != -1) {
Transaction data = (Transaction) _listData.elementAt(selectedIndex);
return data.toString();
} else {
return null;
}
}


//Allow the space bar to toggle the status of the selected row.
protected boolean keyChar(char key, int status, int time)
{
//If the spacebar was pressed...
if (key == Characters.SPACE || key == Characters.ENTER)
{
defaultItemAction();
return true;
}
return false;
}

/**
* Method to draw the focus on the selected item in the list field
* it is set to light green for this application
*/
protected void drawFocus(Graphics graphics, boolean on) {

XYRect rect = new XYRect();

//graphics.setGlobalAlpha(210);

graphics.setColor(Color.LIGHTGREEN);

getFocusRect(rect);

drawHighlightRegion(graphics,HIGHLIGHT_FOCUS,true,rect.x,rect.y,rect.width,rect.height);


} //remove the standard focus highlight

protected boolean touchEvent(TouchEvent message) {
//Log.trace(">>> touchEvent");

if(!this.getContentRect().contains(message.getX(1), message.getY(1)))
{
return false;
}

//DOWN, UP, CLICK, UNCLICK, MOVE, and CANCEL. An additional event, GESTURE
int eventCode = message.getEvent();
if(eventCode == TouchEvent.CLICK) {

defaultItemAction();
return true;
}else if(eventCode == TouchEvent.DOWN) {
//Log.trace("TouchEvent.CLICK");
} else if(eventCode == TouchEvent.UP) {
//Log.trace("TouchEvent.UP");
} else if(eventCode == TouchEvent.UNCLICK) {
//Log.trace("TouchEvent.UNCLICK");
//return true; //consume the event: avoid context menu!!
} else if(eventCode == TouchEvent.CANCEL) {
//Log.trace("TouchEvent.CANCEL");
}

return false;
//return super.touchEvent(message);
}





/**
* Overrides default implementation. Performs default action if the
* 4ways trackpad was clicked; otherwise, the default action occurs.
*
* @see net.rim.device.api.ui.Screen#navigationClick(int,int)
*/
protected boolean navigationClick(int status, int time)
{


if ((status & KeypadListener.STATUS_TRACKWHEEL) == KeypadListener.STATUS_TRACKWHEEL) {

// Input came from the trackwheel
defaultItemAction();
return super.navigationClick(status, time);

} else if ((status & KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY) {
//Log.trace("Input came from a four way navigation input device");
defaultItemAction();
return true;
}
return super.navigationClick(status, time);
}

private void defaultItemAction() {
//Get the index of the selected row.
int index = getSelectedIndex();

if(index == -1) return;

//Get the Transaction for this row.
Transaction data = (Transaction)_listData.elementAt(index);

//Toggle its status.
data.toggleChecked();

//Update the Vector with the new ChecklistData.
_listData.setElementAt(data, index);

//Invalidate the modified row of the ListField.
invalidate(index);

//set the list state as dirty
this.setDirty(true);
}









/**
*
* refresh the list and display the vector
*/
public void refresh ()
{

this.getManager().invalidate();

}

/**
*
* inner class to hold information about the callback for this listfield
* @author Iyenemi Tyger
*
*/
private class TransactionCallback implements ListFieldCallback
{
final protected Bitmap checkedBitmap = Bitmap.getBitmapResource("checked1.png");
final protected Bitmap uncheckedBitmap = Bitmap.getBitmapResource("uncheck3.png");

Vector vector = null;

public TransactionCallback(Vector vectors)
{
this.vector = vectors;
}

/**
*
* drawListRow inherited from the listfieldcallback class
*/
public void drawListRow(ListField listField, Graphics graphics,
int index, int y, int width)
{
//Get the ChecklistData for the current row.

Transaction currentRow = (Transaction)this.get(listField, index);


//int originalColor = graphics.getColor();
graphics.setGlobalAlpha(220);

graphics.setColor(Color.DARKGRAY);

//If it is checked draw the String prefixed with a checked box,
//prefix an unchecked box if it is not.
if (currentRow.isChecked()) {
graphics.drawBitmap(0, y, checkedBitmap.getWidth(), checkedBitmap.getHeight(), checkedBitmap, 0, 0);
} else {
graphics.drawBitmap(0, y, checkedBitmap.getWidth(), checkedBitmap.getHeight(), uncheckedBitmap, 0, 0);
}

// start drawing the text from the width of the image icon(25)
graphics.drawText(currentRow.toString(), checkedBitmap.getWidth(), y, 0, width - checkedBitmap.getWidth());


}

//Returns the object at the specified index.
public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return _listData.elementAt(index);
}

public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return Display.getWidth();
}

public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return _listData.indexOf(prefix, start);
}

}

}

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: ObjectListField proper visuals

1) The easiest way to create a Font that is the 'same' on all devices is to specify it in points, and not pixels, for example. 

font = this.getFont().derive(Font.PLAIN,8, UI.UNITS_pt);

 

2) If you want to paint the background of the row, then you should set the color and then use fillRect in your

drawListRow(ListField listField, Graphics graphics, int index, int y, int width)

 

The rectangle you need to fill is given by:

0, y, width, listField.getRowHeight()