How to set onKeyEnter event for editField in blackberry? - blackberry

How to set onKeyEnter event for EditField. I have two EditFieldin my current screen. But i have to set event only one EditField .How can I set that?. I have Button field and list field in the same screen. I can able to set click even for both button and list.But problem is setting event for EditField.
I have used both key down and keychar method. But that not use to me.
I checked with following method.
protected boolean keyChar(char key, int status, int time){
if (key == Characters.ENTER){Dialog.alert("hi");}
return false;
}
protected boolean keyChar(char character, int status, int time){
if (Characters.ENTER == character){Dialog.alert("hi");}
return false;
}
If i am using like this I can't enter any character in both my editfield.
Can anyone help me?

If i am using like this I can't enter any character in both my editfield.
You're eating all the keystrokes because you're not returning super.keyChar(key, status, time).
If you don't consume the key, you need to pass it on up.

Related

Handling Touch EventListener in presence of navigationClick()

In application, Whenever i move bewteen tabs a full menu, copy, switch application pop up open.
To prevent this i override navigationClick method.
Now pop is not shown. In my second tab i have implemented drop down list. Because i have override navigationClick method when i click on drop down list it is not showing me list of items.
Any idea will be apperciated.
Update:
Here is navigationClick implementation:
public boolean navigationClick(int status, int time) {
return true;
}
Make sure while selecting a tab, no errors occur which probably could be the reason for the menu options to pop up. if this is fixed i guess the subsequent problem will be dissolved!
Out of my experience i have observed the menu pops up in blackberry when a NullPointerException event occurs.
Just check the logs or debug to make sure no exception occurs when you navigate between tabs
If you want to click the labelfield then:
LabelField labelField=new LabelField("Click")
{
protected boolean navigationClick(int status, int time)
{
doLabelFieldClicked();
return true;
}
}
and if Menu option is highlighted then override this:
public boolean onMenu(int instance)
{
return true; //It doesn't show the Menu option.
}
If I am thinking wrong let me know.
I solved this problem by implementing navigationClick() while creating lablelField
tab5 = new LabelField("News", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_SELECT)
{
public boolean navigationClick(int status, int time)
{
return true;
}
};

Blackberry Custom Image button

i have created a custom image button; one is play image button and one is pause image button.
i want to switch between play and pause (i.e when the user clicks on the button,it has to change - play to pause or pause to play).
for my requirement, i have referred to this example url
when i execute my program, i am able to display the button, but when i click on the button, i am not able to change the imagebutton (play to pause)
can any one help me?
The example that you are working off of uses two images, the on and the off image. However, it uses these two images to change the look of the button based on whether the button has focus or not.
You want something a little different. You want to change the look of the button whenever the button gets clicked. To do that, you can use this existing method:
protected boolean navigationClick(int status, int time){
fieldChangeNotify(1);
return true;
}
To do this, change (or add) some member variables to store the play and pause bitmaps, and then change navigationClick() to toggle between them:
private Bitmap _currentPicture;
private Bitmap _playPicture;
private Bitmap _pausePicture;
protected boolean navigationClick(int status, int time){
if (_currentPicture == _playPicture) {
_currentPicture = _pausePicture;
} else {
_currentPicture = _playPicture;
}
invalidate(); // may be necessary to force redraw of the button
fieldChangeNotify(1);
return true;
}
Edit: you also might want to perform the same logic (as above in navigationClick()) in the trackwheelClick() and keyChar() methods, depending on whether your buttons should be clickable by using the trackwheel or enter keys, too. See an example of that here

how to disable the button using bitmapbuttonfield in advanced ui examples in blackberry

I am using bitmap button field in advanced ui examples.
By default no method is working for disabling the button in 5.0 jre,
so i have added the below code for disabling and
then disabling functionality of the button is working but setchangelistener is not working
that is my problem
.. here is my code that i added for disabling the button..please check. do i need to change anything in invoke action method?
public boolean isDisable() {
return isDisable;
}
public void setDisable(boolean isDisable) {
this.isDisable = isDisable;
invalidate();
}
public boolean isFocusable() {
return isFocusable && !isDisable;
}
public void setFocusable(boolean isFocusable) {
this.isFocusable = isFocusable;
}
protected boolean invokeAction(int action) {
if (!isDisable){
fieldChangeNotify(0);
}
return true;
}
public boolean setEnabled() {
return false;
}
Here is a discussion on the BlackBerry forums about this.
What I've sometimes done is actually make use of the isEditable() property on Field objects, since editability and being enabled are somewhat similar concepts. If you really want to keep the separate isDisabled() code, that's fine. Just substitute that below where I use isEditable() (remembering to reverse the boolean ... that's one reason to always program in the affirmative ... make your method isEnabled() instead of isDisabled()).
So, instead of any of the code you posted above, I would just add this code to either BitmapButtonField, or BaseButtonField:
public boolean isFocusable() {
return isEditable() && super.isFocusable();
}
and this in BitmapButtonField:
protected void paint( Graphics g ) {
int oldAlpha = g.getGlobalAlpha();
int index = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? FOCUS : NORMAL;
if (!isEditable()) {
g.setGlobalAlpha(100); // alpha is 0 to 255, so this is 100/255
}
g.drawBitmap( 0, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
g.setGlobalAlpha(oldAlpha);
}
And then, I can setup a change listener, or disable the button, like this in my manager class:
BitmapButtonField btn =
new BitmapButtonField(Bitmap.getBitmapResource("button.png"),
Bitmap.getBitmapResource("button-lit.png"));
btn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Button clicked!");
}
});
btn.setEditable(false); // this disables the button
add(btn);
But, understand, that if you disable a button, that means your change listener won't get called. That's kind of how it's supposed to work. The change listener is only called if the button's enabled and therefore clickable.
Also, note that in order to make the button look different when disabled (not editable), I override paint() to set a different alpha value when the button is disabled. You didn't mention that, so if you don't like it, you can certainly take it out.

Custom List Field with Custom ObjectChoiceField in blackberry?

I am a new BB developer working on android simultaneously, I am preparing a project containing list and object choice fields in both the languages.
In blackberry, I have a list of 10 elements and on each element row I need to show an arrow button on click of that button a ObjectChoiceField will pops up.
Here, it is clear that the list and obejctchoice field both are custom.
I am very confused please let me know, how this can be achieved.
Thanx in advance..!:|
// customize ListField
How to customize a ListField in BlackBerry?
To get clicked item just add below function to code
protected boolean navigationClick(int status, int time) {
Field field = this.getLeafFieldWithFocus();
if(field instanceof ListField){
System.out.println("navigationClick Yes Yes you clicked list item ");
ListField listField = (ListField)field;
// this will give you clicked index.
int index = listField.getSelectedIndex();
// pop up your Object choice field here...
}
return true;
}

Detect spacebar event

The spacebar has a default action on many UI fields: click for buttonfield, select/unselect for checkbox, scrolling on a verticalfieldmanager.
My screen has a listfield with more than 20 rows. When the user hits the spacebar, I want the listfield to scroll.
For example, BlackBerry default calendar app, when we hit spacebar, it will scroll down.
and BlackBerry default text Messages, when we hit spacebar, it will scroll down.
is this a default property? or do I need to write code for spacebar key?
You have to create a custom key listener:
private class CustomKeyListener implements KeyListener {
public boolean keyChar(char key, int status, int time) {
if(key == Characters.SPACE){
//TODO handle key here
//WARNING: this code runs on event thread!
return true;
}
return false;
}
public boolean keyDown(int keycode, int time) {
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 call Mainscreen.addKeyListener with an instance of your key listener as parameter.
From there you can change your manager (main manager or nested one) scroll with the Manager.setVerticalScroll method. If you want to increment it, you can retrieve the current scroll calling Manager.getVerticalScroll and then add a fixed value. In case you don't have a nested VerticalFieldManager in your screen, you can try with your screen's default, which you can obtain calling Mainscreen.getMainManager.
UPDATE:
For List fields, you can call ListField.getSelectedIndex and ListField.setSelectedIndex to change elements, but this is not an smooth scrolling. But if you placed the list field inside a VerticalFieldManager, you can change the manager scroll as described above.

Resources