blackberry buttonfield navigate on 2 second hold - blackberry

I am developing a blackberry app. I want to open an screen from the home screen when user will press one button and hold it for 2 seconds.
Any way?
Thanks.

Here is the code for your question. I have make use of this BlackBerry LongClickListener implementation link which contains good explanation too.
public class HoldButtonScreen extends MainScreen implements FieldChangeListener {
ButtonField _btnHold;
Timer _timer;
public HoldButtonScreen() {
_btnHold = new ButtonField("Hold 2 sec to get popup")
{
protected boolean navigationClick(int status, int time) {
final Field _btnHold= this;
_timer = new Timer();
System.out.println("hi there");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try{
_timer.schedule(new TimerTask() {
public void run() {
fieldChanged(_btnHold, 0);
}}, 2000);
}catch(Exception e){
e.printStackTrace();
}
}
});
return true;
}
protected boolean navigationUnclick(int status, int time) {
System.out.println("hi unclick");
add(new LabelField("You have't hold button for 2 second."));
_timer.cancel();
return true;
}
};
_btnHold.setChangeListener(this);
add(_btnHold);
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
PopupScreen _popUpScreen = new PopupScreen(new VerticalFieldManager()){
public boolean onClose() {
close();
return true;
}
};
_popUpScreen.add(new LabelField("Hello , i am pop up after 2 second."));
UiApplication.getUiApplication().pushScreen(_popUpScreen);
}
});
}
}

Try this this will run successfully.
Just change the Screen in PushScreen.
private Thread splashTread;
protected int _splashTime = 200;
boolean countinue = true;
splashTread = new Thread() {
public void run() {
while (countinue == true) {
try {
synchronized (this) {
wait(_splashTime);
}
} catch (InterruptedException e) {
} finally {
synchronized (UiApplication.getUiApplication().getAppEventLock()) {
UiApplication.getUiApplication().pushScreen(new Login());
SplashScreen.this.close();
}
countinue = false;
}
}
}
};
splashTread.start();

Related

Blackberry - How to make a clock count down and display on screen

I am making a quiz application with question and answer.
I want to make an clock count down ( Minute + Second ) and display it on screen.
But I can not how to do that.
Can anyone help me please ?
this is one solution to print time mm:ss format (Minutes:seconds)
public class StartUp extends UiApplication{
public static void main(String[] args) {
StartUp start=new StartUp();
start.enterEventDispatcher();
}
public StartUp() {
pushScreen(new Timerscreen());
}
}
class Timerscreen extends MainScreen
{
LabelField time;
long mille=0;
Timer timer=null;TimerTask task=null;
public Timerscreen() {
mille=1000*60*1;
time=new LabelField();
add(time);
timer=new Timer();
task=new TimerTask() {
public void run() {
synchronized (UiApplication.getEventLock()) {
if(mille!=0){
SimpleDateFormat date=new SimpleDateFormat("mm:ss") ;
System.out.println("================="+date.formatLocal(mille)+"====================="+Thread.activeCount());
time.setText(date.formatLocal(mille));
mille=mille-1000;
}else{
time.setText("00:00");
mille=1000*60*1;
timer.cancel();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Time expaired");
}
});
}
}
}
};
timer.schedule(task,0, 1000);
}
}
Same like above but some change:
First set:
int secs=120; // how many seconds you want;
public void callTheTimer()
{
label=new LabelField("\t");
add(label);
timer=new Timer();
timerTask=new TimerTask()
{
public void run()
{
synchronized (UiApplication.getEventLock())
{
if(secs!=0)
{
label.setText(""+(secs--)+" secs");
}
else
{
timer.cancel();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
label.setText("");
Dialog.alert("Times Up");
secs=120;
}
});
}
}
}
};
timer.schedule(timerTask, 0, 1000);
}
This is enough;

how to stop Browserfield requestContent() method when back to mainScreen on blackberry

When i click a button, in next screen i've loaded browserfield requestcontent() using thread.
and i've added browserfield listener.
when click back button , i came to first screen. But in background, requestContent is executing. how to stop it.?
i write the code on onClose() method
public boolean onClose()
{
for(int i=0;i<=Thread.activeCount();i++)
{
if(Thread.currentThread().isAlive())
{
Thread.currentThread().interrupt();
}
}
return super.onClose();
}
My code is.,
new Thread(new Runnable()
{
public void run()
{
loadWebContent(path);
}
}).start();
private void loadWebContent(String path)
{
final VerticalFieldManager vfm = new VerticalFieldManager(HORIZONTAL_SCROLL|VERTICAL_SCROLL)
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(maxWidth, (Display.getHeight()-47));
setExtent(maxWidth, (Display.getHeight()-47));
}
};
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
myBrowserField = new BrowserField(myBrowserFieldConfig);
myBrowserField.addListener(new BrowserFieldListener()
{
public void documentLoaded(BrowserField browserField,Document document) throws Exception
{
UiApplication.getApplication().invokeLater(new Runnable()
{
public void run()
{
try
{
mainlayout.delete(spinner);
mainlayout.add(myBrowserField);
myBrowserField.setZoomScale(1.0f);
}
catch (Exception e)
{
System.out.println("++ "+e);
}
}
});
}
});
myBrowserField.requestContent(path);
}
Pls help how to stop execution when back to first screen.
I think this is Enough:
public class LoadingScreen extends MainScreen
{
ButtonField click;
String url;
BrowserField browserField;
public LoadingScreen()
{
url="http://www.google.com";
browserField=new BrowserField();
add(browserField);
browserField.requestContent(url);
}
}
In onClose method:
public boolean onClose()
{
return super.close();
}
If you have any doubbts come on chat room named "Knowledge sharing center for blackberry and java" to clarify your and our doubts.

How to set Font of String Taken from Resource bundle in blackberry

I'm giving localization support in my app in blackberry.Now the problem is i want to use system font in my application but the hieght should be constant.Now i want to use Status.show method to show some string on the screen.The string is coming from the resource bundle.
So kindly suggest how to set the font of that.
Thanx in advance
Regards
Deepak Goel
public class DialogBox extends PopupScreen implements FieldChangeListener {
private ButtonField okButton = new ButtonField("OK", ButtonField.CONSUME_CLICK);
private static DialogBox dialog = null;
private DialogBox( String text) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
add(new LabelField(text, Field.FIELD_HCENTER));
okButton.setChangeListener(this);
add(okButton);
}
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.BLACK);
super.paint(graphics);
}
public static void show(String text) {
dialog = new BMDialogBox(text);
Thread threadToRun = new Thread() {
public void run() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(dialog);
}
});
try {
sleep(2000);
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException("Exception detected while waiting: " + t.toString());
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
UiApplication.getUiApplication().popScreen(dialog);
} catch (Exception e) { }
});
}
};
threadToRun.start();
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
UiApplication.getUiApplication().popScreen(dialog);
} catch (Exception e) { }
}
});
}
}
you can set a fields font with original style of it and given height(15) just like that:
setFont(getFont().derive(getFont().getStyle(),15));

Adding Editfield to Popup screen

I have created my own custom popup screen to which now I am trying to add a editfield , everything seems to be fine but the problem is that I am not able to write anything in the edit field
class sveetIt extends PopupScreen implements FieldChangeListener, DialogClosedListener {
Hashtable pitemData;
ButtonField sveetNowlabelField;
ButtonField sveetLaterlabelField;
WatingScreen watingScreen;
long scheduledTime;
Dialog updateDialog;
public sveetIt() {
super(new MyCustimGridFieldManager());
LabelField messageLabelField = new LabelField("Enter your Sveet Message:",Field.FIELD_HCENTER) {
protected void paint(Graphics graphics) {
graphics.setColor(Color.YELLOWGREEN);
super.paint(graphics);
}
};
EditField sveetTexteditField= new EditField(null, "Sveet Message", 50, EditField.FIELD_HCENTER
| EditField.FIELD_VCENTER
| EditField.NON_SPELLCHECKABLE | EditField.NO_NEWLINE);
VerticalFieldManager buttonVFManager = new VerticalFieldManager(VerticalFieldManager.FIELD_HCENTER);
HorizontalFieldManager hfManager = new HorizontalFieldManager();
sveetNowlabelField = new ButtonField("Sveet Now");
sveetLaterlabelField = new ButtonField("Sveet Later");
sveetNowlabelField.setChangeListener(this);
sveetLaterlabelField.setChangeListener(this);
add(messageLabelField);
add(sveetTexteditField);
hfManager.add(sveetNowlabelField);
hfManager.add(sveetLaterlabelField);
buttonVFManager.add(hfManager);
add(buttonVFManager);
}
public boolean isEditable() {
return true;
}
protected boolean keyChar(char c, int status, int time) {
boolean retVal = false;
if (c == Characters.ESCAPE) {
Ui.getUiEngine().popScreen(this);
retVal = super.keyChar(c, status, time);
}
return retVal;
}
public void fieldChanged(Field field, int context) {
if (sveetNowlabelField == field) {
//Here directly starts uploading file
beginUpload();
} else if (sveetLaterlabelField == field) {
// first picks up time when to upload
scheduledTime = getScheduleTime();
if(scheduledTime!=1L) {
//now begins uploading file
beginUpload();
}
}}
class SubscribingThread extends StoppableThread {
int network = 50;
public void run() {
}
}
public void beginUpload() {
try {
watingScreen = new WatingScreen("Uploading Sveet...");
/*
* UiApplication.getUiApplication().invokeAndWait( new Runnable() {
* public void run() { Ui.getUiEngine().pushScreen(watingScreen); }
* });
*/
BaseScreen.pushScreen(watingScreen);
Thread thread = new Thread(new Runnable() {
public void run() {
uploadToServer();
}
});
thread.start();
// uploadToServer();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
private long getScheduleTime() {
scheduledTime = 0;
final DateTimePicker datePick = DateTimePicker.createInstance();
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Calendar currentCalendar = datePick.getDateTime();
datePick.setMinimumDate(currentCalendar);
datePick.doModal();
Calendar cal = datePick.getDateTime();
if (cal.after(currentCalendar)) {
Date date = cal.getTime();
Dialog.alert("You selected " + date.toString());
scheduledTime = date.getTime();
} else {
Dialog.alert("Invalid date selection");
scheduledTime = 1L;
}
}
});
System.out.println("date in MilliSeconds is:" + scheduledTime);
return scheduledTime;
}
public void uploadToServer() {
} public void dialogClosed(Dialog arg0, int arg1) {
}
}
class MyCustimGridFieldManager extends VerticalFieldManager {
public MyCustimGridFieldManager() {
super(VERTICAL_SCROLL | USE_ALL_WIDTH | FIELD_HCENTER);
}
protected void paint(Graphics gr) {
super.paint(gr);
}
}
try this:
protected boolean keyChar(char c, int status, int time) {
if (c == Characters.ESCAPE) {
Ui.getUiEngine().popScreen(this);
}
return super.keyChar(c, status, time);
}
Try adding Field.EDITABLE to your style for the EditField.

BlackBerry BrowserField IllegalStateException

I'm trying to implement simple user interaction with field2.BrowserField: when button is clicked BrowserField loads
another page, but I'm getting IllegalStateException.
here's my code:
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1");
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test1();
}
});
add(btn1);
ButtonField btn2 = new ButtonField("test2");
btn2.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test2();
}
});
add(btn2);
browser = new BrowserField();
add(browser);
browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
}
private void test1() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
});
}
private void test2() {
synchronized (Application.getEventLock())
{
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
}
}
Try this one:
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1");
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//test1();
test1("http://www.blackberry.com/developers");
}
});
add(btn1);
ButtonField btn2 = new ButtonField("test2");
btn2.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//test2();
test1("http://www.blackberry.com/developers");
}
});
add(btn2);
browser = new BrowserField();
add(browser);
//browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
test1("http://stackoverflow.com/");
}
private void test1(final String url) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
//browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
browser.requestContent(url + ";deviceside=true");
}
});
}
// private void test2() {
// synchronized (Application.getEventLock())
// {
// browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
// }
// }
}
Wow, adding browser.setFocus(); do the trick
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1", ButtonField.CONSUME_CLICK);
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test1();
}
});
add(btn1);
browser = new BrowserField();
add(browser);
browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
}
private void test1() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
browser.setFocus();
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
});
}
}

Resources