Custom Pop up with no Black background in blackberry - 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

Related

Custom PopUp to work like tooltip without time out

I need to create a Customised PopUp to work like a ToolTip How can i achieve it
i have worked on a code please check out and tell me where i need improvement
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class ToolTip extends PopupScreen{
private static VerticalFieldManager vfm;
private LabelField lbl;
private int xPosition;
private int yPosition;
private String message;
public ToolTip(String message,int xPos,int yPos){
super(vfm);
this.xPosition=xPos;
this.yPosition=yPos;
this.message=message;
vfm=new VerticalFieldManager(){
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0,0,getWidth(),getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0,0,getWidth(),getHeight());
super.paint(graphics);
}
};
lbl=new LabelField(message);
vfm.add(lbl);
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setExtent(lbl.getPreferredWidth(), lbl.getPreferredHeight());
setPosition(xPosition, yPosition);
}
}
i get an error of NullPointer Exception at super(vfm) because vfm is null when i use it as follows . How can i optimise my code .
ButtonField bf1=new ButtonField("Login"){
protected boolean navigationClick(int status, int time) {
UiApplication.getUiApplication().pushScreen(new ToolTip("Hello ", 50, 50));
return super.navigationClick(status, time);
}
};
add(bf1);
Pass an initialized instance of VerticalFieldManager to super(..) of PopupScreen. Check following code.
class ToolTip extends PopupScreen {
private static VerticalFieldManager vfm = new VerticalFieldManager() {
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0, 0, getWidth(), getHeight());
super.paint(graphics);
}
};
// other codes
public ToolTip(String message, int xPos, int yPos) {
super(vfm);
// other codes
}
protected void sublayout(int width, int height) {
// other codes
}
}
Updated code for custom background support
class ToolTip extends PopupScreen {
private int xPosition;
private int yPosition;
public ToolTip(String message, int xPos, int yPos) {
super(new VerticalFieldManager());
// Define and set background here
// Use - BackgroundFactory.createBitmapBackground(bitmap) for bitmap background
getDelegate().setBackground(BackgroundFactory.createSolidBackground(Color.RED));
// Add other UI Field here
this.xPosition=xPos;
this.yPosition=yPos;
add(new LabelField(message));
// other codes
}
// Empty implementation of this will disable default rounded border.
protected void applyTheme() {
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setPosition(xPosition, yPosition);
}
}
You need to use getDelegate().setBackground(..) to set the background. Check the class BackgroundFactory for creating custom background.

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 .

Label/Name for Bitmap Button Field

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();
}
}

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.

How to setBackground of Field in Blackberry 4.5?

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;
}

Resources