How to set keyListener for edittext in Android? - android-edittext

I want to input a Hexadecimal number in a EditText. How to set keyListener in order to limit the input value?
Thank you!

Use android:inputType="number" in your EditText xml
You can also add text watcher editText.addTextChangedListener (new MyTextWatcher ());
private class MyTextWatcher implements TextWatcher {
#Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged (Editable s) {
}
}

Related

JavaFX binding a BooleanProperty to Paint

I am writing a code that can take some boolean values from a part of some other code and change colours of certain circles on the screen accordingly. However I ran into problems trying to bind the boolean values to colours. I ended up with this:
unit1.getNeuron().getWorkingProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue == Boolean.FALSE) {
controller.paint1 = new ObservableValueBase<Paint>() {
#Override
public Paint getValue() {
return Color.RED;
}
};
} else {
controller.paint1 = new ObservableValueBase<Paint>() {
#Override
public Paint getValue() {
return Color.DODGERBLUE;
}
};
}
}
});
but I'll have to repeat it for n times for n variables I use. Is there a different way to implement this?
Let' s say you want to create an ObservableObjectValue<Paint> you want to toggle based on an ObservableBooleanValue, then Bindings is your friend:
final ObservableBooleanValue booleanCondition = unit1.getNeuron().getWorkingProperty();
final ObservableObjectValue<Paint> paintProperty = Bindings.when(booleanCondition).then(Color.RED).otherwise(Color.DODGERBLUE);

List field in blackberry , event click listener not working

I am working on BB OS v5.0. I have managed to get the list to appear on the screen. I am getting data from webservice and adding it into a Vector.
Now I want to find out onclick, which is the item that is clicked and accordingly perform some operation. For that i am trying to display an alert. But I'm not getting the alert.
Here is my code :
In my mainscreen , i added fieldmanager=new VerticalFieldManager(); and add(fieldmanager);
void fetchAlbumsForLetter(String letter) {
Status.show("Processing ....", 3000);
fieldManager.deleteAll();
VerticalFieldManager top = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setBackgroundColor(0x00290008);
graphics.setColor(Color.WHITE);
graphics.clear();
graphics.drawBitmap(0, 0, sha.getWidth(),
sha.getHeight(), sha, 0, 0);
super.paint(graphics);
}
};
add(top);
CustomListField4 list4 = new CustomListField4(null){
protected boolean navigationClick(int status, int time) {
getValue4();
return true;
}
};
fieldmanager.add(list4);
}
protected void getValue4() {
Field f = getFieldWithFocus();
if (f instanceof ListField) {
ListField l = (ListField) f;
final int index = l.getSelectedIndex();
HistoryItem _contactslist = (HistoryItem) CustomListField4.val4.elementAt(index);
final String id = _contactslist.getName();
Dialog.alert(id+"");
}
}
Please help me to resolve this
EDIT
class CustomListField4 extends ListField implements ListFieldCallback {
public CustomListField4(Vector data) {
super(0, ListField.MULTI_SELECT);
final TableRowManager row = new TableRowManager() {
public void paint(Graphics g) {
// g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0f3e19b);
g.clear();
super.paint(g);
}
};
Bitmap icon = Bitmap.getBitmapResource("Devil Skype.png");
HorizontalFieldManager h=new HorizontalFieldManager();
h.add(new BitmapField(icon));
//h.add(new BitmapField(song.getThumb()));
h.add(new LabelField(song.getAlbumName()));
//h.add(new LabelField(row1.getLanguage()));
//h.setMargin(0,0,50,0);
//Dialog.alert(song.getName());
VerticalFieldManager vfm=new VerticalFieldManager();
vfm.add(h);
//vfm.add(new LabelField(song.getArtist()));
row.add(vfm);
contacts.addElement(row);
}
setSize( contacts.size());
}
// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
listField.setRowHeight(index,107);
CustomListField4 list = (CustomListField4) listField;
TableRowManager rowManager = (TableRowManager) CustomListField4.contacts.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
public class TableRowManager extends Manager {
public TableRowManager() {
super(0);
}
You are calling getFieldWithFocus() which will give you the manager. You need to get the leaf field
protected void getValue4() {
Field f = getLeafFieldWithFocus();
if (f instanceof ListField) {
//Your code
}
}
I think your hierarchy of Field and Manager objects is incorrect, and this is causing problems with your detection of field focus/selection.
It wasn't obvious from the original code you posted, but by looking at your update, I assume that you are calling fetchAlbumsForLetter() once for every row. That's not right.
fetchAlbumsForLetter() is creating a new CustomListField4 each time it's called. And, CustomListField4 is a ListField.
A ListField is not meant to represent only one row. It's meant to represent all the rows. You should only create one instance of CustomListField4.
I would do either one of two things:
1. Continue to Use a ListField
If you want CustomListField4 to be a ListField (extends ListField), then in your implementation of
public void drawListRow(ListField listField, Graphics g, int index, int y, int width);
you should actually draw graphics objects, using all the Graphics#draw methods. These are primitive graphics items, like filled areas, lines, text, or bitmaps. You would not be using Field objects inside each ListField row, as you're trying to do with your TableRowManager class.
See here for a sample ListField, or here for a more sophisticated example
2. Imitate ListField with a Manager
change your code to
public class CustomListField4 extends VerticalFieldManager {
or
public class CustomListField4 extends Manager {
Then, you can use a TableRowManager for each row, and add LabelField or BitmapField objects to it.
See here for an example of this
If you fix these problems, then I think the way you are overriding navigationClick() will work fine for detecting the row click, and doing something with the selected row.
You can try this
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert(id+"");
}
});

BlackBerry - Set the text width of a EditField from a changeListener 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;
}
}
}

How to customize ListFieldCallBack?

i have written a class which implements ListFieldCallBack like,
import java.util.Vector;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
class ListCallBack implements ListFieldCallback
{
private Vector listelements = new Vector();
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
String text = (String)listelements.elementAt(index);
graphics.drawText(text,0,y,0,width);
}
public Object get(ListField listField, int index)
{
return listelements.elementAt(index);
}
public int indexOfList(ListField listField, String prefix, int start)
{
return listelements.indexOf(prefix, start);
}
public int getPreferredWidth(ListField listField)
{
return Graphics.getScreenWidth();
}
public void insert(String toInsert, int index)
{
listelements.addElement(toInsert);
}
public void erase()
{
listelements.removeAllElements();
}
}
And in my constructor having the main class is coded as
helloWorld()
{
mylist = new ListField();
ListCallBack myCallBack = new ListCallBack();
mylist.setCallback(myCallBack);
for(int i = 0; i<array.length;i++)//array is a string array
{
list_category.insert(i);
myCallBack.insert(array[i], i);
}
this.add(list_category);
}
this works properly..
like, i am getting output like,
Aby
Eric
Allay
vine
But i want to add another string to the next of that array in the each row displayed in list.. How could i do this?
Like, for example, i want my screen output like,
Aby : Smart
Eric : 0000
Allay : 9789
vine : Like
how could i do this?
You should change the ListFieldCallback.drawListRow(ListField listField, Graphics graphics, int index, int y, int width) to draw that.
Use net.rim.device.api.ui.Graphics API to draw whatever you want.

BlackBerry - KeyListener with global scope

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.

Resources