I don't want the user to insert anything in the middle of the EditText box. I only want to allow the user to click on the EditText box to bring out the keyboard, and insert at the end of the EditText box.
Is there a way to disable the ability for the user to change the cursor position when clicking on the EditText box?
I don't mind if the answer if done in XML or Java.
Thank you :)
Edit: if I have to use setSelection, is there a way to detect when the EditText box is clicked? so that I can set the cursor back to the end when the user click on the 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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments