How to implement clickable LabelField. - blackberry

I need to create a LabelField which can perform click action. I have overrided navigationClick(int status, int time). But when I click on that LabelField, MenuButton popup is appearing. How to disable it. The code I'm using is given below:
LabelField pdtitem = new LabelField(pdts.getProducts_name(),LabelField.FOCUSABLE) {
protected boolean navigationClick(int status, int time) {
Dialog.alert("hai");
UiApplication.getUiApplication().pushScreen(new desc());
return super.navigationClick(status, time);
}
};

Consume event in your navigationClick() method.
return true instead of return super.navigationClick(status, time);

Related

rating stars are hiding as i click over the star image

My requirement is to create 5 stars when i click bitmap changes,When "Unclicked" bitmap changes back to default.and if star 1 is not clicked then all 4 stars after must not be clickable, if star 2 is clicked then star 3 must be clickable and stars 4-5 must not be clickable, (and then backwards) if all 5 stars have been selected only star5 must be clickable, if star 5 and 4 are unclicked then star3must be clickable stars 2-1 must not be clickable, etc.
public class Starscreen extends MainScreen
{
protected static BitmapField Star1 = null;
protected static BitmapField Star2 = null;
protected static BitmapField Star4 = null;
protected static BitmapField Star5 = null;
protected static final Bitmap StarClicked = null;
protected static BitmapField Star3 = null;
BitmapField bitmapField1;
private Bitmap StarNotClicked;
public Starscreen(Secondscreen secondscreen)
{
setTitle("Star Screen");
LabelField RateDeal = new LabelField("Rating: ");
Mainlayout.add(RateDeal);
HorizontalFieldManager StarManager=new HorizontalFieldManager
(USE_ALL_WIDTH);
final Bitmap StarNotClicked = Bitmap.getBitmapResource("Star3.png");
final Bitmap StarClicked = Bitmap.getBitmapResource("Star4.png");
Star1 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
private Object StarClicked;
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap((Bitmap) StarClicked);
Star2.setBitmap(StarNotClicked);
Star3.setBitmap(StarNotClicked);
Star4.setBitmap(StarNotClicked);
Star5.setBitmap(StarNotClicked);
AmountOfStarsSelected(1);
return true;
}
};
Star2 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
private Object StarClicked;
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap((Bitmap) StarClicked);
Star2.setBitmap((Bitmap) StarClicked);
Star3.setBitmap(StarNotClicked);
Star4.setBitmap(StarNotClicked);
Star5.setBitmap(StarNotClicked);
AmountOfStarsSelected(2);
return true;
}
};
Star3 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
// private Object StarClicked;
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap((Bitmap) StarClicked);
Star2.setBitmap((Bitmap) StarClicked);
Star3.setBitmap((Bitmap) StarClicked);
Star4.setBitmap(StarNotClicked);
Star5.setBitmap(StarNotClicked);
AmountOfStarsSelected(3);
return true;
}
};
Star4 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap(StarClicked);
Star2.setBitmap(StarClicked);
Star3.setBitmap(StarClicked);
Star4.setBitmap(StarClicked);
Star5.setBitmap(StarNotClicked);
AmountOfStarsSelected(4);
return true;
}
};
Star5 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
Star1.setBitmap(StarClicked);
Star2.setBitmap(StarClicked);
Star3.setBitmap(StarClicked);
Star4.setBitmap(StarClicked);
Star5.setBitmap(StarClicked);
AmountOfStarsSelected(5);
return true;
}
};
StarManager.add(Star1);
StarManager.add(Star2);
StarManager.add(Star3);
StarManager.add(Star4);
StarManager.add(Star5);
Mainlayout.add(StarManager);
add(Mainlayout);
}
}
By executing above code i am getting an error and it shows create method for AmountOfStarsSelected(); but i dont have any idea what condition should i use inside AmountOfStarsSelected(); please help me i am new to blackberry
I'd say make a separate class for this, extended from HorizontalFieldManager. Have that class create the 5 buttons and lay them out, and contain a local state variable int level. Create a public setLevel(int level) and getLevel() method in this class. Where level refers to the star currently selected.
In setLevel put the following code:
public void setLevel(int level)
{
if(Math.abs(this.level - level) != 1)
{
return; // this.level refers to your local state. return because there is no change, or the user didn't click 1 up or down.
}
if(level == 0)
{
Star1.setBitmap(StarClicked);
}
else
{
Star1.setBitmap(StarNotClicked);
}
if(level == 1)
{
Star2.setBitmap(StarClicked);
}
else
{
Star2.setBitmap(StarNotClicked);
}
if(level == 2)
{
Star3.setBitmap(StarClicked);
}
else
{
Star3.setBitmap(StarNotClicked);
}
if(level == 3)
{
Star4.setBitmap(StarClicked);
}
else
{
Star4.setBitmap(StarNotClicked);
}
if(level == 4)
{
Star5.setBitmap(StarClicked);
}
else
{
Star5.setBitmap(StarNotClicked);
}
this.level = level;
}
In each of your stars you should call this setLevel method
Star1 = new BitmapField(StarNotClicked, BitmapField.FOCUSABLE){
protected boolean navigationClick(int status, int time)
{
setLevel(0); // If these fields are the only thing in your horizontal field you can get away with using setLevel(getIndex());
return true;
}
};
Remember that in your constructor you should set level to -1, then call setLevel(0) so that the first star can be selected by default.

Move Focus is not working properly on List Field

I am working on ListView section, in this, the user can search the content by name and directly move at the first element of List via pressing a keyboard button. Like, if you press button B from (right vertical manager) it will scroll the list and move focus to first record of B.
The code is working fine in simulator but it's not working on Touch device - I have BB 9380 Curve.
here is the code for :
LabelField a = new LabelField("A" , FOCUSABLE)
{
protected void paint(Graphics graphics)
{
graphics.setColor(0xC4C4C4);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
//fieldChangeNotify(1);
injectKey(Characters.LATIN_CAPITAL_LETTER_A);
injectKey(Characters.LATIN_CAPITAL_LETTER_A);
return true;
}
};
private void injectKey(char key)
{
try
{
searchList.setFocus();
KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
inject.post();
/*inject.post();*/
} catch (Exception e) {
Log.d("In injectKey :: :: :: "+e.toString());
MessageScreen.msgDialog("In Inject Key "+e.toString());
}
}
Alternate Solution
I would recommend a different strategy for this. Instead of trying to simulate key press events, I would define one method that handles a keypress of a certain letter, or a touch click on that same letter's LabelField.
Source: blackberry.com
So, you can have code that handles key presses by using
protected boolean keyChar( char character, int status, int time )
{
// you might only want to do this for the FIRST letter entered,
// but it sounds like you already have the keypress handling
// the way you want it ...
if( CharacterUtilities.isLetter(character) )
{
selectLetter(character);
return true;
}
return super.keyChar( character, status, time );
}
and then also handle touch events:
LabelField a = new LabelField("A" , FOCUSABLE)
{
protected void paint(Graphics graphics)
{
graphics.setColor(0xC4C4C4);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
char letter = getText().charAt(0);
selectLetter(letter);
return true;
}
};
then, simply define a method that takes in one character, and scrolls to the start of that part of the list:
private void selectLetter(char letter);
Key Injection
If you really, really want to simulate key presses, though, you might try changing the code so that it injects two events: key down, and then key up (you're currently injecting two key down events). This might be causing problems.
injectKey(Characters.LATIN_CAPITAL_LETTER_A, true);
injectKey(Characters.LATIN_CAPITAL_LETTER_A, false);
with
private void injectKey(char key, boolean down)
{
try
{
searchList.setFocus();
int event = down ? KeyEvent.KEY_DOWN : KeyEvent.KEY_UP;
KeyEvent inject = new KeyEvent(event, key, 0);
inject.post();
} catch (Exception e) { /** code removed for clarity **/
}
}
Additional Note
For UIs, I like to trigger events on the key up, or unclick events. I think this makes a better experience for the user. So, you could replace keyChar() with keyUp() and navigationClick() with navigationUnclick() if you want to do this.

Blackberry button click handler

I want to run some Java code when the user clicks on this ToolbarButtonField in my BlackBerry app. I have the following code which is not working. Please tell me where I am wrong.
butHome = new ToolbarButtonField(new StringProvider("Home"));
butHome.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Clicked...");
}
});
You can use:
ToolbarButtonField#invoke
Performs an action when this
ToolbarButtonField is clicked on if
Command has been set. A click is
defined as the following sequence of
touch events: TouchEvent.DOWN,
TouchEvent.CLICK, TouchEvent.UNCLICK
and TouchEvent.UP.
You're going to have to use that in conjuction with the Command framework. If that's not desirable, override ToolbarButtonField#touchEvent for a TouchEvent.UNCLICK event to execute the desired code.
public boolean touchEvent(TouchEvent message) {
if ( message.geEvent() == TouchEvent.UNCLICK ) {
// do what I want.
}
}
Try this:
butHome = new ToolbarButtonField(new StringProvider("Home")) {
protected boolean navigationClick(int status, int time) {
System.out.println("Clicked...");
return true;
}
});

problem with back button

when i am pressing the back button a pop screen is displayed which shows three button save, discard and cancel button i don't want this screen to be popped up. is this possible.
Thanks in advance
The default behaviour of the back button is to save changes for dirty screens. Rewrite the onClose() method to overwrite the default behaviour.
public boolean onClose() {
int choice = Dialog.ask(Dialog.D_YES_NO, "¿Do you want to exit?", Dialog.YES);
if (choice == Dialog.YES) {
//write a close() routine to exit
close();
}
return true;
}
You return true because you managed the ESC button pressed event. Review the Screen class docs.
You can also change the default behaviour of the ESC button rewriting the keyChar method as follows:
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ESCAPE) {
onClose();
return true;
}
return super.keyChar(character, status, time);
}
close() should be somenthing like:
public void close() {
System.exit(0);
}
Override the onSavePrompt method. Then that screen will not come. Actually that popup screen will come only when something is changed on your screen. So it will ask you for the appropriate action.
protected boolean onSavePrompt() {
return true;
}
Skip the saving prompt with it
protected boolean onSavePrompt() {
return false;
}
Override onClose() method like this:
public boolean onClose() {
close();
return true;
}
you will not get that annoying alert message.

how to close a popup screen in blackberry bold

hey i have displayed a pop-up screen when i click on a menu item
now i want to close that pop-up screen when user presses escape key.but it does not work and remain stuck,till i click on a button on the pop=up screen.
how can i achieve that????
filter is my pop-up screen
my code is :::
protected boolean keyChar(char c, int status, int time)
{
boolean retVal = false;
if (c == Characters.ESCAPE)
{
close();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
//UiApplication.getUiApplication().popScreen(filter);
UiApplication.getUiApplication().
popScreen(UiApplication.getUiApplication().getActiveScreen());//(filter);
}
});
retVal = super.keyChar(c,status,time);
}
return retVal;
}
i need to override the keychar method in pop-up screen,search for escape and then close
code :
popupscreen1=new PopupScreen(myverticalfieldmanager)
{
protected boolean keyChar(char c, int status, int time)
{
if (c == Characters.ESCAPE)
close();
return super.keyChar(c, status, time);
}
};

Resources