Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout and EditText double hint issue

I want to set the hint with java in EditText(which is in TextInputLayout).

Code used for setting hint:

aET = (EditText) findViewById(R.id.aET); aET.setHint("h?");

But even when edittext is focused, Hint is displayed twice(inside edittext also).

Please let me know if anyone had faced and found some workaround

when editText is focused...

Second

when editText is not focused..

First

EDIT[10th July 2015]:

<android.support.design.widget.TextInputLayout
     android:id="@+id/aTIL"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
     <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/aET" />
</android.support.design.widget.TextInputLayout>
like image 614
karsas Avatar asked Jul 06 '15 16:07

karsas


People also ask

How do I remove TextInputLayout error?

Now you can simply do input. setError(..) for new error and input. setErrorEnabled(false) to remove it.

How do I show error in TextInputLayout?

You can use the TextInputLayout to display error messages according to the material design guidelines using the setError and setErrorEnabled methods. In order to show the error below the EditText use: TextInputLayout til = (TextInputLayout) findViewById(R. id.

How do you set different gravity for TextInputLayout hint and text in android?

To set a different gravity for the EditText , don't set any gravity attributes in the layout, then set the EditText 's gravity programmatically, in your code. That is, after setContentView() , use findViewById() to get the EditText , then call setGravity(Gravity. CENTER_HORIZONTAL) on it.

How do I disable TextInputLayout padding?

You can just set the start and end padding on the inner EditText to 0dp. Here's a screenshot with Show Layout Bounds turned on so you can see that the hints go all the way to the edge of the view.


3 Answers

This problem occurs because the hint from the xml is passed on to the TextInputLayout, so it can be displayed as a floating hint. But when you set it programatically, the hint is set to the EditText, which then results in two hints (one from the xml that is a floating hint and one you set programatically and is a normal EditText hint). If you wish to change the floating hint, you must set the hint on TextInputLayout.

You code will then look like this:

aTIL = (TextInputLayout) findViewById(R.id.aTIL);
aTIL.setHint("h?");
like image 194
neits Avatar answered Oct 09 '22 03:10

neits


I found the solution !

In your EditText add

android:textColorHint="@android:color/transparent"

And in the code set the hint from the EditText

aET.setHint("h?");

The hint in your editText is hidden and the hint from the TextInputLayout is shown.

EDIT :

Other solution (The best)

Update Graddle with the new version of android:design

compile 'com.android.support:design:22.2.1'
like image 23
acs-team Avatar answered Oct 09 '22 04:10

acs-team


I also had this issue.

when I needed to update the hint, I (erroneously) did that on both the EditText and the TextInputLayout, which resulted in a blur.

solution was to not call EditText.setHint() but only TextInputLayout.setHint()

like image 29
axd Avatar answered Oct 09 '22 04:10

axd