There are two EditText
,while loading the page a text is set in the first EditText, So now cursor will be in the starting place of EditText
, I want
If you want to place the cursor in a certain position on an EditText, you can use:
yourEditText.setSelection(position);
Additionally, there is the possibility to set the initial and final position, so that you programmatically select some text, this way:
yourEditText.setSelection(startPosition, endPosition);
Please note that setting the selection might be tricky since you can place the cursor before or after a character, the image below explains how to index works in this case:
So, if you want the cursor at the end of the text, just set it to yourEditText.length()
.
if(myEditText.isSelected){
myEditText.setSelection(myEditText.length())
}
You can use like this:
if (your_edittext.getText().length() > 0 ) {
your_edittext.setSelection(your_edittext.getText().length());
}
Can you add this line to your EditText xml
android:gravity="right"
android:ellipsize="end"
android:paddingLeft="10dp"//you set this as you need
But when any Text writing you should set the paddingleft to zero
you should use this on addTextChangedListener
If you want to set the cursor after n
character from
right to left then you have to do like this.
edittext.setSelection(edittext.length()-n);
If edittext's text like
version<sub></sub>
and you want to move cursor at 6th position from right
Then it will move the cursor at-
version<sub> </sub>
^
How to resolve the cursor position issue which is automatically moving to the last position after formatting in US Mobile like (xxx) xxx-xxxx in Android.
private String oldText = "";
private int lastCursor;
private EditText mEtPhone;
@Override
public void afterTextChanged(Editable s) {
String cleanString = AppUtil.cleanPhone(s.toString());
String format = cleanString.length() < 11 ? cleanString.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3") :
cleanString.substring(0, 10).replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3");
boolean isDeleted = format.length() < oldText.length();
try {
int cur = AppUtil.getPointer(lastCursor, isDeleted ? s.toString() : format, oldText, isDeleted);
mEtPhone.setSelection(cur > 0 ? cur : format.length());
}catch (Exception e){
e.printStackTrace();
mEtPhone.setSelection(format.length());
}
mEtPhone.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
oldText = s.toString();
lastCursor = start;
}
Define the below method to any Activityclass in my case Activity name is AppUtil and access it globally
public static int getPointer(int index, String newString, String oldText, boolean isDeleted){
int diff = Math.abs(newString.length() - oldText.length());
return diff > 1 ? isDeleted ? index - diff : index + diff : isDeleted ? index : index + 1;
}
public static String cleanPhone(String phone){
if(TextUtils.isEmpty(phone))
return "";
StringBuilder sb = new StringBuilder();
for(char c : phone.toCharArray()){
if(Character.isDigit(c))
sb.append(c);
}
return sb.toString();
}
And if you want to set any specific position
edittext.setSelection(position);
some time edit text cursor donot comes at particular position is if we directly use editText.setSelection(position);
.
In that case you can try
editText.post(new Runnable() {
@Override
public void run() {
editText.setSelection(string.length());
}
});