Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the cursor at end in edittext

Tags:

android

Whenever i touch the edittext, cursor should be at end.Even if the editText is already having some text and it should not allow to move left awards

 editTextView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    editTextView.onTouchEvent(event);

                    if(!textFocus) {
                        editTextView.setSelection(editTextView.getText().length());
                        textFocus = true;
                    }

                    return true;
                }
            });

            editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    textFocus = false;
                }
            });
like image 242
Devyani M Avatar asked Sep 20 '16 09:09

Devyani M


People also ask

How do I move my cursor to the end of editText?

setSelection(editText. getText(). length()); This places cursor to end of EditText.

How do I move cursor from one editText to another in Android?

How do I change the cursor position in EditText? Say in your xml's edittext section, add android:paddingLeft="100dp" This will move your start position of cursor 100dp right from left end. Same way, you can use android:paddingRight="100dp" This will move your end position of cursor 100dp left from right end.

Can you set text in editText?

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.


1 Answers

This is helped me. Always when i touch the edittext the cursor will be at the end

editTextView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    editTextView.onTouchEvent(event);
                    editTextView.setSelection(editTextView.getText().length());
                    return true;
                }
            });
like image 120
Devyani M Avatar answered Oct 19 '22 21:10

Devyani M