Android: make text and drawable inside EditText to have padding - android

There is EditText pictured on the left.
How to make text inside EditText and drawableRight to have padding as on the right?
The EditText has rounded corners, see android:background,
however there is no interval set from EditText left edge to text start and from drawableRight to the EditText right edge.
<EditText
android:id="#+id/search_editbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#id/search_execute_imageview"
android:layout_toRightOf="#id/layout_back_logo"
android:background="#drawable/rounded_edittext_states"
android:drawableRight="#drawable/abc_ic_clear_holo_light"
android:gravity="left|center_vertical"
android:hint="#string/search_hint"
android:maxLength="20"
android:padding="5dip"
android:singleLine="true"
android:textCursorDrawable="#null"
android:textSize="#dimen/main_text_size" />
I tried
android:paddingLeft="30dp"
android:paddingRight="30dp"
but it had no result.

Try this, it will work-
android:drawablePadding="30dp" for interval between drawable and text.
Edit: actually remove padding and paddingLeft and paddingRight

Related

How to change text alignment by measurement?

In my application i have a an EditText that is aligned to the left.
I would like the text in that EditText to be a bit more to the right by like 5dp or some other form of measurement that is not "left" or "center".
How to do that?
Thank you.
Use padding in EditText
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:gravity="left"/>

Is there a way to align the text in a Edit Text by dip's within AXML?

I have a EditText in my app and i would like to align the text in it a bit more to the right is there a would to do this by axml?
To move your text to right side in your editText,you can use gravity and padding.
Check this here:
<EditText
android:id="#+id/edt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:paddingLeft="10dp"
android:text="Test"
android:hint="Email"
android:singleLine="true"
android:textSize="18sp" />
Try adding android:gravity="right" to the EditText

Setting Margin between Icon and Hint of EditText

I have taken an EditText as follows :
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="This is text"
android:drawableLeft="#drawable/search"
android:layout_centerInParent="true"/>
What if I want to set some spaces between that drawable icon and hint of EditText ?
For this use the attribute drawablePadding. This adds some padding between text and image.
Use android:drawablePadding property.
eg. android:drawablePadding="5dp"
Try this
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="This is text"
android:drawableLeft="#drawable/search"
android:layout_centerInParent="true"
android:drawablePadding="10dp" />
Add These Two Lines Of Code And you are good to go your icon will not touch the border of EditText and your icon and hint has some space in between As you can see in the pictures below
<EditText
android:drawablePadding="10dp"
android:paddingLeft="15dp"/>

Center Drawable and Text in EditText

I want to make drawable and text center in my EditText. The text is centered but the drawable is not. I want something like this:
http://i.imgur.com/cacxR2C.jpg?1
Please help. This is what I have done so far
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center_horizontal"
android:drawableLeft="#drawable/lol2"
/>
When the user types in something in the EditText, the drawable should move itself to the left if the text is lenghty.
You can do something like this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#android:drawable/editbox_background"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:background="#null"
android:gravity="center"
android:drawableLeft="#drawable/ic_launcher"
/>
</RelativeLayout>
This worked for me:
<EditText
android:id="#+id/searchET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="75dp"
android:paddingEnd="75dp"
android:inputType="text"
android:singleLine="true"
android:gravity="center"
android:hint=" Username"
android:drawableStart="#drawable/your_icon"
android:background="#android:drawable/editbox_background"/>
The kicker here is that you wrap_content for the width, align the text however, center the view however you'd like, then add padding to each end of the edit text.
This shoves the drawable to the start of your text but still lets you expand your edit text background.
instead of
android:drawableLeft="#drawable/lol2"
try using
android:drawableStart="#drawable/lol2"
Basically gravity attribute is used to apply changes of aligning the contents of lay out so you can put the gravity to center to align contents in center and center_vertical to to align contents on left side and gravity to center_horizontal to put contents in horizontal center. i.e
android:gravity="center"

Gap between left drawable and text in a EditText

Hello I am setting drawable to left end in the EditText using property android:drawableLeft="#drawable/email_drawable", I am trying this it set the drawable perfectly but it is not setting gap between left drawable and text. Could you please help me to achieve this ?
<EditText
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/sign_up_edittext_vertical_top_margin"
android:drawableLeft="#drawable/email_drawable"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:singleLine="true" />
You should add android:drawablePadding attribute to your EditText. Example layout with 10dp drawable padding:
<EditText
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/sign_up_edittext_vertical_top_margin"
android:drawableLeft="#drawable/email_drawable"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:drawablePadding="10dp"
android:singleLine="true" />
to move the drawable icon left or right use android:paddingLeft or android:paddingRight do not use android:drawablePadding
When you set any drawable using android:drawableLeft / android:drawableRight property then you can add space or gap using android:drawablePadding property.
<EditText
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/sign_up_edittext_vertical_top_margin"
android:drawableLeft="#drawable/email_drawable"
android:imeOptions="actionNext"
android:drawablePadding="5dp" // set your padding or gap size here
android:inputType="textEmailAddress"
android:singleLine="true" />

Categories

Resources