Home » Android » android – Padding for hint text inside EditText

android – Padding for hint text inside EditText

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to be able to add padding to the text that is displayed inside the EditText, using

android:hint="some text"

If I only add padding for the EditText regularly, it adds the padding for the whole view. And i want it for the text that gives a hint for the user, what needs to be entered. If someone knows how to do this, please give me a feedback.

enter image description here

How to&Answers:

You may find your solution in this answer:
EditText set text start 10dp from left border

It helped me to achieve the padding for hint in username field:

<EditText
 android:id="@+id/txtUsername"
 android:layout_width="match_parent"
 android:layout_height="30dp"
 android:layout_alignParentLeft="true"
 android:background="@drawable/bkg_login_txt_usuario"
 android:hint="@string/hint_username"
 android:paddingLeft="10dp"
 android:singleLine="true" >
 <requestFocus />
</EditText>

I hope this helps.

Answer:

Use the TextChangedListener and a TextWatcher to monitor the text inside the EditText. Use the TextWatcher check to see if the EditText is empty and set the padding, otherwise remove the padding.