I am using a custom ButtonField in my application. I have used the code from the "Blackberry Custom Button Field" blog post on Coderholic to create a custom button in my app.
Now I want to set the editable property to false for this custom button.
How do I do the equivalent of button.setEditable(false) for this custom button?
mybuttonid.setEditable(false) is not working.
Override Field.setEditable(boolean editable) to track your own custom editable boolean:
private boolean customEditable = true;
public void setEditable(boolean editable) {
super.setEditable(editable);
customEditable = editable;
// invalidate(); forces paint(Graphics graphics) to be called
}
Override navigationClick(int status, int time) to use that boolean to detect whether to react on click events:
protected boolean navigationClick(int status, int time) {
if (customEditable) fieldChangeNotify(1);
return true;
}
If you need a custom visual appearance for disabled state, then also override paint(Graphics graphics) to use another color. In this case you'll also need to call invalidate() from the setEditable().
Related
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;
}
};
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.
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.
I want to show another screen, when the user clicks in a ListField.
But my class containing ListField extends MainScreen!
How can I push to another screen?
Is there any way other than pushScreen to go to another screen?
OverRide NavigationClick ,below is the example
public boolean navigationClick(int status,int time){
Field focus = _countries.getLeafFieldWithFocus();
if(focus instanceof ListField){
ListField listField = (ListField)focus;
country = listField.getCallback().get(listField,listField.getSelectedIndex()).toString();//this will return the text clicked from the listfield
TransitionContext transition = new TransitionContext(TransitionContext.TRANSITION_FADE);
transition.setIntAttribute(TransitionContext.ATTR_DURATION,500);
transition.setIntAttribute(TransitionContext.ATTR_DIRECTION,TransitionContext.DIRECTION_DOWN);
transition.setIntAttribute(TransitionContext.ATTR_STYLE,TransitionContext.STYLE_PUSH);
UiEngineInstance engine = Ui.getUiEngineInstance();
engine.setTransition(MyApp.scr,MyScreen._radio,UiEngineInstance.TRIGGER_PUSH,transition);
MyApp.scr=null;
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
UiApplication.getUiApplication().pushScreen(MyScreen._radio);
}
return true;
}
I'm working on adding a BitmapField to my Blackberry project.
I implemented my class with a FieldChangeListener and added the FieldChangeListener method to my class. I even added a setChangeListener to that particular Bitmap Field, but it's not responding to click events.
How do I fix this?
First, BitmapField is not focusable by default, so you'll need to subclass and override isFocusable to fix that. Then override navigationclick to fire a fieldChanged event. Code snippet for a minimum field:
import net.rim.device.api.ui.component.BitmapField;
public class ClickableBitmapField extends BitmapField {
public boolean isFocusable() {
return true;
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
}
In addition to this, you may want to provide some indication of when your field is in focus (unless you only care about touch-screen devices). The default implementation will just draw a highlight on any transparent areas of your bitmap. You can change this by overriding drawFocus, and maybe onFocus and onUnfocus to change the bitmap you display when the focus state changes.