Is it possible to set the padding for the hint inside an EditText in Android?
Cannot seem to find any documentation in regards.
Try following code:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/email"
android:hint="Hint Text"
android:paddingStart="15dp"
/>
You cannot add padding to hint text only. but you can apply padding for both hint and text using paddingStart attribute.
android:paddingStart="16dp"
Related
I need to custom create a TextInputLayout where the EditText can have padding but the upper hint and lower error text does not share that padding like this in Android.
When I'm adding padding to the EditText inside TextInputLayout, the hint and error text also showing that padding like this.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/et_phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorText="Invalid Number" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp_54"
android:paddingEnd="0dp"
android:paddingBottom="#dimen/dp_20"
android:inputType="phone"
style="#style/editTextStyle_large"
android:hint="#string/w12_mobile_sign_in_hint"/>
</com.google.android.material.textfield.TextInputLayout>
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"/>
I want to give padding to hint of edittext in TextInputLayout
where email address is hint and Sample is text
also tried this solution but didn't work.
facing this issue after changing support design library 23.1.0
here is my code
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edtEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_round_edt_grey"
android:focusableInTouchMode="true"
android:hint="#string/emailadd"
android:padding="10dp"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
As per this answer ,if you have custom background set on EditText the android:padding attribute simple doesn't work to alter the spacing b/w the hint text and edit text .So if you have set custom background to your AppCompatEditText, you can use android:translationY attribute in the AppCompatEditText
Please add below line in your AppCompatEditText.
android:translationY="10dp"
hope you got your answer :)
I need to center the Hint text within an EditText in Android. How do I do this?
In order for centering of hint text to work with EditText you have to make sure android:ellipsize="start" is defined. I don't know why this makes it work, but it does.
Example pulled from personal code:
<EditText
android:id="#+id/player2Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="start"
android:gravity="center_horizontal"
android:hint="#string/player2_name"
android:inputType="textCapWords|textPersonName"
android:singleLine="true" />
Actually, android:gravity="center_horizontal" creates the centering effect you're looking for. Similarly, you can use android:gravity="start" to put hint text at the beginning of the EditText view, and so on.
Use this xml attribute:
android:gravity="center"
use attribute
android:gravity="center"
I used this code in every circumstances, and it works perfectly without using android:ellipsize="start" to make a hint in center.
<EditText
android:id="#+id/player2Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="robi"
android:inputType="text" />
I think the answer should be :
android:textAlignment="center"
textAlignment worked for me.
textAlignment="center"
Unfortunately, neither answer helped me aligning hint, written in LTR language, while the layout orientation was RTL. I needed such layout in a kind of translation application to make overlay icons not interfere with RTL text. But this trick didn't work with hints while there was no any text yet (icons appeared above the hint) and I came to the following java solution to layout problem:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
. . .
if (isRightToLeft()){
EditText guess = (EditText) rootView.findViewById(android.R.id.text1);
CharSequence hint = "\u200F" + guess.getHint();
guess.setHint(hint);
}
. . .
}
The trick was in right-to-left mark to prepend a left-to-right string. And it seems to work, but does anyone know a more elegant solution?
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="ENTER PIN"
android:inputType="numberPassword" />
</android.support.design.widget.TextInputLayout>
Note: in the tag TextInputEditText,the property
android:gravity="center"
is what makes the deal of aligning the text in the center including the hint text
use this: android:gravity="center"
I use this and worked for me
android:gravity="Left|center_vertical"
use android:textAlignment="center" in EditText , it work for me
This worked for me:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nombreslayoutinput">
<EditText
android:id="#+id/nombreslayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/nombres"
android:layout_centerInParent="true"
android:gravity="center|center_vertical"
android:ellipsize="start"
android:inputType="textCapWords|textPersonName"
/>
</android.support.design.widget.TextInputLayout>
The correct answer is
android:gravity="center_horizontal
hint gravity
android:textAlignment="center"
text gravity
android:gravity="left"
My problem was that the EditText wasn't big enought to fit exactly inside its parent (FrameLayout in my case), so using just android:gravity="center" (center_vertical, horizontal, start, or whatever) would just fit it inside the EditText, and not in its parent, so I had to center the EditText inside its parent using:
android:layout_gravity="center"
pd: I used android:background="#android:color/transparent" to hide the ugly underline in the EditText hint
My EditText configured as follows won't show the hint:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:hint="The hint..."
android:scrollHorizontally="true"
android:singleLine="true" />
It works if I set android:gravity="left" or if I remove android:scrollHorizontally and android:singleLine attributes, which is not desirable. Any suggestions?
using android:ellipsize="end" fixed it for me
Weird bug !! (but Android has a lot of these weirdo bug)
In Lollipop version the default text and hint text color is white for EditText.
So we have to change like this in EditText
android:textColorHint="#color/grey"
I wanted my single-line EditText box to scroll but keep the hint on the right also.
I had the same issue and got the hint to stick by keeping gravity="right", and setting singleLine="true" and ellipsize="end".
You need to give text color to hint
android:textColorHint="#000000"
No need of android:scrollHorizontally attribute. Remove it.EditText is a fixed item on the screen. we want scroll the layout contains the EditText is enough. that is the best design too. you have put android:ellipsize="end" instead of android:scrollHorizontally.
Using android:ellipsize="end" resolves the obvious platform bug. Unfortunately, Xperias still misbehave :(
I found no other solution than to:
if (android.os.Build.MANUFACTURER.matches(".*[Ss]ony.*"))
editText.setGravity(Gravity.LEFT);
else
editText.setGravity(Gravity.CENTER);
The below worked for me:
<EditText
android:id="#+id/UserText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/UserPassword"
android:layout_marginTop="85dp"
android:ems="10"
android:hint="#string/UserHint"
android:inputType="textPersonName"
android:singleLine="true"
android:text="#string/UserName" >
<requestFocus />
</EditText>
This is how, I did for by EditText to have hint in it.
<EditText
android:id="#+id/productQuantity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:hint="#string/quantity"
android:inputType="numberSigned"
android:ellipsize="end"
android:singleLine="true" >
</EditText>
Try changing the hint text color, sometimes the hint color is the same as the background color
android:textColorHint="#color/colorBlack"
I changed the style to "#style/Base.WidgetMaterialComponents.TextImputEditText". For me it wasn't gravity or color.