-1

The goal is to set cursor position of an EditText to 2, using setSelection(int). Here's the excerpt from my RegisterActivity.java:

 UserEmail = (EditText) findViewById(R.id.register_email);
 UserEmail.setSelection(2); //cursor position

Here's the activity_register.xml:

<EditText
        android:id="@+id/register_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:inputType="textEmailAddress"
        android:background="@drawable/ab_transparent_example"
        android:padding="4dp"
        android:drawableStart="@drawable/ic_mail_outline_gray_24dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="17dp"
        android:layout_marginRight="17dp"/>

The app crashes when I use an int > 0 in setSelection(int), such as 2 in the code above.

Is there an alternative to that method in the java file? Is there a workaround inside the xml itself?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 4
    *`App crashes when defining cursor position...`* Where is your Crash Log, you need to share it with question mean while Have a look **[Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this)** – AskNilesh Jan 08 '19 at 10:40

2 Answers2

3

If your edittext is empty, it will crash because you are trying to set the cursor at a position greater than the length of the string inside the edittext.

First, make sure the string inside has a greater length than the position.

Example:

String text = yourEditText.getText().toString();

if (desirablePosition < text.length()){
  yourEditText.setSelection(desirablePosition);
}
Daniel B.
  • 2,491
  • 2
  • 12
  • 23
  • Got it. Is there a way to define the position of an empty EditText, then? –  Jan 08 '19 at 10:42
  • @TheTourist on an empty EditText, use setSelection(0), otherwise it will crash. – Daniel B. Jan 08 '19 at 10:44
  • Ok. Since it's an email field, I need it to be empty when the app starts; perhaps filled only by a hint in the xml, such as "example@gmail.com". The problem I'm facing is that the cursor is right after the email icon drawable, making the spacing poor. What would you suggest to fix this? –  Jan 08 '19 at 10:56
  • @TheTourist instead of messing with the selection, I would suggest finding a way to set the margin of the drawable. – Daniel B. Jan 08 '19 at 11:00
  • Yes, that seems reasonable. Thanks for the help. –  Jan 08 '19 at 11:03
  • @TheTourist also note, that even if you were able somehow to set the selection itself further away from the drawable, when the user starts typing, the text would still be too close to the drawable, so the margin suggestion from the comment above is a way better solution. – Daniel B. Jan 08 '19 at 11:03
0

Set default text to the edit text which is greater than 2.. to work set selection > 0, your edit text should have content.

Ramesh Yankati
  • 1,197
  • 9
  • 13