TextInputLayout and EditText double hint issue

后端 未结 7 2279
生来不讨喜
生来不讨喜 2021-02-11 19:34

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

Code used for setting hint:

aET = (EditText) findViewById(R.id.

相关标签:
7条回答
  • 2021-02-11 19:48

    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?");
    
    0 讨论(0)
  • 2021-02-11 19:50

    first compile dependency compile 'com.rengwuxian.materialedittext:library:2.1.3'

    add this in your xml

    <com.rengwuxian.materialedittext.MaterialEditText    
    android:id="@+id/etId"    
    android:layout_width="match_parent"    
    android:layout_height="70dip"    
    android:hint="Hint goes here"    
    android:textColor = "#000000"    
    android:textSize="15sp"    
    app:met_accentTypeface="fonts/yourcustomfonts.ttf"   
    app:met_floatingLabel="normal"    
    app:met_floatingLabelTextColor="#ff0000"    
    app:met_floatingLabelTextSize="15sp"    
    app:met_hideUnderline="true"    
    app:met_textColorHint="#ff00ff"    
    app:met_typeface="fonts/yourcustomfont.ttf"/>
    

    add xmlns:app="http://schemas.android.com/apk/res-auto"

    0 讨论(0)
  • 2021-02-11 19:59

    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'
    
    0 讨论(0)
  • 2021-02-11 20:01

    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()

    0 讨论(0)
  • 2021-02-11 20:06

    Try to write hint in Text Input Layout

     <com.google.android.material.textfield.TextInputLayout
     style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password">
    
    0 讨论(0)
  • 2021-02-11 20:11

    Simple use this programmatically it's working for me

        TextInputLayout til = new TextInputLayout(this);
        til.setHintTextAppearance(android.R.style.TextAppearance_Large);
    

    You can customize the style as i mention below...

    <style name="TextInputLayout" parent="@android:style/TextAppearance">
            <item name="android:textColor">@color/colorPrimaryDark</item>
            <item name="android:textColorHint">@color/colorPrimaryDark</item>
            <item name="android:textSize">23sp</item>
        </style>
    
    TextInputLayout til = new TextInputLayout(this);
            til.setHint("hai);
            til.setHintTextAppearance(R.style.TextInputLayout);
    
    0 讨论(0)
提交回复
热议问题