2

I am trying to push a new screen when a bitmap is "clicked" on the screen. For this I have created a Class from this post: Blackberry Clickable BitmapField whose partial code I've posted below:

public class CustomMenuButtonField extends Field{
    Bitmap normal,focused;

    ...

    protected boolean navigationClick(int status, int time)
    {
        // push new screen
        fieldChangeNotify(0);
        return true;
    }

    ...

I want to push a new screen when the user clicks the bitmap. I have consulted this thread: Communicating between classes, but I still can't figure out the commands to call the new screen command. The UserInterface I have so far is:

public class UserInterface extends UiApplication {
    public static void main(String[] args){
        UserInterface theApp = new UserInterface();
        theApp.enterEventDispatcher();
    }
    public UserInterface() {
        pushScreen(new UserInterfaceScreen());
    }
}

final class UserInterfaceScreen extends MainScreen {
    public UserInterfaceScreen() {
    ...

What would be the command to pop up the new screen, and more importantly where would I be able to work on it? I know it should probably use pushScreen() but that is not recognized in that class. Would I create a new final class NewScreenFromClick extends MainScreen? and if so how would I call it and put it in the eventDispatcher. I've been going through the blackberry site but they don't have much sample code on this issue and I am very new to Java so this is fairly confusing to me.

Community
  • 1
  • 1
user1152440
  • 895
  • 2
  • 13
  • 25

4 Answers4

2

You can use:

UiApplication.getUiApplication().pushScreen(new HomeScreen());

If your application is not doing anything strange UiApplication.getUiApplication() will always return the UiApplication object for your program. This is a singleton object created for each UiApplication context.

You could also use:

UserInterface.getUiApplication().pushScreen(new HomeScreen());

If the UserInterface class is visible to the class you are working on, but this would make your code less re-usable.

I've collected some basic samples on my blog. Have a look at this page. Start at the bottom and work your way up.

Richard
  • 8,920
  • 2
  • 18
  • 24
  • Thank you Richard, the commands you provided worked perfectly. I will definitely also be looking at your blog page. Do you happen to know what the issue was (explained in the comment section) in the post below by rfsk2010? I just couldn't seem to call the button portion of the code. – user1152440 Jan 27 '12 at 19:56
  • Nothing leaps out. What error are you getting from the simulator? – Richard Jan 28 '12 at 03:45
  • App Error 104 Uncaught NullPointerException – user1152440 Jan 30 '12 at 14:24
  • Good question, you're probably going to have to try to insert some try{} catch{} blocks and find out where the NullPointer is being used. – Richard Jan 30 '12 at 17:49
  • Thanks ill try that. Would it be something along the lines of try{ code being tested here } catch { exception e } ? – user1152440 Jan 30 '12 at 19:09
2
    imageField imageField = new imageField ("",Field.FOCUSABLE,"image.png","image.png", 0x102839);
    add(imageField );
    FieldChangeListener listener = new FieldChangeListener() {
    public void fieldChanged(Field field, int context) {
            if (field == imageField ) {
    home home = new home();//your screen
    UiApplication.getUiApplication().pushScreen(home);

                  }
                }
                         };
imageField .setChangeListener(listener);

//imagefield class is given below

package com.pl.button;

import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;

public class imagefield extends Field {

private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;

private Bitmap _currentPicture;
private Bitmap _onPicture; 
private Bitmap _offPicture; 
int color;

public imagefield (String text, long style ,String img, String img_hvr, int color){
    super(style);


    _offPicture = Bitmap.getBitmapResource(img);
    _onPicture = Bitmap.getBitmapResource(img_hvr);

    _font = getFont();
    _label = text;
    _labelHeight = _onPicture.getHeight();  
    _labelWidth = _onPicture.getWidth();

    this.color = color;

    _currentPicture = _offPicture;
}

/**
 * @return The text on the button
 */
String getText(){
    return _label;
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#getPreferredHeight()
 */
public int getPreferredHeight(){
    return _labelHeight;
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#getPreferredWidth()
 */
public int getPreferredWidth(){
    return _labelWidth;
}

/**
 * Field implementation.  Changes the picture when focus is gained.
 * @see net.rim.device.api.ui.Field#onFocus(int)
 */
protected void onFocus(int direction) {
    _currentPicture = _onPicture;
    invalidate();
}

/**
 * Field implementation.  Changes picture back when focus is lost.
 * @see net.rim.device.api.ui.Field#onUnfocus()
 */
protected void onUnfocus() {
    _currentPicture = _offPicture;
    invalidate();
}

/**
 * Field implementation.  
 * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
 */
protected void drawFocus(Graphics graphics, boolean on) {
    // Do nothing
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#layout(int, int)
 */
protected void layout(int width, int height) {
    setExtent(Math.min( width, getPreferredWidth()), 
    Math.min( height, getPreferredHeight()));
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#paint(Graphics)
 */
protected void paint(Graphics graphics){       
    // First draw the background colour and picture
    graphics.setColor(this.color);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);

    // Then draw the text
    graphics.setColor(Color.BLACK);
    graphics.setFont(_font);
    graphics.drawText(_label, 4, 2, 
        (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
        getWidth() - 6 );
}

/**
 * Overridden so that the Event Dispatch thread can catch this event
 * instead of having it be caught here..
 * @see net.rim.device.api.ui.Field#navigationClick(int, int)
 */
protected boolean navigationClick(int status, int time){
    fieldChangeNotify(1);
    return true;
}

}

Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
1

Try the following code I have added UserInterfaceScreen. When UserInterfaceScreen is constructed, it adds a CustomMenuButtonField and sets a field change listener. When the button is clicked it pushes the new screen to the stack

package mypackage;

import net.rim.device.api.ui.UiApplication;

public class UserInterface extends UiApplication {
public static void main(String[] args){
    UserInterface theApp = new UserInterface();
    theApp.enterEventDispatcher();
}
public UserInterface() {
    pushScreen(new UserInterfaceScreen());
}
}

This is the UserInterfaceScreen

package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class UserInterfaceScreen extends MainScreen {

public UserInterfaceScreen() {
    super();
    //replace null with the image string
    CustomMenuButtonField button = new CustomMenuButtonField(null, null);
    button.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            UiApplication.getUiApplication().pushScreen(new         MyHomeScreen());

        }
    });
}
}
rfsk2010
  • 8,571
  • 4
  • 32
  • 46
  • Thank you the code UserInterface.getUiApplication().pushScreen(new HomeScreen()); does work! However I couldn't run the button function right under super(). It would always provide me an error in the simulator. I pretty much copy pasted the example you provided right under my super() call and created a MyHomeScreen class in another file. When I comment out the button code there are no errors. I was eventually able to call the function through a bitmap click navigationClick(). Do you have an idea of why I wasn't able to call the button portion of the code without a simulator error-104. – user1152440 Jan 27 '12 at 19:37
0

Look the answer in the below link which I posted by me(alishaik786) :

Clickable Bitmap;

alishaik786
  • 3,696
  • 2
  • 17
  • 25