Search code examples
androidandroid-edittextpositiontext-cursor

Is there any way to set position of cursor in EditText from left to right 2dp


is there any way to set position of cursor in EditText from left to right depend on px.

I use this code

EditText etEmail = (EditText) findViewById(R.id.et_email);
etEmail.setText("Email");
etEmail.setSelection(2);

it work but the Email text not remove. so I change etEmail.setText("Email") to etEmail.setHnt("Email")

like this

EditText etEmail = (EditText) findViewById(R.id.et_email);
etEmail.setHint("Email");
etEmail.setSelection(2);

but it Error.

Is there any way to set position of cursor in EditText?

this is the first load

enter image description here

this is want I want

enter image description here

<EditText
            android:id="@+id/et_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/fillbox"
            android:hint="Email"
            android:ems="10"
            android:inputType="textEmailAddress" />

Solution

  • You can set paddingLeft attribute in XML

      <EditText 
        android:layout_width="90dp"
        android:layout_height="70dp"
        android:text="BBBBB"
        android:hint="AAAAAAAAAAAAAAAAAAAA"
        android:paddingLeft="30dp"/>
    

    Or set from code

    EditText edt = (EditText)findViewById(R.id.edt);
    
            edt.setPadding(2, 0, 0, 0); //int left, int top, int right, int bottom
    

    Checked on real deviceenter image description here