Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout not showing EditText hint before user focus on it

I am using recently released Android Design Support Library to show floating label with EditTexts. But i am facing the problem that the Hint on the EditText is not showing when UI is rendered, but i see the Hint after i focus on the EditTexts.

My Layout is as follows:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">     <FrameLayout         android:layout_width="match_parent"         android:layout_height="match_parent">     <ScrollView         android:id="@+id/ScrollView01"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:scrollbars="vertical">          <LinearLayout             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:orientation="vertical"             android:paddingLeft="@dimen/activity_horizontal_margin"             android:paddingRight="@dimen/activity_horizontal_margin">              <android.support.design.widget.TextInputLayout                 android:id="@+id/name_et_textinputlayout"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:layout_marginTop="@dimen/activity_vertical_margin">                  <EditText                     android:id="@+id/FeedBackerNameET"                     android:layout_width="fill_parent"                     android:layout_height="wrap_content"                     android:hint="@string/feedbackname"                     android:inputType="textPersonName|textCapWords" />             </android.support.design.widget.TextInputLayout>              <android.support.design.widget.TextInputLayout                 android:id="@+id/email_textinputlayout"                 android:layout_width="match_parent"                 android:layout_height="wrap_content">                  <EditText                     android:id="@+id/FeedBackerEmailET"                     android:layout_width="fill_parent"                     android:layout_height="wrap_content"                     android:hint="@string/feedbackemail"                     android:inputType="textEmailAddress" />              </android.support.design.widget.TextInputLayout>              <Spinner                 android:id="@+id/SpinnerFeedbackType"                 android:layout_width="fill_parent"                 android:layout_height="48dp"                 android:layout_marginTop="@dimen/activity_vertical_margin"                 android:entries="@array/feedbacktypelist"                 android:prompt="@string/feedbacktype" />              <android.support.design.widget.TextInputLayout                 android:id="@+id/body_textinputlayout"                 android:layout_width="match_parent"                 android:layout_height="wrap_content">                  <EditText                     android:id="@+id/EditTextFeedbackBody"                     android:layout_width="fill_parent"                     android:layout_height="wrap_content"                     android:hint="@string/feedbackbody"                     android:inputType="textMultiLine|textCapSentences"                     android:lines="5" />              </android.support.design.widget.TextInputLayout>              <CheckBox                 android:id="@+id/CheckBoxFeedBackResponse"                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:layout_marginTop="@dimen/activity_vertical_margin"                 android:text="@string/feedbackresponse" />              <Button                 android:id="@+id/ButtonSendFeedback"                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:layout_marginTop="@dimen/activity_vertical_margin"                 android:text="@string/feedbackbutton" />         </LinearLayout>     </ScrollView>  </FrameLayout>  <View     android:layout_width="match_parent"     android:layout_height="5dp"     android:background="@drawable/toolbar_shadow" /> </FrameLayout> 

I have also tried to set hint for the TextInputLayout using method setHint but no luck.

mNameTextInputLayout = (TextInputLayout) v.findViewById(R.id.name_et_textinputlayout); mNameTextInputLayout.setErrorEnabled(true); mNameTextInputLayout.setHint(feedBackerNameET.getHint());  mEmailTextInputLayout = (TextInputLayout) v.findViewById(R.id.email_textinputlayout); mEmailTextInputLayout.setErrorEnabled(true); mEmailTextInputLayout.setHint(feedBackerEmail.getHint());  mBodyTextInputLayout = (TextInputLayout) v.findViewById(R.id.body_textinputlayout); mBodyTextInputLayout.setErrorEnabled(true); mBodyTextInputLayout.setHint(feedBackBody.getHint()); 
like image 839
Shajeel Afzal Avatar asked May 29 '15 19:05

Shajeel Afzal


People also ask

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 get TextInputLayout from text?

Just use: TextInputLayout textInputLayout = findViewById(R. id. custom_end_icon); String text = textInputLayout.

How do you make TextInputLayout clickable?

Set TextInputLayout android:clickable="true" . Set TextInputEditText android:clickable="true" and android:focusable="false" .


Video Answer


2 Answers

Update:

This is a bug that has been fixed in version 22.2.1 of the library.

Original Answer:

As mentioned by @shkschneider, this is a known bug. Github user @ljubisa987 recently posted a Gist for a workaround:

https://gist.github.com/ljubisa987/e33cd5597da07172c55d

As noted in the comments, the workaround only works on Android Lollipop and older. It does not work on the Android M Preview.

like image 114
Dave Jensen Avatar answered Oct 05 '22 17:10

Dave Jensen


That is a known bug of the Android Design library. It has been accepted and assigned.

So you should have this fixed in the next Android Design library release.

In the meantime, you could watch the issue tracker for a hacky-fix that might get posted there, but I don't know any as of now.

And yes, it only affect Lollipop and above.

like image 21
shkschneider Avatar answered Oct 05 '22 18:10

shkschneider