custom field focus in blackberry - blackberry

I created one customUi class in that im creating 3 fiels as 1 label and two button fileds....
so i m creating the customui and passing the filed names...
like
public class CustomTextField extends Manager {
Field field ;
//passing 3 names to the construcor
protected void sublayout(int width, int height) {
// TODO Auto-generated method stub
int fieldCount = getFieldCount();
int screenWidth = Display.getWidth();
int yOffset =0;
for(int i=0; i<fieldCount; i++)
{
field= getField(i);
layoutChild(field, screenWidth, 100 );
setPositionChild(field, (screenWidth-field.getWidth())>>1, yOffset);
yOffset += field.getHeight()+5;
}
setExtent(screenWidth, yOffset);
}
protected void paintBackground(Graphics g) {
if(field.isFocus())
{
//g.setBackgroundColor(Color.ALICEBLUE);
int oldColor = g.getColor();
g.setColor(Color.DARKBLUE);
g.drawRoundRect(0, 0, getWidth(), getHeight(), Graphics.ALL_ROUNDED_RECT_CORNERS, 60, 60);
g.setColor(oldColor);
}
}

Don't override paintBackground(Graphics g) inside of the manager. Override it inside of the child field class. It's responsibility of a child field how to present its focused state.

Related

labelField.getWidth() returns 0? Why does this happen?

I'm working on my own custom manager, and I've gotten it complete so far, but it setsMargins using a percentage of the screen resolution.
Here's how I call the following class:
LabelIconCommandManager licm3 = new LabelIconCommandManager("Address blah bklahblah ", 0);
licm3.add(new ImageButtonField(b1, b2, b3, Field.FIELD_LEFT | ImageButtonField.CONSUME_CLICK));
Here's the class [I've marked in a comment where it returns 0 and where it returns 219. please tell me why this happens:
public class LabelIconCommandManager extends HorizontalFieldManager implements BCMSField
{
LabelIconCommandManager me = this;
EvenlySpacedHorizontalFieldManager buttonManager = new EvenlySpacedHorizontalFieldManager(0);
LabelField labelField;
int side = 0;
int HPADDING = 3;
int VPADDING = 4;
int screenWidth = Display.getWidth();
int labelField_width = 40;
public LabelIconCommandManager()
{
this("", 0);
}
public LabelIconCommandManager(String label, long style)
{
super(USE_ALL_WIDTH| FOCUSABLE);
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit.png" )));
this.setMargin(1,10,1,10);
labelField = new LabelField(label,LabelField.ELLIPSIS)
{
public void layout(int width, int height)
{
// Done because otherwise ellipses dont work with labelfields
super.layout((int)(screenWidth * 0.61), getHeight());
setExtent((int)(screenWidth * 0.61), getHeight());
labelField_width = labelField.getWidth();
DisplayDialog.alert("labelField_width = " + labelField_width); // returns 219
}
};
// Top Right Bottom Left
labelField.setMargin(VPADDING, HPADDING, VPADDING, 0);
// super because we want this horizontalfieldManager to add it
super.add(labelField);
super.add(buttonManager);
}
public void alternateConstructor(Attributes atts)
{
labelField = new LabelField(atts.getValue("label"), 0);
}
public void onFocus(int direction)
{
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit_select.png" )));
// uses the same color as listStyleButtonField selections
this.setBackground(BackgroundFactory.createSolidBackground(0x186DEF));
super.onFocus(direction);
}
//Invoked when a field loses the focus.
public void onUnfocus()
{
//top, right,bottom,left
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit.png" )));
this.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.GRAY, 0));
super.onUnfocus();
invalidate();
}
// Overrride this managers add function
public void add(Field imageButton)
{
// Add a button to the evenly spaced manager
buttonManager.add(imageButton);
// Based on how many buttons there are, set the margin of where the manager holding the buttons start [offset from labelField]
if(buttonManager.getFieldCount() == 1)
{
//side = (int)(screenWidth * 0.1388);
side = screenWidth - labelField_width - 32 - 10 - 15;
DisplayDialog.alert("Screen Width = " + screenWidth);
DisplayDialog.alert("labelField_width2 = " + labelField_width); // returns 0
DisplayDialog.alert("Side = " + side);
}
else side = (int)(screenWidth * 0.05);
buttonManager.setMargin(0,0,0,side);
}
public int getLabelWidth()
{
return labelField_width;
}
}
Here's a picture just to be more clear:
Note: when I ran your code, I didn't actually see labelField_width set to 0. You initialize the value to 40 in the code you posted above. So, I do sometimes see it set to 40, or 219 (on a 360 px wide screen).
But, the problem is that I think you're trying to access the value of labelField_width too soon. The only place it's properly assigned is in the layout() method of your anonymous LabelField. Just because you declare and implement the layout() method in line with the instantiation, doesn't mean that it's called when the LabelField is created. This is actually one of the reasons I don't like anonymous classes.
Anyway, this code:
LabelIconCommandManager licm3 = new LabelIconCommandManager("Address blah bklahblah ", 0);
licm3.add(new ImageButtonField(b1, b2, b3, Field.FIELD_LEFT | ImageButtonField.CONSUME_CLICK));
Will first instantiate the LabelField (inside the LabelIconCommandManager constructor). As I said, that does not trigger the layout() method. The second line above (add()) will trigger your overridden method:
// Overrride this managers add function
public void add(Field imageButton)
{
which is where you see the bad value for labelField_width. That method gets called before layout(). That's the problem.
Since it looks like you only use that width to set the buttonManager margin, you could just wait a little longer to do that. If you wait until the LabelIconCommandManager sublayout() method is called, your LabelField will have had its layout() method called, and labelField_width assigned correctly:
protected void sublayout(int maxWidth, int maxHeight) {
// make sure to call superclass method first!
super.sublayout(maxWidth, maxHeight);
// now, we can reliably use the label width:
side = screenWidth - labelField_width - 32 - 10 - 15;
buttonManager.setMargin(0,0,0,side);
}
That method goes in the LabelIconCommandManager class. And then, you can remove the other place you call buttonManager.setMargin().
Some brief summary from Nate post.
When you construct manager and add fields don't expect that it will be layouted correctly. Manager doesn't know the context - where it will be placed. So layout method for field will be called only when you add his manager to the screen (when layout for manager will be also called). And this is correct.
Move the calculation of your side variable to layout method.
If you really need side value before you put manager to screen. You could precalculate it by using Field.getPrefferedWidth() which returns meaningful values for standard fields (getFont().getAdvance(text) for LabelField, probably also with borders please check yourself). But be careful with this values.
Please review code below. It's manager which has label and buttons. And it puts label at the left side and buttons at the right.
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.decor.Border;
import java.util.Vector;
public class TabFieldManager extends Manager {
public TabFieldManager(long style) {
super(style);
}
protected void sublayout(int width, int height) {
LabelField label = null;
Vector tabs = new Vector();
int tabsWidth = 0;
int tabHeight = 0;
int tabPaddingTop = 0;
int tabPaddingLeft = 0;
for (int i=0; i < getFieldCount(); i++) {
Field field = getField(i);
if (field instanceof LabelField) {
label = (LabelField) field;
} else if (field instanceof ButtonField){
tabs.addElement(field);
layoutChild(field, width, height);
int fieldwidth = field.getWidth() > 0 ? field.getWidth() : field.getPreferredWidth() ;
tabsWidth += fieldwidth + getBorderAndPaddingWidth(field);
int fieldHeight = field.getHeight() > 0 ? field.getHeight() : field.getPreferredHeight();
if (fieldHeight > tabHeight) {
tabHeight = getBorderAndPaddingHeight(field) + fieldHeight;
}
int fieldPaddingTop = field.getPaddingTop();
if (fieldPaddingTop > tabPaddingTop) {
tabPaddingTop = fieldPaddingTop;
}
int fieldPaddingLeft = field.getPaddingLeft();
if (fieldPaddingLeft > tabPaddingLeft) {
tabPaddingLeft = fieldPaddingLeft;
}
}
}
if (label != null) {
layoutChild(label, width - tabsWidth, height);
int y = tabHeight - label.getHeight() >> 1;
setPositionChild(label, tabPaddingLeft , y);
}
for (int i = 0; i < tabs.size(); i++) {
Field tabField = (Field) tabs.elementAt(i);
setPositionChild(tabField, width - tabsWidth, getBorderAndPaddingHeight(tabField));
tabsWidth -= tabField.getWidth() + getBorderAndPaddingWidth(tabField);
}
setExtent(width, tabHeight);
}
private int getBorderAndPaddingHeight( Field field ) {
int height = field.getPaddingTop() + field.getPaddingBottom();
Border border = field.getBorder();
if( border != null ) {
height += border.getTop() + border.getBottom();
}
return height;
}
private int getBorderAndPaddingWidth( Field field ){
int width = field.getPaddingLeft() + field.getPaddingRight();
Border border = field.getBorder();
if( border != null ) {
width += border.getLeft() + border.getRight();
}
return width;
}
protected int moveFocus(int amount, int status, int time) {
if ((status & Field.STATUS_MOVE_FOCUS_VERTICALLY) == Field.STATUS_MOVE_FOCUS_VERTICALLY && amount > 0) {
return amount;
} else
return super.moveFocus(amount, status, time);
}
protected int nextFocus(int amount, int axis) {
if (amount > 0 && axis == Field.AXIS_VERTICAL)
return -1;
else
return super.nextFocus(amount, axis);
}
}

Checkbox in CustomListField in Blackberry

I am creating a CustomList. In each row I am having a image, some text fields and a checkbox. But i am not able to check and uncheck the checkbox.
I have gone through this link but cant solve my problem.
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/How_To_-_Create_a_ListField_with_check_boxes.html?nodeid=1165752&vernum=0
this will add a menu item in the menu to change the checkbox status.
I want the normal checkbox behaviour.
Please help
this is my code
class CustomList extends ListField implements ListFieldCallback
{
public Vector rows;
private Bitmap p1;
int z = this.getRowHeight();
LabelField task,task2,task3;
CheckboxField checkbox[]= new CheckboxField[40];
int rowcount;
public CustomList(int rowcount,String text1,double text2,String type,Bitmap p)
{
super(0, ListField.MULTI_SELECT);
this.rowcount= rowcount;
setRowHeight(z*3);
setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
setCallback(this);
p1 = p;
rows = new Vector();
for (int x = 0; x < rowcount; x++)
{
checkbox[x]=new CheckboxField("",false,Field.USE_ALL_HEIGHT|Field.EDITABLE|Field.FIELD_VCENTER|Field.FOCUSABLE);
}
for (int x = 0; x < rowcount; x++)
{
TableRowManager row = new TableRowManager();
row.add(checkbox[x]);
row.add(new BitmapField(p1));
task= new LabelField("abc");
row.add(task);
task2=new LabelField("abc2");
row.add(task2);
task3= new LabelField("abc3");
row.add(task3);
rows.addElement(row);
}
setSize(rows.size());
}
public void drawListRow(ListField listField, Graphics g, int index, int y,int width)
{
CustomList list = (CustomList) listField;
TableRowManager rowManager = (TableRowManager) list.rows.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
private class TableRowManager extends Manager
{
public TableRowManager()
{
super(0);
}
// Causes the fields within this row manager to be layed out then
// painted.
public void drawRow(Graphics g, int x, int y, int width, int height)
{
layout(width, height);
setPosition(x, y);
g.pushRegion(getExtent());
subpaint(g);
g.setColor(0x00CACACA);
g.popContext();
}
protected void sublayout(int width, int height)
{
// set the size and position of each field.
}
public int getPreferredWidth()
{
return UIConstants.SCREEN_WIDTH;
}
public int getPreferredHeight()
{
return getRowHeight();
}
}
}
I am trying to implement listfield with checkboxes and i have seen its code in the knowledge base.
But i want that when i click on the checkbox it should be checked or unchecked onlyand when i click on the rest part of the row, i.e. anywhere except the checkbox then nothing should occur without checking the checkbox
How can i do that ?? Or i should do something else to do it.. Please suggest..
Do some thing like this where you are overriding your sublayOut method() in table row manager class
protected void sublayout(int width, int height)
{
Field field = getField(0);
layoutChild(field, Display.getWidth(), Display.getHeight());
setPositionChild(field, 10, 20);
Field field = getField(0);
layoutChild(field, Display.getWidth(), Display.getHeight());
setPositionChild(field, 10, 20);
//Set field
field = getField(1);
layoutChild(field, 120, fontHeight - 1 );
setPositionChild(field, 70, 05);
// set the list field
field = getField(2);
layoutChild(field, 150, fontHeight +10);
setPositionChild(field, 70, 25);
// set the field
field = getField(3);
layoutChild(field, (Display.getWidth()/6)-12, fontHeight + 10);
setPositionChild(field, 70, 45);
setExtent(preferredWidth, getPreferredHeight());
}
change the padding as your require ments.
And you can also check this link create a listField with check box. Thanks
boolean checked = false;
Vector box1 = new Vector();
for(int i=0;i<vector.size();i++){
FriendsRequestObject co_vec = (FriendsRequestObject)vector.elementAt(i);
String name=co_vec.getSender_name();
CheckboxField box = new CheckboxField(" "+name , checked, Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
box1.addElement(box);
box.setMargin(8, 0, 0, 5);
add(box);
}
then add a button. On the click listener of the button,
for(int d=0; d<box1.size(); d++){
CheckboxField box=(CheckboxField)box1.elementAt(d);
if(box.getChecked()==true){
FriendsRequestObject co_vec = (FriendsRequestObject)vector.elementAt(d);
String name_ = co_vec.getSender_name();
Dialog.alert(name_);// here you can implement your code
}
}

BlackBerry Torch scrolling issue

I have created an app which is working fine on BlackBerry bold but when I installed it on Torch the screen is not scrolling..
I have used Manager with enabled vertical scrolling and added it to another master vertical field manager?
Did any one get this issue before ??
the code for the manager is below
public class TableManager extends Manager {
public int HEIGHT = 0;
public TableManager(int height) {
super(Manager.USE_ALL_WIDTH|Manager.VERTICAL_SCROLL);
HEIGHT = height;
}
public int getPreferredWidth() {
return Constants.width * 90 / 100;
}
public int getPreferredHeight() {
return HEIGHT;
}
protected void sublayout(int maxWidth, int maxHeight) {
int y = 150;
try{
int count = getFieldCount();
for (int i = 0; i < count; i++) {
Field field = getField(i);
layoutChild(field, field.getPreferredWidth(), field
.getPreferredHeight());
setPositionChild(field, (Constants.width - field
.getPreferredWidth()) >> 1, y);
y += field.getPreferredHeight();
}
setExtent(maxWidth, HEIGHT);
}catch(Exception e){
e.printStackTrace();
}
}
}
Try to add your TableManager to a master manager that is not vertically Scrollable.
You may also use a simple VerticalFieldManager with any Field inside set to FIELD_HCENTER instead of using this TableManager.

it displays only text,but not the image

i developed the code as below.in this i used listfield ,one bitmapfield and one label field,when i will run it ,it displays only text on the list field row,but not the image
i don't know where i did mistake,so,plz,any one help me to know where i did mistake
thanks for any help
class TaskListField extends MainScreen implements ListFieldCallback {
private Vector rows;
private Bitmap p1;
ListField list;
TableRowManager row;
public TaskListField() {
super();
list=new ListField() {
protected void drawFocus(Graphics graphics, boolean on) {
}
};
list.setRowHeight(40);
list.setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
list.setCallback(this);
p1 = Bitmap.getBitmapResource("res/images/10.png");
rows = new Vector();
for (int x = 1; x < 13; x++) {
row = new TableRowManager();
LabelField task = new LabelField("" + String.valueOf(x),
DrawStyle.ELLIPSIS);
row.add(task);
row.add(new BitmapField(p1));
rows.addElement(row);
}
list.setSize(rows.size());
add(list);
}
// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y,
int width) {
TableRowManager rowManager = (TableRowManager) rows
.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
private class TableRowManager extends Manager {
public TableRowManager() {
super(0);
}
public void drawRow(Graphics g, int x, int y, int width, int height) {
layout(width, height);
setPosition(x, y);
g.pushRegion(getExtent());
// Paint this manager's controlled fields.
subpaint(g);
g.setColor(0x00CACACA);
g.drawLine(0, 0, getPreferredWidth(), 0);
// Restore the graphics context.
g.popContext();
}
protected void sublayout(int width, int height) {
int preferredWidth = getPreferredWidth();
Field field = getField(0);
layoutChild(field, 30, 30);
setPositionChild(field, 0, 0);
field = getField(1);
layoutChild(field, 40, 25);
setPositionChild(field, 120, 10);
setExtent(preferredWidth, getPreferredHeight());
}
// The preferred width of a row is defined by the list renderer.
public int getPreferredWidth() {
return Graphics.getScreenWidth();
}
// The preferred height of a row is the "row height" as defined in the
// enclosing list.
public int getPreferredHeight() {
return list.getRowHeight();
}
}
public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return null;
}
public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
Are you sure the bitmap is not null? I would check that first -- perhaps it's not finding the resource.
Could the LabelField be taking the entire width of the screen (it does that on certain situations)? When you only set the image without setting the label, does the image show?
The image path is not necessary as you put it
p1 = Bitmap.getBitmapResource (" res/images/10.png ");
only needs
p1 = Bitmap.getBitmapResource (" 10.png ");

How to control manager class in Blackberry

Dear All, I have a problem when creating a UI on Blackberry.
First, i try to create a ChatLayoutManager class extended from Manager class. My layout has three component: topfield, mainfield and bottom field.
public class ChatLayoutManager extends Manager {
private Field bottomField;
private Field mainField;
private Field titleField;
public ChatLayoutManager(long style) {
super(style);
}
protected void sublayout(int width, int height) {
setExtent(width, height);
int y = 0;
if (bottomField != null) {
layoutChild(bottomField, width, height);
// This goes at the bottom of the screen
setPositionChild(bottomField, 0, height-bottomField.getHeight());
height -= bottomField.getHeight();
}
if (titleField != null) {
layoutChild(titleField, width, height);
// This goes at the top of the screen
setPositionChild(titleField, 0, 0);
height -= titleField.getHeight();
y += titleField.getHeight();
}
if (mainField != null) {
layoutChild(mainField, width, height);
// This goes just below the title field (if any)
setPositionChild(mainField, 0, y);
}
}
public void setMainField(Field f) {
mainField = f;
add(f);
}
public void setBottomField(Field f) {
bottomField = f;
add(f);
}
public void setTitleField(Field f) {
titleField = f;
add(f);
}
Then i create another field (ChatField) extended from manager to add to mainfield in the ChatLayoutManager class which i have created above.
public class ChatField extends Manager{
private Field _contentField[];
protected ChatField(){
super(Manager.HORIZONTAL_SCROLL | Manager.VERTICAL_SCROLL);
}
// TODO Auto-generated constructor stub}
protected synchronized void sublayout(int width, int height) {
// TODO Auto-generated method stub
setExtent(width, height);
int x = 0;
int y = 0;
if(_contentField.length > 0){
for(int i = 0 ;i<_contentField.length; i++){
//if(getManager() == this){
this.layoutChild(_contentField[i],
_contentField[i].getWidth(),
_contentField[i].getHeight());
this.setPositionChild(_contentField[i], x, y);
if(_contentField[i++]!= null){
if ((_contentField[i].getWidth() + _contentField[i].getWidth())
>= width){
x = 0;
y += _contentField[i].getHeight();
}
else{
x += _contentField[i].getWidth();
}
}
//}
}
}
}
public void setContentField(Field field[]){
_contentField = field;
}
}
And now, when i create some fields(such as TextField, BitmapField ...) added to ChatField, the program has an error "Field is not a child of this manager". The reason is when the framework invokes the sublayout function of the ChatField class , when sublayout starts calling layoutChild function the manager of field is not ChatField but ChatlayoutManager.
I've experience hard time trying to resolve this problem, still I have no solution. Anybody can give me some suggestions? I really appreciate.
When you call Manager.add() (passing in a Field) that field becomes a child of that manager. A field can only be a child of a single Manager - it cannot belong to multiple Managers. If you're getting that error, then you may be accidentally adding it to more than one Manager.
Your setContentField(Field[]) function gets a reference to the field array provided, but it never adds the content of the array to the manager (ChatField). This is why you get the error that the field is not a child of this manager.
Secondly, with respect to your ChatField#sublayout, you should do one of the following:
1) Instead of referencing the _contentField array directly, use functionality provided by Manager to reference its children:
int numFields = getFieldCount(); // getFieldCount() is a member of Manager
int marginHorizontal = 0;
for(int i = 0; i < numFields; i++) {
field = getField(i); // getField() is a member of Manager
// Whatever you need to do to it.
}
2) If you would still rather reference _contentField directly, or if you need control over a specific field, then make sure that the Field's manager is the manager being laid out:
for(int i = 0 ;i<_contentField.length; i++){
if(_contentField[i] != null && _contentField[i].getManager() == this){
// Whatever you need to do to it.
}
}
Hope this helps.

Resources