How to change where user input starts typing

Viewed 81

I currently have a box to type the username and password in with "username" and "password" as hints. Where the words "username" and "password" starts is where I want the user input to start. When a user starts typing that is where I want the text to start. Currently, it is pushing the text over to the beginning of the box and is cutting part of the text off. How can I change where the text starts?

<!-- Username text -->
    <EditText
        android:id="@+id/username"
        android:layout_width="329dp"
        android:layout_height="37dp"
        android:layout_marginBottom="160dp"
        android:background="@drawable/text_input_rounded_button"
        android:clickable="true"
        android:ems="10"
        android:fontFamily="@font/days_one"
        android:hint="   Username"
        android:inputType="textPersonName"
        android:singleLine="true"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        android:textColor="#80000000"
        android:textSize="12sp"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/loginButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

as you can see from the image, I want the "dbullard417" to start more to the right so it's not so close to the edge but I don't want it centered.

3 Answers

If you just want your cursor to be a little shifted towards the right, you can add padding to the view.

You can use this to add padding only in the start:

android:paddingStart="15dp"

Or You can use this to add padding in all the directions:

android:padding="15dp"

I you want the cursor on TextView to start on a specific position, use setSelection() method on TextView as -

//will take integer value to start position on textView for cursor.
textView.setSelection(<integer value>);

I was not able to understand the desired result you are looking for but following example might help you to get your result -

String userNameSample = "Text";
username.setText(userNameSample );
username.setSelection(userNameSample.length());

enter image description here

Happy Coding !

try this code :

<EditText
  android:id="@+id/edt_username"
  android:inputType="textEmailAddress"
  android:textSize="@dimen/_12sdp"
  android:maxLines="1"
  android:text="test"
  android:hint="Username"/>
Related