I want to cause the focus of one edit text box to move to another on editting (meaning you can only type on letter before it automatically moves on to the next edit text).
It's the "on edit" that I can't get my head around. Can anyone help me out with a simple example? Theres a lot I need to implement it into, so just a basic understanding should set the ball rolling ^_^
I do not really recommend this. With soft keyboards and multiple languages, what exactly is "one letter"? After all, a soft keyboard might enter in an entire word, like it or not.
CommonsWare makes an excellent point: you can't prevent the user from adding more characters to the EditText box, however you can listen to what's changed and act on that. Here's how to:
EditText editbox = (EditText) findViewById(R.id.MyEditBoxName);
editbox.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
// Test s for length, request focus for the next edit.
// editbox2.requestFocus();
}
});
Be careful not to get yourself into an infinite loop changing the editbox, any changes you make will cause these methods to be called again recursively.
Related
Below is an example that replicates my lagging problem. Once I set the text on the EditTextView it takes at least 1.5 seconds for the user to be allowed to input another character.
amountEditText.addTextChangedListener(new TextWatcher() {
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override public void afterTextChanged(Editable s) {}
#Override public void onTextChanged(CharSequence s, int start, int before, int count) {
String amount = s.toString();
if( !amount.equals(current )) {
amountEditText.removeTextChangedListener(this);
amountEditText.setText(s);
Selection.setSelection(amountEditText.getText(), amountEditText.getText().length());
amountEditText.addTextChangedListener(this);
}
}
});
I've searched around and have not found a solution.
I identified that the issue was coming from the textView.setText() call.
The solution was to not use setText(), instead use the Editable that is provided to you in the onTextChanged callback.
I tried to use the Editable before, however i couldn't get it working with inputs such as "$12,000".
This was due to having InputFilters still attached to the Editable.
Regards,
Scott.
It lags because you remove and then readd your listener.
You seems to be trying to select the text inside the EditText. To do so, simply set editText.setSelectAllOnFocus(true); or android:selectAllOnFocus="true" in xml. Then remove the entire TextChangedListener from your code.
According to me it is most probably because of trying to remove (amountEditText.removeTextChangedListener(this);) and add (amountEditText.addTextChangedListener(this);) the Text Change Listener every time. I would recommend you to replace your code without those adding and removal.
Hope this helped.
How can I trigger the "shift-button" on the Android keyboard, so that the next character will be capital?
I set the InputType to InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_MULTI_LINE, but this doesn't work if the cursor is anywhere in the line.
I have tried these two methods. Both don't work. The second one throws a RuntimeException ("This method cannot be called from the main application thread"). Maybe because there is no keycode for this button.
Is there a way to trigger the button?
EDIT:
It seems that buttons like shift and alt create meta states that affect pressed buttons. From https://developer.android.com/reference/android/view/KeyEvent.html:
Meta states describe the pressed state of key modifiers such as META_SHIFT_ON or META_ALT_ON.
These meta states can be simulated using dispatchKeyEvent like so:
dispatchKeyEvent(new KeyEvent(0,0,KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_L,0,KeyEvent.META_SHIFT_ON));
Were an EditText in focus this would write "L" into it. But it seems that you cannot only send the shift KeyEvent and expect the next character to be uppercase. Perhaps you can come up with some sort of workaround with this.
You're using and EditText, right? For some reason I can't get
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT));
to work when the EditText is focused while KeyEvents for letters and numbers work.
A workaround could be adding a TextWatcher to the EditText:
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged: "+s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG, "beforeTextChanged: "+s.toString());
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextChanged: "+s.toString());
}
});
and checking whether the wanted character of CharSequence s is capitalized in afterTextChanged. If not, then change it to a capitalized version. This is a bit hacky and doesn't quite answer your question I know but unfortunately I'm not able to comment.
Hi I am learning Android programming and have run into an issue that I couldn't get a clear answer to through researching.
I have a TextView which serves as a label for my EditText. I have a method which checks if the EditText is an empty String. If the string is empty I want to be able to get a reference to the TextView that corresponds to that EditText in order to make a toast saying something like "please enter a value for ".
I've looked into getLabelFor/setLabelFor but is there a way to do this in the layout XML?
What is best practice for this type of functionality.
You're describing a functionally that is build in to EditText. There is a special field you can define in xml called hint, which is the recommended way to label an EditText rather than a nearby TextView. Additionally, EditText has a method called setError() (link). If the user attempts to hit a submit button, for example, you can check to see if the EditText is empty and if so, call setError().
I wonder if the following is the thing that you need
TextWatcher inputTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
textView.setText("please enter a value for ..");
} else {
textView.setText("<the textedit is not empty>");
}
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(inputTextWatcher);
I need to control pressed buttons, before they goes to my EditText widget. It's should work like filter.
For example: I need that user could fill EditText only with digits 1,2,3,4,5 other symbols must be ignored. So the part of buttons on virtual keyboard should be disabled or I need to catch last pressed symbol, analyze it and disable for EditText.
Who knows the way how to solve this problem?
Thanks..
statusEdt.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do stuff
charTxt.setText(statusEdt.getText().length() + "/140");
}
});
I used this TextChangedListener(TextWatcher) to keep a count of how many characters had been typed into an EditText for a Twitter client I made. You could probably use a listener like this. You'll want to override beforeTextChanged or onTextChanged. These methods will pass you whatever CharSequence has been typed. You can check what was typed in and if it is not valid input you can remove it by calling setText() and passing in whatever has been typed so far minus the invalid characters.
What you probably need is an InputFilter.
For example to allow only digits, sign and decimal point:
editText.setFilters(DigistKeyListener.getInstance(true, true));
I need to implement an edittext field that just allows user input from 20 to 60. If user input a number that is out of range, a dialog will display and force user to input again.
So the text watcher is not useful because it cannot prevent user input a number that lower than 20.
The onFocusChangedListener is neither, if user clicks on 'done' button, the edittext doesn't lost focus, so the trigger doesn't fire as well.
Besides, the edittext is inside a tab view, so when user clicks on another tab, the trigger fires but user cannot input value for that edittext any more.
Alvin is right ... this is my code to do something with text once entered, but could have as easily been a validation sequence:
smsMsgBody_editText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do something fancy
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void afterTextChanged(Editable s) { }
});
How about keeping track of the EditText field with onTextChanged?
use these
android:maxLength="2" android:numeric="integer"
then when you get the number like: number.gettext() you make validations like if number>60 and lower then 20 you can do number.setHint("number no valid");