i am one Blackberry developer and i built one application that to view multiple location and point thus user go the point at that time they show is address and they click label then push screen to another page.This type application done then how to do?
public class GpsScreen extends FullScreen{
LabelField label;
public GpsScreen() {
super(DEFAULT_CLOSE);
label=new LabelField(){
protected boolean trackwheelClick(int status, int time) {
Dialog.alert("Hi");
return super.trackwheelClick(status, time);
}
};
String document = ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "";
Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments(MapsArguments.ARG_LOCATION_DOCUMENT,document));
}
Here i given code thus given me solution with some example .some urgent.
try this
LabelField label=new LabelField("click"){
protected boolean trackwheelClick(int status, int time) {
Dialog.alert("track wheel click");
return true;
}
protected boolean keyChar(char character, int status, int time) {
if(character == '\n'){
Dialog.alert("enter button press");
return true;
}
return super.keyChar(character, status, time);
}
};
Related
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.
i'm creating one application in which i get gift images with id's from web server through JSON. When i click on any gift image, it goes on next page where it shows all information of that image (get image information with its id from web server through JSON).
Problem is: When i click on any gift image on page to see its relevant information, it gets the last gift image id every time, i want when i click on any image, it gets the specific image id which i click. How it is possible??
Screenshot of the page is : http://ugo.offroadstudios.com/gifts.png
Here is sample code:
public class Gifts extends MainScreen {
String giftsid;
BitmapField giftimg;
public Gifts(){
setTitle("Gift Store");
creategifts();
}
public void creategifts()
{
//Link URL
String strURL = "http://ugo.offroadstudios.com/api/frndgift/?loginusername=adil;deviceside=true";
webConnection wb = new webConnection();
String res = wb.getJson(strURL);
try {
JSONObject object = new JSONObject(res);
if(object.getString("status") == "error")
{
Dialog.alert("Invalid "+object.getString("status"));
}
else
{
int totalgifts;
totalgifts = object.getInt("totalgifts");
Bitmap listThumb;
JSONArray imagearr;
JSONArray giftsidarr;
String imgname;
Bitmap bmpResized;
for(int i=0; i < totalgifts; i++){
imagearr = object.getJSONArray("gifts_image");
imgname = imagearr.getString(i);
giftsidarr = object.getJSONArray("gifts_id");
giftsid = giftsidarr.getString(i);
listThumb = getImage.getImageFromUrl("http://ugo.offroadstudios.com/wp-content/plugins/bp-gifts-rebirth/includes/images/"+imgname+";deviceside=true");
bmpResized = GPATools.ResizeTransparentBitmap(listThumb, 80, 80,
Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FIT);
giftimg =new BitmapField(bmpResized,FOCUSABLE)
{
protected boolean navigationClick(int status, int time)
{
Dialog.alert("giftsid "+giftsid);
UiApplication.getUiApplication().pushScreen(new SendGift(giftsid));
return true;
}
};
add(giftimg);
}
}
}
catch (JSONException e) {
System.out.println("EX is "+e);
e.printStackTrace();
}
}
}
You are always getting the gift id of the last gift in the list because you have created your buttons with this code:
giftimg =new BitmapField(bmpResized,FOCUSABLE)
{
protected boolean navigationClick(int status, int time)
{
Dialog.alert("giftsid "+giftsid);
UiApplication.getUiApplication().pushScreen(new SendGift(giftsid));
return true;
}
};
Your navigationClick() method used the giftsid variable, which is a persistent member variable of your class. You assign this variable in your for loop, so the final value it keeps is the last value assigned in the loop (giftsidarr.getString(totalgifts)).
Although you declare the navigationClick() method in a loop where the giftsid is many different values, the navigationClick() method uses the value of giftsid when it is run. The last value.
There's many ways to fix it. You can use a separate constant value in your loop:
final String nextGiftsId = giftsid;
giftimg =new BitmapField(bmpResized,FOCUSABLE)
{
protected boolean navigationClick(int status, int time)
{
Dialog.alert("nextGiftsId= "+nextGiftsId);
UiApplication.getUiApplication().pushScreen(new SendGift(nextGiftsId));
return true;
}
};
Or, as Signare suggested, attach a cookie to each button that identifies its corresponding gift:
giftimg =new BitmapField(bmpResized,FOCUSABLE)
{
protected boolean navigationClick(int status, int time)
{
String giftId = (String)getCookie(); // read gift id from the cookie
Dialog.alert("giftId= "+giftId);
UiApplication.getUiApplication().pushScreen(new SendGift(giftId));
return true;
}
};
giftimg.setCookie(giftsid); // set the cookie after creating the field
Inside your for loop, add the following code -
giftimg[i].setChangeListener(this);
Then -
public void fieldChanged(Field field, int context) {
for(int i=0;i<totalgifts;i++) {
if(field == giftimg[i]) {
// you can trigger your event
}
}
EDIT :-
giftimg[i].setChangeListener(listener);
listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if ( field instanceof BitmapField ) {
for(int i=0;i<totalgifts;i++) {
if ( field == giftimg[i] ) {
// you can trigger your event
}
}
}
}
};
If the length returned by input.getText() is greater than 13, the last character entered by the user should not appear on the edit field. If the 13th character is a ',' the program should allow 2 additional characters after the ','. That way, the maximum length of the edit field would be 16.
What would be an option to limit the text width of an EditField like this?
input = new BorderedEditField();
input.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if(input.getText().length() < 13)
input.setText(pruebaTexto(input.getText()));
else
//do not add the new character to the EditField
}
});
public static String pruebaTexto(String r){
return r+"0";
}
I have coded a simple BorderedEditField class which extends EditField. The method, protected boolean keyChar(char key, int status, int time) of this class gets modified so that manipulation of EditField's default behavior is possible. If you found this example helpful, then you can improve the implementation.
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen {
public MyScreen() {
BorderedEditField ef = new BorderedEditField();
ef.setLabel("Label: ");
add(ef);
}
}
class BorderedEditField extends EditField {
private static final int MAX_LENGTH = 13;
private static final int MAX_LENGTH_EXCEPTION = 16;
private static final char SPECIAL_CHAR = ',';
protected boolean keyChar(char key, int status, int time) {
// Need to add more rules here according to your need.
if (key == Characters.DELETE || key == Characters.BACKSPACE) {
return super.keyChar(key, status, time);
}
int curTextLength = getText().length();
if (curTextLength < MAX_LENGTH) {
return super.keyChar(key, status, time);
}
if (curTextLength == MAX_LENGTH) {
char spChar = getText().charAt(MAX_LENGTH - 1);
return (spChar == SPECIAL_CHAR) ? super.keyChar(key, status, time) : false;
}
if (curTextLength > MAX_LENGTH && curTextLength < MAX_LENGTH_EXCEPTION) {
return super.keyChar(key, status, time);
} else {
return false;
}
}
}
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);
}
};
I am new to BlackBerry App development. I want to be able to listen for keypress events whenever the BlackBerry (8900 in my case) is on and on all screens is this possible?
If so, it would be great for someone to direct me in the right direction. I am already having a look at Interface KeyListener.
import net.rim.device.api.system.*;
Thanks all
Implement a keylistenerClass like:
import model.Profile;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
public final class ShortcutHandler implements KeyListener {
public boolean keyChar(char key, int status, int time) {
return false;
}
public boolean keyDown(int keycode, int time) {
if (Keypad.KEY_ESCAPE == Keypad.key(keycode)) {
// Consume the event.
// Here I'm consuming the event for the escape key
return true;
}
//let the system to pass the event to another listener.
return false;
}
public boolean keyRepeat(int keycode, int time) {
return false;
}
public boolean keyStatus(int keycode, int time) {
return false;
}
public boolean keyUp(int keycode, int time) {
return false;
}
}
Then in your application constructor
public Application() {
//Add the listener to the system for this application
addKeyListener(new ShortcutHandler());
}
I confirm that it's working when the application is in the background.
As I understood, you want to listen to all key events in all applications running on device, not only in your application.
I think it's not possible.
UPDATE
How does the volume up and down key work? – Abs 11 hours ago
If you want to say that all applications receive key events from volume keys, thats not true. RIM OS will receive those events and then update all audio components like alert, audio, player etc.
you can easely check it with this sample:
Do following:
run sample
enter some key events
look at events number
go background
enter some key events
go back to sample by menu->switch application
check events number, it still the same
Code:
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
public class KeyListenerApp extends UiApplication implements KeyListener {
Scr mScreen;
public KeyListenerApp() {
mScreen = new Scr();
pushScreen(mScreen);
addKeyListener(this);
}
public static void main(String[] args) {
KeyListenerApp app = new KeyListenerApp();
app.enterEventDispatcher();
}
private void updateScreen(final String text) {
mScreen.addLine(text);
}
public boolean keyChar(char key, int status, int time) {
updateScreen("keyChar " + key);
return true;
}
public boolean keyDown(int keycode, int time) {
updateScreen("keyDown " + keycode);
return true;
}
public boolean keyRepeat(int keycode, int time) {
updateScreen("keyRepeat " + keycode);
return true;
}
public boolean keyStatus(int keycode, int time) {
updateScreen("keyStatus " + keycode);
return true;
}
public boolean keyUp(int keycode, int time) {
updateScreen("keyUp " + keycode);
return true;
}
}
class Scr extends MainScreen {
int mEventsCount = 0;
LabelField mEventsStatistic = new LabelField("events count: "
+ String.valueOf(mEventsCount));
public Scr() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
add(mEventsStatistic);
}
public void addLine(final String text) {
getApplication().invokeLater(new Runnable() {
public void run() {
mEventsStatistic.setText("events count: "
+ String.valueOf(++mEventsCount));
insert(new LabelField(text), 1);
}
});
}
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(goBGMenuItem);
}
MenuItem goBGMenuItem = new MenuItem("go backgroun", 0, 0) {
public void run() {
getApplication().requestBackground();
}
};
}
This how I imagine it could work
create application which extends UiApplication or even Application
create an implementation of Keylistener (which could also extend Thread if you want)
add your KeyListener implementation to your application via addKeyListener()
Then do whatever you want.
The code given above certainly works, but there is a catch. You wont be able to trap the key presses on native apps like call handling sms incoming browsing and stuff. As system generates global event to these apps. Its like you are able to define a routine for clicks when your app is in background , but the functionality of that routine is localised to your application only. It wont effect other apps as such.