How to have fixed cursor position for EditText?

Jeremy Yang

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.

Anuj Sharma

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to have fixed position of element but relative to container

From Dev

Set cursor position in edittext android

From Dev

Set cursor position in edittext android

From Dev

How to move editText cursor?

From Dev

How do I make the child-div to have a fixed position?

From Java

How can I have a position: fixed; behaviour for a flexbox sized element?

From Dev

How can I have a position:fixed after a scroll event?

From Dev

have an issue with position fixed.

From Dev

Android IME, set cursor position in EditText

From Dev

EditText cursor in top left position.

From Dev

Can't control cursor position in edittext

From Dev

set cursor position of edittext in center...!

From Dev

Get word from EditText on current cursor position

From Dev

set cursor position of edittext in center...!

From Dev

Cursor at wrong position when resizing EditText with animateLayoutChanges

From Dev

How can i get last(just before new line starts) cursor position of current line in EditText?

From Dev

How to position container to fixed position?

From Dev

How to position the cursor in AutoCompleteTextView

From Dev

How to animate to position fixed?

From Java

How to move cursor in EditText in Android

From Dev

How to add blinking cursor to editText

From Dev

How to set the cursor to the right (EditText)?

From Dev

Scrolling with a button which should have a fixed position

From Dev

How do I rotate an image based on my cursor position, and have it work?

From Dev

How do I rotate an image based on my cursor position, and have it work?

From Dev

Saving and restoring EditText cursor position in a DialogFragment using savedInstanceState

From Dev

How can I have a div/span "fill in" the remaining width/height of the page, and stay fixed in position?

From Dev

ASTRewrite for a QuickFix: How to position the cursor?

From Dev

How to set menu cursor position?

Related Related

  1. 1

    How to have fixed position of element but relative to container

  2. 2

    Set cursor position in edittext android

  3. 3

    Set cursor position in edittext android

  4. 4

    How to move editText cursor?

  5. 5

    How do I make the child-div to have a fixed position?

  6. 6

    How can I have a position: fixed; behaviour for a flexbox sized element?

  7. 7

    How can I have a position:fixed after a scroll event?

  8. 8

    have an issue with position fixed.

  9. 9

    Android IME, set cursor position in EditText

  10. 10

    EditText cursor in top left position.

  11. 11

    Can't control cursor position in edittext

  12. 12

    set cursor position of edittext in center...!

  13. 13

    Get word from EditText on current cursor position

  14. 14

    set cursor position of edittext in center...!

  15. 15

    Cursor at wrong position when resizing EditText with animateLayoutChanges

  16. 16

    How can i get last(just before new line starts) cursor position of current line in EditText?

  17. 17

    How to position container to fixed position?

  18. 18

    How to position the cursor in AutoCompleteTextView

  19. 19

    How to animate to position fixed?

  20. 20

    How to move cursor in EditText in Android

  21. 21

    How to add blinking cursor to editText

  22. 22

    How to set the cursor to the right (EditText)?

  23. 23

    Scrolling with a button which should have a fixed position

  24. 24

    How do I rotate an image based on my cursor position, and have it work?

  25. 25

    How do I rotate an image based on my cursor position, and have it work?

  26. 26

    Saving and restoring EditText cursor position in a DialogFragment using savedInstanceState

  27. 27

    How can I have a div/span "fill in" the remaining width/height of the page, and stay fixed in position?

  28. 28

    ASTRewrite for a QuickFix: How to position the cursor?

  29. 29

    How to set menu cursor position?

HotTag

Archive