TextInputLayout not showing EditText hint before user focus on it

后端 未结 16 836
时光说笑
时光说笑 2020-11-27 13:50

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 w

相关标签:
16条回答
  • 2020-11-27 13:52
       <android.support.design.widget.TextInputLayout
                android:id="@+id/editText1"
                android:layout_margin="15dp"
                android:layout_centerVertical="true"
                android:layout_width="match_parent"
    
                android:layout_height="wrap_content">
    
                  <AutoCompleteTextView
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:singleLine="true"
                    android:hint="Add City" />
    
              </android.support.design.widget.TextInputLayout>
    

    Use this code snippet.Make sure your activity is AppCompatActivity. And dependency are also with latest versions

    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
    

    Update the android studio to latest version. see output here

    0 讨论(0)
  • 2020-11-27 13:52

    I found something different.

    When setting the style for the fragment,

    f1.setStyle(DialogFragment.STYLE_NO_FRAME,android.R.style.Theme_DeviceDefault_Light);
    

    The bug was gone when I tried different Styles than "Theme_DeviceDefault_Dialog".

    Give it a try.

    0 讨论(0)
  • 2020-11-27 13:53

    I solved my problem with this code:

    new Handler().postDelayed(
            new Runnable() {
                @Override
                public void run() {
                    TextInputLayout til = (TextInputLayout) someParentView.findViewById(R.id.til);
                    til.setHint("Your Hint Text");
                    til.bringToFront();
                }
            }, 10);
    

    The key here is the bringToFront. It forces the TextInputLayout to redo its drawing, which is not the same as doing invalidate(). If you are trying to display the TextInputLayout on a view that has animations or tranistions, you need to execute the above code at the end of the animation or transition. Just make sure that the code above runs on the UI thread.

    0 讨论(0)
  • 2020-11-27 13:54

    Problem solved: Goto build.gradle of ur app and check design library. compile 'com.android.support:design:22.2.1' It should be 22.2.1 or newer.

    0 讨论(0)
  • 2020-11-27 13:55

    you should use android.support.v7.widget.AppCompatEditText as EditText and set android:hint like below

    <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <android.support.v7.widget.AppCompatEditText
                    android:id="@+id/etx_first_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"                    
                    android:inputType="text"
                    android:hint="@string/hint_first_name"/>
        </android.support.design.widget.TextInputLayout>
    
    0 讨论(0)
  • 2020-11-27 13:58

    Set hint color that contrast to the EditText background.

    <item name="android:textColorHint">#FF424242</item>

    Related:
    setHintTextColor() in EditText
    Change EditText hint color when using TextInputLayout
    How to change the floating label color of TextInputLayout

    0 讨论(0)
提交回复
热议问题