How to trigger shift-button on Android keyboard? - android

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.

Related

Lag after setting text in OnTextChanged()

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.

EditText with TextWatcher and Suggestions

Moving an app from 2.2 to 3.x, one of my EditText's that I was using a TextWatcher on for validation is behaving badly. In short, when a user clicks on the EditText and the entire word goes into 'suggestions mode' (where it is underlined), it effectively gets removed from the EditText from the TextWatcher's perspective, triggering my text validation check that I do for an empty EditText. The code:
mText = (EditText) findViewById(R.id.inpt_title);
mText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable editable) {
final String title = editable.toString();
Log.d(LOG_TAG, "addTextChangedListener(): title: " + title + ", length: " + title.length());
if (title.length() == 0) {
// empty title
mText.setError(getString(R.string.error_note_title_empty));
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
mText.setTextColor(getResources().getColor(R.color.black));
}
#Override
public void onTextChanged(CharSequence s, int start, int count, int after) {}
});
I'd like to keep suggestions working, but there seems to be some weird interaction here.
Any way to either a) keep the EditText from being empty when the entire word is in 'suggestion mode', or at least checking to see if the EditText is in the 'suggestion' state to determine if the EditText is truly empty, or b) turing off suggestions? I've tried android:inputType="text|textCapWords|textNoSuggestions" for the EditText in question, as well as setting it via mText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); in the code above but suggestions keep happening on a Lenovo 3.1 tablet.
Update:
I see API 14 added a isInputMethodTarget() method to the EditText, which I could use to check for active suggestions and disable the validation... but I am running against API 12. Perhaps I could check the IME directly to see if the suggestions are active?

How to catch key pressed with the virtual keyboard and disable it for EditText in Android?

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));

Android - How can I trigger a validation for an edittext field

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");

Android EditText boxes

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.

Categories

Resources