Android Edit Text - Cursor Stays At The Starting Position
Solution 1:
Are you binding you view to a model? i.e. Do you have an MVVM setup?
If you do, make sure you do not have any cyclical update especially for a two-way binding i.e. if user types a character, you update your model which will in turn update the EditText control and repeat.
So depending on your Editext control properties, each time the model change updates it, it could have the cursor reset to the start position.
If this is the case, a simple equality check between the model value and the EditText text value before you update it from the model should fix the issue.
if (txtEdit.text != modelValue)
{
txtEdit.text = modelValue;
}
Solution 2:
It may be a bit too late to answer this question. But even i faced the same problem by using custom Keyboard. The problem was solved for me too after adding the following line in the XML file for every edittext
android:imeOptions="flagNoExtractUi"
android:inputType="textNoSuggestions"
Like this
<Editextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="2dp"android:inputType="textNoSuggestions"android:imeOptions="flagNoExtractUi"/>
I have seen a lot of people searching for this so i have posted to the one relevant to my problem.
Solution 3:
In my case, it was caused by using:
android:textAllCaps="true"
in the EditText element of my xml layout.
When I removed it, it worked as expected
Solution 4:
I am using this workaround for this problem, If characters are in English or digit characters then change EditText gravity to left else in another Language in my case it's in Arabic then will change gravity to left and the cursor will move in it's right way according to each language:
editText.addTextChangedListener(newTextWatcher() {
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0){
charc= s.charAt(0);
// characters are Enlgish or digits.if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || Character.isDigit(c)) {
editText.setGravity(Gravity.LEFT);
}else {
editText.setGravity(Gravity.RIGHT);
}
}
}
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@OverridepublicvoidafterTextChanged(Editable s) {
}
});
Solution 5:
set these properties to your XML file
<YourLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:focusable="true"android:focusableInTouchMode="true"><EditTextandroid:id="@+id/xEt"android:layout_width="fill_parent"android:layout_height="295dp"android:layout_alignParentLeft="true"android:ems="10"android:clickable="true"android:cursorVisible="true"/></YourLayout>
Try this. and put this in your java code.
EditTextet= (EditText)findViewById(R.id.xEt);
et.setSelection(et.getText().length());
Post a Comment for "Android Edit Text - Cursor Stays At The Starting Position"