TextInputLayout not showing when View added programmatically

I noticed some strange behaviour of TextInputLayout:

When I add the following to my layout:

  • Can't put double SharedPreferences
  • How to check whether a user has signed-in Google+?
  • StatusBarNotification how to get data or resend intent?
  • Gradle: How to publish a Android library to local repository
  • How to preserve/rebind event listeners on MapFragment after rotating the device (portrait / landscape)?
  • Removing Ignored Android Studio (or Intellij) Update Builds
  •     <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <EditText
                android:id="@+id/txtFirstName"
                style="@style/EditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="In layout"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>
    

    everything works as expected.

    When I inflate a similar Layout like:

        View v = LayoutInflater.from(this).inflate(R.layout.edittext_w_surrounding_textinputlayout, null);
        EditText editText = (EditText) v.findViewById(R.id.editText);
        editText.setHint("Added programmatically");
    
        ViewGroup root = (ViewGroup) findViewById(R.id.root);
        root.addView(v);
    

    the TextInputLayout doesn’t appear and the EditText behaves the standard way.

    Any ideas what the reason could be?

    enter image description here

  • android ANR in MediaPlayer reset
  • Weird “Receiver not registered” exception
  • How to create something similar to Play Newsstand layout
  • How to switch between hide and view password
  • How to do HTTP authentication in android?
  • How to show an empty view with a RecyclerView?
  • 2 Solutions collect form web for “TextInputLayout not showing when View added programmatically”

    You should change hint, not on EditText, but on TextInputLayout. So it will be:

    TextInputLayout v = (TextInputLayout) LayoutInflater.from(this).inflate(R.layout.edittext_w_surrounding_textinputlayout, null);
    v.setHint("Added programmatically");
    

    TextInputLayout has it’s own hint parameter and when inflating from layout it get’s hint from it’s child EditText and set empty hint on it.

    When you want to change hint programatically you have to call textInputLayout.setHint(String text) instead of changing EditText hint

    I use this ((FrameLayout) findViewById(R.id.framePreview)).addView(preview); with no issues at all, maybe its the view type?
    should this

    ViewGroup root = (ViewGroup) findViewById(R.id.root);
    root.addView(v);
    

    not be this

    LinearLayout root = (LinearLayout) findViewById(R.id.root);
    root.addView(v);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.