I am not able to set the focus on the HorizontalFieldManager having two LabelFields. I want to highlight the HorizontalFieldManager when it gets focus. I am using the following code, but it is not working.
HorizontalFieldManager hrzMgrTimeLabel = new HorizontalFieldManager(
Manager.USE_ALL_HEIGHT|Manager.FOCUSABLE) {
protected void onFocus(int direction) {
focussed = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
focussed = false;
invalidate();
super.onUnfocus();
}
protected void paint(Graphics g) {
g.setBackgroundColor(0x646060);
if (focussed) {
g.setColor(Color.BLUE);
}
g.clear();
super.paint(g);
}
};
hrzMgrTimeLabel.add(a);
hrzMgrTimeLabel.add(b);
horizontalFieldManager_left15.add(hrzMgrTimeLabel);
Here is the LabelField implementation.
final LabelField a= new LabelField("") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.setBackgroundColor(0x646060);
graphics.clear();
super.paint(graphics);
}
};
final LabelField b= new LabelField("") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.setBackgroundColor(0x646060);
graphics.clear();
super.paint(graphics);
}
};
The getFocusRect() function might give you the answer.
It gives the extent for which the focus is applied in Manager and then the Graphics.paint() method or Border class can be used.
You need to at least one focusable field on the HorizontalFieldManager. The following line will do that.
hrzMgrTimeLabel.add(new NullField(NullField.FOCUSABLE));
Check the following code.
HorizontalFieldManager hrzMgrTimeLabel = new HorizontalFieldManager(Manager.USE_ALL_HEIGHT | Manager.FOCUSABLE) {
protected void paint(Graphics g) {
g.setBackgroundColor(isFocus() ? Color.BLUE : 0x646060);
g.clear();
super.paint(g);
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
protected boolean navigationMovement(int dx, int dy, int status, int time) {
invalidate();
return super.navigationMovement(dx, dy, status, time);
}
protected boolean navigationClick(int status, int time) {
invalidate();
return super.navigationClick(status, time);
}
protected boolean navigationUnclick(int status, int time) {
invalidate();
return super.navigationUnclick(status, time);
}
};
final LabelField a = new LabelField("First Label Field") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
final LabelField b = new LabelField("Second Label Field") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
hrzMgrTimeLabel.add(new NullField(NullField.FOCUSABLE));
hrzMgrTimeLabel.add(a);
hrzMgrTimeLabel.add(b);
Note
onFocus(...), onUnfocus(), navigationMovement(...), navigationClick(...), navigationUnclick(...) - these methods were just overrided for refreshing the drawing.
If you want to focus your label field then use final LabelField a= new LabelField("".FOCUSABLE).
Related
I want to hilight the background of a horizontalFieldManager when I'm scrolling. I tried this code:
public class HorizentalFocus extends HorizontalFieldManager{
protected void paint(Graphics g) {
g.setBackgroundColor(isFocus() ? 4210231 : 0xFFFFFF);
g.getFont();
g.clear();
super.paint(g);
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
}
It works but I have two problems: it doesn't hilight the entire horizontalField and I want to change the color of the text when it's focused.
Thanks for your help.
Hi, I had Generate custom layouts in my screen, i want change color or image of horizontal Field manager when it focus and also perform Click event of selected horizontal Field manager. For
More reference see screen shot.
this is my code which i had try..
HorizontalFieldManager hfmrow=new HorizontalFieldManager(USE_ALL_WIDTH|HorizontalFieldManager.FOCUSABLE | Field.FOCUSABLE)
{
public void onFocus(int direction)
{
isFocus = true;
super.onFocus(direction);
invalidate();
}
public void onUnfocus()
{
invalidate();
isFocus = false;
super.onUnfocus();
}
public void paint(Graphics g)
{
if(isFocus)
{
hfmrow.setBackground(BackgroundFactory.createSolidBackground(Color.RED));
invalidate();
super.paint(g);
}
else
{
hfmrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
invalidate();
super.paint(g);
}
}
protected boolean navigationClick(int status, int time)
{
Dialog.alert("You are in hfm"+id);
return true;
}
};
hfmrow.setMargin(0, 10, 0, 10);
hfmrow.add(new NullField(Field.FOCUSABLE));
Any help is appreciate..
Thanx in advance
Here how can you do it this code is for changing color when you are selecting hfm
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public class sample extends MainScreen implements FieldChangeListener{
HorizontalFieldManager logInDetailManager=null;
int background_color=0;
public sample() {
logInDetailManager = new HorizontalFieldManager(Manager.USE_ALL_WIDTH|Field.FOCUSABLE){
protected void sublayout(int maxWidth, int maxHeight) {
int height=40;
super.sublayout(Display.getWidth(), height);
setExtent(Display.getWidth(), height);
}
protected void paint(Graphics graphics) {
graphics.setBackgroundColor(background_color);
graphics.clear();
invalidate();
super.paint(graphics);
}
protected void onFocus(int direction) {
super.onFocus(direction);
background_color=Color.RED;
invalidate();
}
protected void onUnfocus() {
invalidate();
background_color=Color.GREEN;
}
protected boolean navigationClick(int status, int time) {
if(Touchscreen.isSupported()){
return false;
}else{
fieldChangeNotify(1);
return true;
}
}
protected boolean touchEvent(TouchEvent message)
{
if (TouchEvent.CLICK == message.getEvent())
{
FieldChangeListener listener = getChangeListener();
if (null != listener)
this.setFocus();
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
};
logInDetailManager.setChangeListener(this);
logInDetailManager.add(new LabelField("hello"));
logInDetailManager.add(new NullField(Field.FOCUSABLE));
add(logInDetailManager);
add(new LabelField("good",Field.FOCUSABLE));
}
public void fieldChanged(Field field, int context) {
if(field==logInDetailManager){
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Hi how are you?");
}
});
}
}
}
Use onFocus() and onUnfocus to maintain the focus to your HorizontalFieldManager. and use navigationClick() to click on your hfm . but make sure that you havo to set Field.FOCUSABLE in your HorizontalFieldManager.
take one boolean to check the focus status and do the code in paint method. check below code ..
`
boolean _isFocus = false;
HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FOCUSABLE | Field.FOCUSABLE)
{
public void onFocus(int direction)
{
_isFocus = true;
super.onFocus(direction);
}
public void onUnfocus()
{
_isFocus = false;
super.onUnfocus();
}
public void paint(Graphics g)
{
if(_isFocus)
{
// replace your bitmap image or bgcolor here
hfm.invalidate();
super.paint(g);
}
else
{
// replace your bitmap image or bgcolor here
hfm.invalidate();
super.paint(g);
}
}
protected boolean navigationClick(int status, int time)
{
Dialog.alert("You are in hfm");
return true;
}
}`
hope it works !!!
I have defined a BitmapButtonField in Blackberry. But the default grey color background does not go. And the Bluish onFocus border?
How to change them in BB 4.5
Hi this is bitmap button and background color Red
manager=new Field(Field.FOCUSABLE){
protected void onFocus(int direction) {
super.onFocus(direction);
flag1=true;
invalidate();
}
protected void onUnfocus() {
flag1=false;
invalidate();
}
protected void layout(int width, int height) {
setExtent(image.getWidth(),image.getHeight());
}
protected void paint(Graphics graphics) {
if(flag1){
graphics.setBackgroundColor(Color.RED);
graphics.clear();
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
}else{
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
}
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
};
manager.setChangeListener(this);
add(manager);
output as following image
This is background image and with default background color
manager1=new Field(Field.FOCUSABLE){
protected void onFocus(int direction) {
super.onFocus(direction);
flag2=true;
invalidate();
}
protected void onUnfocus() {
button=image;
flag2=false;
invalidate();
}
protected void layout(int width, int height) {
setExtent(image.getWidth(),image.getHeight());
}
protected void paint(Graphics graphics) {
if(flag2)
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
else
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
};
manager1.setChangeListener(this);
add(manager1);
out put as following
Fully functional class is following for better understanding
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
public class StartUp extends UiApplication{
public static void main(String[] args) {
StartUp up=new StartUp();
up.enterEventDispatcher();
}
public StartUp()
{
pushScreen(new Demoscreen());
}
class Demoscreen extends MainScreen implements FieldChangeListener
{
Bitmap image,image_hover;
boolean flag1,flag2;
Field manager=null,manager1=null;
public Demoscreen() {
image=Bitmap.getBitmapResource("Download_48x48.png");
image_hover=Bitmap.getBitmapResource("Download_48x48.png");
manager=new Field(Field.FOCUSABLE){
protected void onFocus(int direction) {
super.onFocus(direction);
flag1=true;
invalidate();
}
protected void onUnfocus() {
flag1=false;
invalidate();
}
protected void layout(int width, int height) {
setExtent(image.getWidth(),image.getHeight());
}
protected void paint(Graphics graphics) {
if(flag1){
graphics.setBackgroundColor(Color.RED);
graphics.clear();
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
}else{
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
}
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
};
manager.setChangeListener(this);
add(manager);
manager1=new Field(Field.FOCUSABLE){
protected void onFocus(int direction) {
super.onFocus(direction);
flag2=true;
invalidate();
}
protected void onUnfocus() {
flag2=false;
invalidate();
}
protected void layout(int width, int height) {
setExtent(image.getWidth(),image.getHeight());
}
protected void paint(Graphics graphics) {
if(flag2)
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image_hover,0,0);
else
graphics.drawBitmap(0, 0, image.getWidth(),image.getHeight(), image,0,0);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
};
manager1.setChangeListener(this);
add(manager1);
}
public void fieldChanged(Field field, int context) {
if(field==manager)
{
displayDialog("I am first field");
}else if(field==manager1)
{
displayDialog("I am second field");
}
}
public void displayDialog(final String message)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform(message);
}
});
}
}
}
For more details please visit following link for BackgroundsBackground logic for 4.5 OS
Check following links for customized Button Field:
Implement advanced buttons, fields, and managers.
User Defined Buttons - Create a Custom Button From an Image
Blackberry Custom Button Field
How to create a Custom Button Field in Blackberry
Normally overriding applyTheme(..) (an empty implementation) removes default styling of a field. Also we need to override paint(..), paintBackground(..), drawFocus(..) and change their implementation according to current focus status of the field. Below is a code snippet for the case when I extend a Field for making Custom Button Field.
private final Bitmap focusedBitMap = Bitmap.getBitmapResource("focused.png");
private final Bitmap unfocusedBitmap = Bitmap.getBitmapResource("unfocused.png");
protected void paint(Graphics graphics) {
// paint background
graphics.setBackgroundColor(isFocus() ? Color.RED : Color.GREEN);
graphics.clear();
// draw button image
Bitmap myBitmap = isFocus() ? focusedBitMap : unfocusedBitmap;
if (myBitmap != null) {
// image position calculation
int imageX = 0;
int imageY = 0;
graphics.drawBitmap(imageX, imageY,
myBitmap.getWidth(), myBitmap.getHeight(),
myBitmap, 0, 0);
}
}
protected void drawFocus(Graphics graphics, boolean on) {
}
public boolean isFocusable() {
return true;
}
How to place bitmapFields in Manager while some buttons on it get focus.I tried following code but it is not working.`
when "button" get focus I need to add "field1" beside the button in Vfm.
button = new ImgButtonField("res/images/trans.png", Field.FOCUSABLE){
protected void onFocus(int direction) {
Vfm.insert(field1, button.getIndex()) ;
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
Vfm.delete(field1);
super.onUnfocus();
invalidate();
}
};
Try using add rather than insert ,sample code i tried
ButtonField button,button1;
LabelField field1;
VerticalFieldManager vfm;
public MyScreen()
{
// Set the displayed title of the screen
setTitle("MyTitle");
vfm=new VerticalFieldManager();
field1=new LabelField("Sample");
button=new ButtonField("Sample"){
protected void onFocus(int direction) {
vfm.add(field1);
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
vfm.delete(field1);
super.onUnfocus();
invalidate();
}
};
button1=new ButtonField("Sampl1");
this.add(button1);
this.add(button);
this.add(vfm);
}
Regards
Rakesh Shankar.P
Try placing a NullField in the manager as a placeholder. The NullField will not be visible. On focus, you can replace that NullField with field1.
NullField myNullField = new NullField();
button = new ImgButtonField("res/images/trans.png", Field.FOCUSABLE){
protected void onFocus(int direction) {
Vfm.replace(myNullField, field1) ;
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
Vfm.replace(field1, myNullField);
super.onUnfocus();
invalidate();
}
};
//add fields to Vfm, including the myNullField placeholder.
I have created a custom button and i am placing the bunch of Custombuttons in a verticalfieldManager , I have aligned the verticalField Manager in the center. when i am creating a default buttonField then verticalfield Manager is able to align the buttonfield in the center. but when i am assigning custombuttonfield in the verticalField Manager it's not aligning in the center.
here is my custombuttoncode
public CustomButtonField(String label,long style) {
super(style);
this.label = label;
onPicture = Bitmap.getBitmapResource(onPicturePath);
font = getFont();
this.setPadding(5,5, 5, 5);
}
public String getLabel() {
return label;
}
public int getPreferredHeight() {
return onPicture.getHeight();
}
public int getPreferredWidth() {
return onPicture.getWidth();
}
protected void layout(int width , int height) {
setExtent(Math.min(width, Display.getWidth()), Math.min(height,getPreferredHeight()));
}
protected void paint(Graphics graphics) {
int texty =(getHeight()-getFont().getHeight())/2;
if (isFocus()) {
graphics.setColor(Color.BLACK);
graphics.drawBitmap(0, 0, getWidth(), getHeight(),onPicture , 0, 0);
graphics.setColor(Color.WHITE);
graphics.setFont(font);
graphics.drawText(label,0,texty,DrawStyle.ELLIPSIS,getWidth());
} else {
graphics.drawBitmap(0, 0, getWidth(), getHeight(),onPicture , 0, 0);
graphics.setColor(Color.WHITE);
graphics.setFont(font);
graphics.drawText(label,0,texty,DrawStyle.ELLIPSIS,getWidth());
}
}
public boolean isFocusable() {
return true;
}
protected void onFocus(int direction) {
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
invalidate();
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ENTER) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
When you create the button you need to pass the Field.FIELD_HCENTER flag as one of your style flags.
CustomButtonField button = new CustomButtonField("TestLabel", Field.FIELD_HCENTER);