How to change the picture of CustomButtonField on click event? - blackberry

I have posted this question previously but the answer is not appropiate. The solution provided just change the picture when the custombutton has focus and unfocus. Suppose in my application I need to change the picture if the user clicks on the customButton, n i m doing this by calling the same screen (ie UiApplication.getUiApplication().pushScreen(new Screen2(b));) . Screen2 is the screen which holds the customButton. On the click evevt i m pushing the same screen by passing aint variable pic_status that determines which picture to be drawn in the CustomButton in the new screen. Is there any way to update the picture in the CustomButtonField on click event without pushing the same Screen again and again.
//code in Screen2
public void fieldChanged(Field field, int context)
{
if(field == bf1)
{
if(pic_status == 0)
{
pic_status=1;
}
UiApplication.getUiApplication().pushScreen(new Screen2(pic_status));
}
//code in CustomButtonField
CustomButtonField(String label,int pic_status,long style)
{
super(style);
this.label = label;
this.labelHeight = getFont().getHeight();
this.labelWidth = getFont().getAdvance(label);
this.notice = s;
if(pic_status ==0)
{
currentPicture1 = onPicture;
currentPicture2 = onPicture;
}
if(pic_status ==1)
{
currentPicture1 = clickPicture;
currentPicture2 = onPicture;
}
if( pic_status==2 )
{
currentPicture1 = onPicture;
currentPicture2 = clickPicture;
}
}
I need a way to update the customButtonField text and picture on the buttonClick event not on focus/unfocus event without pushing the same Screen again and again. If my above description of problem is not satisfactory, plz add a comment n i can give more details explanation of my problem?

We can override some methods of the CustomButtonField such as protected boolean keyChar(...), protected boolean navigationClick(...), protected boolean trackwheelClick(...), protected boolean touchEvent(...), etc and use them to change button image when click or select event occurred.
For example on protected boolean touchEvent(...) we can do following task..
Replace the image with desired ones when we get TouchEvent.ClICK.
Call invalidate().
On TouchEvent.UNCLICK restore original image.
Call invalidate().

Related

Hide Keyboard in iOS by using Tabris

is there an option to remove the keyboard from screen on iOS ? I use Tabris (http://developer.eclipsesource.com/tabris/) and Java in this context.
My problem is that i use two textfields to enter a user/passwort combination. After I filled these textfields and press a button to continue the keyboard from iOS is always shown but I want that the keyboard no longer appears. Only after I click somewhere then the keyboard disappears.
On Tabris, you can "open" the keyboard programmatically by setting the Focus on a Control which uses the keyboard (like org.eclipse.swt.widgets.Text ).
To hide the keyboard, just set the Focus to a Control which does not require a keyboard, like the parent composite of your Textfield.
In your case I would add al line to the SelectionListener of your Button to set Focus on the Parent of your Textfields, then start the Login procedure.
Here is some code to play and understand the Focus mechanism:
public class FocusTest implements EntryPoint {
public int createUI() {
Display display = new Display();
Shell shell = new Shell(display, SWT.NO_TRIM);
shell.setMaximized(true);
GridLayoutFactory.fillDefaults().applyTo(shell);
createContent(shell);
shell.open();
//while (!shell.isDisposed()) {
// if (!display.readAndDispatch()) {
// display.sleep();
// }
//}
return 0;
}
private void createContent(final Composite parent) {
Button buttonSingleText = new Button(parent, SWT.PUSH);
buttonSingleText.setText("Focus on SingleText");
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(buttonSingleText);
Button buttonMultiText = new Button(parent, SWT.PUSH);
buttonMultiText.setText("Focus on MultiText");
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(buttonMultiText);
Button buttonNoFocus = new Button(parent, SWT.PUSH);
buttonNoFocus.setText("Loose Focus");
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(buttonNoFocus);
final Text singleText = new Text(parent, SWT.SINGLE);
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(singleText);
final Text multiText = new Text(parent, SWT.MULTI);
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(multiText);
buttonSingleText.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
singleText.setFocus();
}
});
buttonMultiText.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
multiText.setFocus();
}
});
buttonNoFocus.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
parent.setFocus();
}
});
}
}
Did you set UITextField delegate and add the following method there?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

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.

ListField and button focus issue in same screen

Hello all i stuck at one problem. I have implemented a ListField in one Screen. On top of the Screen i have used one HorizontalFieldManager to hold a TitleLabel and Two Butons. I have pushed some screen on all the rows of the listfield. My problem is, letSuppose when i am on 4th row and i have selected what i want then when i clicking on the button, then the button worked but the screen which i have implemented at 4th row also appear how to avoid it. I am testing it on storm 9550 simulator and using Blackberry eclipse plugin5.0 . I am running out of idea please help me .
navigation click is like this
protected boolean navigationClick(int status, int time) {
selectListAndButton();
return true;
}
//And here is the selectListAndButton Method
private selectListAndButton(){
Field field = getFieldWithFocus().getLeafFieldWithFocus();
if(field == backCustomButton){
//Popped the active Screen
}else if(field == saveCustomButton){
//Saving some Data to the Database And pushing another Screen here
// problem comes here if i am at 2nd row of the listfield and selects
something from there and clicked on the button the screen which was
implemented at 2nd row also appears
}
else if (_list.getSelectedIndex() == 0){
//Called a date picker
}
else if (_list.getSelectedIndex() == 1){
//Pushed some another screen
}
else if (_list.getSelectedIndex() == ){
//Pushed some another screen
}
You need to override onTouch for Screen and call specific functionality depend on event coordinates and Field boundaries:
protected boolean touchEvent(TouchEvent message) {
if (isTouchInside(message, saveCustomButton)) {
save();
} else {
showNextScreen();
}
}
private boolean isTouchInside(TouchEvent messages, Field field) {
int eventCode = message.getEvent();
int touchX = message.getX(1);
int touchY = message.getY(1);
XYRect rect = field.getContentRect();
return rect.contains(touchX, touchY);
}
I had solved it. but posting the answer late. I just dropped the idea of using ListField because i dont have so much rows to add. So i have just used the little trick rather using ListField i have used HorizontalFieldManager and its look like a List so everything working fine.
Thanks cheers :)

BlackBerry java detecting screen foreground event

In my BlackBerry application, I have a home screen. The user can then navigate to a settings screen. When the user goes back to the home screen, is there no method that is called on the home screen indicating that the screen has come to the foreground?
I have tried onFocus() with no avail.
Thanks!
Unfortunately, hooking on the onExposed is not enough. I found that in Blackberry dialogs are also screens and even context menus are screens too. They are pushed on top of your screen so you receive onExposed callback when they are dismissed.
Though it's OK in many cases, in other cases it poses a problem - e.g. if I must refresh the screen's content only when the user returns to it, but not after menus/dialogs, then how do I do that? My case is, unfortunately, one of those.
I found no documented way of detecting "covered"/"uncovered" events. Here is my approach. onCovered/onUncovered callbacks are called when the current screen is covered/uncovered by another screen of the app, but not by dialogs/menus/virtual keyboard:
public class MyAppScreen extends MainScreen {
private boolean isCovered;
protected void onExposed() {
Log.d("onExposed");
super.onExposed();
if (isCovered) {
onUncovered();
isCovered = false;
}
}
protected void onObscured() {
Log.d("onObscured");
super.onObscured();
final Screen above = getScreenAbove();
if (above != null) {
if (isMyAppScreen(above)) {
isCovered = true;
onCovered();
}
}
}
private boolean isMyAppScreen(final Screen above) {
return (above instanceof MyAppScreen);
}
protected void onUncovered() {
Log.d("onUncovered");
}
protected void onCovered() {
Log.d("onCovered");
}
protected void onUiEngineAttached(final boolean attached) {
if (attached) {
Log.d("UI Engine ATTACHED");
} else {
Log.d("UI Engine DETACHED");
}
super.onUiEngineAttached(attached);
}
protected void onFocusNotify(final boolean focus) {
if(focus){
Log.d("focus GAINED");
} else {
Log.d("focus LOST");
}
super.onFocusNotify(focus);
}
}
And a test. Try various combinations and see what events you receive in the log.
public class TestLifecycle extends MyAppScreen implements FieldChangeListener {
private final ABNTextEdit txt1;
private final ButtonField btn1;
private final ButtonField btn2;
public TestLifecycle() {
final Manager manager = getMainManager();
txt1 = new ABNTextEdit();
manager.add(txt1);
btn1 = new ButtonField("Dialog", ButtonField.CONSUME_CLICK);
btn1.setChangeListener(this);
manager.add(btn1);
btn2 = new ButtonField("Screen", ButtonField.CONSUME_CLICK);
btn2.setChangeListener(this);
manager.add(btn2);
}
public void fieldChanged(final Field field, final int context) {
if (field == btn1) {
Dialog.alert("Example alert");
} else if (field == btn2) {
UiApplication.getUiApplication().pushScreen(new TestLifecycle());
}
}
}
Update:
This method has a limitation: if a new screen is pushed when a dialog or the soft keyboard has focus your current screen will not receive onCovered/onUncovered notification.
Example A: if you have an input field of fixed size and you push a new screen when the user completes it, your current screen will not receive the notification if the user types very quickly. This happens because in the moment between you call push(newScreen) and it is actually pushed the user clicks on a letter on soft KB and it grabs the focus. So only onObscured is called, but not onCovered.
Solution: explicitly hide the soft keyboard before the push(newScreen).
Example B: if you have a customized dialog which pushes new screen and then dismisses itself, your current screen will not receive the notification. This happens because your customized dialog is not recognized as a screen, so only onObscured is called, but not onCovered.
Solution: dismiss the dialog in the first place returning a result value, and let your screen push the new screen based on that value. -OR- override isMyAppScreen() to return true also for your customized dialog.
You should be able to use protected void onExposed() to detect when it is displayed again.

Connect Blackberry menuitem to onscreen buttons

I have requirement for having onscreen navigation buttons along with menu items on blackberry. I need to generate menu item commands as onscreen buttons. Is there a way to generate onscreen menu item buttons in Blackberry? i.e On each screen of my application the menu items should be populated as onscreen buttons both having same functionality?
Thank you
The easiest way to accomplish what you're trying to do is write one function then have both the button and the menu item use the same function.
For example:
function doSomething() {
// Your Code Here
}
// In the function building your screen
MenuItem somethingMi = new MenuItem() {
private MenuItem() { super("Do Something",100001, 5); }
public void run() { doSomething() };
}
Button somethingBtn = new ButtonField("Do Something");
somethingBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context){
doSomething();
}
}
addMenuItem(somethingMI);
add(somethingBtn);

Resources