Label/Name for Bitmap Button Field - blackberry

Currently I have 4 bitmap buttons in my application. I want to have something like Label/Name for each of the buttons so that whenever someone focus on the particular button, the name appears somewhere on the screen. It could be on top, below the buttons or anywhere will do.
How can I do this? I tried searching for bitmap button field labelling but I found nothing really helpful.

you can put a CustomLabelField below button fields. Override your ButtonFields onFocus(int direction) and onUnfocus() methods. Inside them invoke setLabel(String label) method of your CustomLabelField
class CustomLabelField extends Field {
String label;
public void setLabel(String label){
this.label = label;
invalidate();
}
protected void layout(int arg0, int arg1) {
setExtent(Display.getWidth, getFont().getHeight());
}
protected void paint(Graphics graphics) {
graphics.setColor(Color.Black);
graphics.drawText(label, 0, 0);
}
}
EDIT (after comments)
You can use this custom button and add your additional features in it. I did not try if this is working, but should work.
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.container.MainScreen;
public class CustomButtonField extends BitmapField{
private String label = "";
private MainScreen yourScreen;
public CustomButtonField(String label, MainScreen yourScreen) {
super();
this.label = label;
this.yourScreen = yourScreen;
}
protected void onFocus(int direction) {
yourScreen.setTitle(label);
super.onFocus(direction);
}
protected void onUnfocus() {
yourScreen.setTitle("");
super.onUnfocus();
}
}

Related

Custom Pop up with no Black background in blackberry

I want to remove the black background in Popup field
i know we use applyTheme method in blackberry to subdue its effect but dunno how to use it
I want to remove the black background and use an image instead .
I have tried this method
protected void applyTheme(Graphics arg0, boolean arg1) {
// TODO Auto-generated method stub
super.applyTheme(arg0, arg1);
}
public class CustomDialogBox extends PopupScreen {
Bitmap mDialogImg=null;
public CustomDialogBox(Bitmap dialogImg) {
super(new VerticalFieldManager(),Field.FOCUSABLE);
this.mDialogImg=dialogImg;
VerticalFieldManager vfm=new VerticalFieldManager() {
protected void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, mDialogImg.getWidth(), mDialogImg.getHeight(), mDialogImg, 0, 0);
};
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(mDialogImg.getWidth(), mDialogImg.getHeight());
super.setExtent(mDialogImg.getWidth(), mDialogImg.getHeight());
}
};
add(vfm);
}
protected void applyTheme() {
}
}
I tried the following program and it works perfectly fine
i added a Bitmap image to a vertical field manager and then
using the method
applyTheme() as follows
protected void applyTheme() {
}
i do get the required results

BlackBerry: Hilight the background of a horizontalFieldManager

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.

Blackberry BitmapField focus-unfocus prob

I want to set a text in a specific label on focus of an BitmapField, and reset the text on unfocus. I am using the following code:
final BitmapField tab1 = new BitmapField(Bitmap.getBitmapResource("img/icon1.png"), FIELD_BOTTOM | BitmapField.FOCUSABLE) {
protected void drawFocus(Graphics graphics, boolean on) {
// the simplies way to draw a rectangle and this will be the
// focus
}
protected boolean navigationClick(int status, int time) {
// write here your code what you want to run the user clicks to
// the bitmap
// try something like this
Dialog.alert("Code for tab1");
return true;
}
public void setFocus(){
super.setFocus();
selectedLabel.setText("tab1");
}
public void onUnfocus(){
super.onUnfocus();
selectedLabel.setText("");
}
};
But focus is changing properly, but label is not setting at all. Where is the problem in my code?
override onFocus(int direction) instead of setFocus()
protected void onFocus(int direction) {
super.onFocus(direction);
selectedLabel.setText("tab1");
}
protected void onUnfocus() {
super.onUnfocus();
selectedLabel.setText("");
}
Use onfocus() & onUnfocus() method and replace your LabelField in both method with Invalidate() option .

Blackberry scoll only browserfield, not entire app-layout

I have a verticalfieldmanager with buttons and below that a browserfield. The problem is that when I scroll vertically in the browserfield the whole layout of the app scrolls with it(!).. I've been looking like a mad man for a sollution but haven't found anything yet. I tried disabling vertical scroll in the app but this only results in not being able to scroll the browserfield/homepage.
Any suggestions?
Thanks
Hope this code helps you: try this;
public class NewsBrowserScreen extends MainScreen implements FieldChangeListener
{
String url="http://www.google.com/news/";
VerticalFieldManager vertical;
BrowserField browserField;
ButtonField click;
public NewsBrowserScreen()
{
createGUI();
}
private void createGUI()
{
click=new ButtonField("Click",Field.FIELD_HCENTER);
click.setChangeListener(this);
add(click);
vertical=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR|HORIZONTAL_SCROLL|HORIZONTAL_SCROLLBAR)
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(Display.getWidth(),250);
setExtent(Display.getWidth(),250);
}
};
vertical.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
vertical.setPadding(10, 0, 10, 0);
add(vertical);
}
protected boolean onSavePrompt()
{
return true;
}
public boolean onMenu(int instance)
{
return true;
}
public void fieldChanged(Field field, int context)
{
if(field==click)
{
browserField=new BrowserField();
vertical.add(browserField);
browserField.requestContent(url);
}
}
}
I Get like this below Image;
You can add a VerticalFieldManager to your MainScreen and add your BrowserField to your VerticalFieldManager. You should set style of VerticalFieldManager with VERTICAL_SCROLL | VERTICAL_SCROLLBAR and clear the syle of your MainScreen

How can i create a popup for custom Choicefield and call it?

I have created my own simple ChoiceField by extending ObjectChoiceField.. It is like..
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Now i want to display the my choices when i select MyChoiceField in custom menu(not the default popup that blackberry provides when user clicks on a ChoiceField)..
How can i achieve it? Sample codes will be appreciable..
Thanks
Try Overriding the method fieldChangeNotify(int context) of ObjectChoiceField like this:
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
protected void fieldChangeNotify(int context) {
int index = getSelectedIndex();
try{
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(new LabelField("[Pop Up Content]\nChoice Field Changed #\n"+choices[index]+" selected."));
PopupScreen popUpScreen = new PopupScreen(vfm){
public boolean onClose()
{
this.close();
return true;
}
};
UiApplication.getUiApplication().pushScreen(popUpScreen);
} catch(Exception exc) {
System.out.println("Exception in choiceField fieldChangeNotify");
}
}
public void paint(Graphics graphics)
{
super.paint(graphics);
}
}
Use MyChoiceField now somewhat like this:
String[] choices; // Keep This Global
VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
choices = new String[2];
choices[0] = new String("Choice1");
choices[1] = new String("Choice2");
vfm.add(new MyChoiceField("Choise", choices));
Let me know whether this solution works.

Resources