Place Cursor at the End of Text in Edittext

android how to set the EditText Cursor to the end of its text

You have two options, both should work:

a)

editText.setText("Your text");
editText.setSelection(editText.getText().length());

b)

editText.setText("");    
editText.append("Your text");

Move cursor to end of EditText?

One trick I used was setting the text to "" and then appending the entire string I wanted.

i.e.

String newtext = editText.getText().toString() + "the new text";
editText.setText("");
editText.append(newtext);

This seems to place the cursor in the right place for me.

MVVM Set Cursor in end of text in EditText when using afterTextChanged fuction

you could a BindingAdapter for it

@BindingAdapter("cursorPosition")
@JvmStatic
fun setCursorPosition(editText: EditText, value: String?) {
value ?: return
editText.setSelection(value.length)
}

and in your layout use is as

cursorPosition="@{viewmodel.amount}"

Edittext place cursor at the end of a Hint

There is a trick.First you get hint string and hint color then you set EditText's text (value equals hint string) and do not forget set textcolor (values hint color).When user clicks the EditText, you can change the color back while reset empty text.

    final EditText editText = (EditText) findViewById(R.id.your_id);
String hint = editText.getHint().toString();
ColorStateList hintTextColor = editText.getHintTextColors();
final ColorStateList textColor = editText.getTextColors();
editText.setTextColor(hintTextColor);
editText.setText(hint);
editText.setSelection(hint.length());

editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setTextColor(textColor);
editText.setText("");
}
});

How to have fixed cursor position for EditText?

You can apply this trick, This worked for me.
Just disable the cursor from edittext and on its click listener add your code that will keep the cursor on right most.

Here is code that i tried.

<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="12"
android:background="@color/white"
android:padding="@dimen/activity_margin_10"
/>

Java Code

EditText mEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

mEditText = (EditText)findViewById(R.id.edit); // Initialzation of Edittext
mEditText.setSelection(mEditText.getText().length()); // After initialization keep cursor on right side
mEditText.setCursorVisible(false); // Disable the cursor.

/*
Add Click listener and on put your code that will keep cursor on right side
*/
mEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setSelection(mEditText.getText().length());
}
});
}

Hope it will help you.

How to set cursor position in EditText?

Where position is an int:

editText1.setSelection(position)


Related Topics



Leave a reply



Submit