12

When using EditText in combination with Design lib's (ver 22.2.1) TextInputLayout getting hint programmatically returns null.

I'm trying to append asterisk '*' to a mandatory field programmatically, hence EditText.getHint() but the fact that it returns null is an issue in this case.

EditText editText = (EditText) findViewById(R.id.edit2);
String hint = String.format("%s *", editText.getHint());
editText.setHint(hint);

Asterisk overlays the hint

A simple code illustration: Layout.xml:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <EditText
            android:id="@+id/edit2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hello_world"
            android:inputType="text"/>
</android.support.design.widget.TextInputLayout>

Java:

EditText editText = (EditText) findViewById(R.id.edit2);
if (editText.getHint() == null) throw new AssertionError("Hint should not be null");

dependency: compile 'com.android.support:design:22.2.1'

Previously related issue here!

Community
  • 1
  • 1
dobridog
  • 380
  • 4
  • 12
  • 1
    There are [many hint-related issues with `TextInputLayout`](https://code.google.com/p/android/issues/list?can=1&q=TextInputLayout+hint&colspec=ID+Type+Status+Owner+Summary+Stars&cells=tiles). You might consider trying [other floating label implementations](http://android-arsenal.com/search?q=floating+label). – CommonsWare Jul 30 '15 at 13:34
  • Well issue is obviously setHint() as well as it does not reflect on the hint previously configured on the TextInputLayout – dobridog Jul 30 '15 at 13:37

5 Answers5

17

Actually the hint moves to the parent view TextInputLayout that surrounds the EditText view:

You can get the hint like this:

android.support.design.widget.TextInputLayout parent = (android.support.design.widget.TextInputLayout) yourEditText.getParent();
String hint = parent.getHint().toString();

And if you want to add * make it like this:

parent.setHint(parent.getHint() + "*");

Happy codding! :)

Siddhivinayak
  • 1,103
  • 1
  • 15
  • 26
Ivo Stoyanov
  • 16,256
  • 8
  • 62
  • 65
  • 11
    Note that if you are using `TextInputEditText` as is recommended you have to use `getParent().getParent()` to actually get the `TextInputLayout` – Bron Davies Sep 14 '16 at 18:34
1

Hint is linked with its's parent layout

((TextInputLayout)view.getParent()).getHint()

Rinkesh
  • 3,150
  • 28
  • 32
0

Simply you need to target the TextInputLayout instead of the EditText, I'm using Material Design

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/inputLayoutFirstName"
    ...>
    <androidx.appcompat.widget.AppCompatEditText
        ...
        android:hint="First Name"/>
</com.google.android.material.textfield.TextInputLayout>

So basically to change the hint try:

inputLayoutFirstName.apply {
    hint = hint.toString().plus("*")
}

You can also have this extension:

private fun TextInputLayout.addRequiredSymbolHint() {
    hint = hint.toString().plus("*")
}
Yasser AKBBACH
  • 539
  • 5
  • 7
0

I'm using in the kotlin

((edittext.parent as FrameLayout).parent as TextInputLayout).hint

and you can add that code to extension class as

fun EditText.findHint(): String {
    ((this.parent as FrameLayout).parent as TextInputLayout).hint?.let { 
        return  it.toString()
    }?: kotlin.run { return "" }
}
bebosh
  • 806
  • 10
  • 25
-1

This is fixed in design support 23.0.0 but the project has to be compiled for api 23.

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion '23'
    ....
}
dependencies{
    compile 'com.android.support:appcompat-v7:23.0.0'
...
}

Setting the hint on the EditText like before:

<android.support.design.widget.TextInputLayout
    android:id="@+id/inputField"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hello_world"
        android:inputType="text"/>

Now adding an extra character to the hint:

TextInputLayout inputField = (TextInputLayout) findViewById(R.id.inputLayout);
String hint = String.format("%s *", inputField.getHint());
inputField.setHint(hint);
Siddhivinayak
  • 1,103
  • 1
  • 15
  • 26
dobridog
  • 380
  • 4
  • 12
  • 2
    This is not fixed in 23.0.0 – superuserdo Sep 02 '15 at 13:30
  • The issue with declaring the hint in TextInputLayout is that hint value is not showing unless set programmatically (like in the example). But eventually moving the hint to edit text worked. Will update the answer, feel free to verify. – dobridog Sep 02 '15 at 14:05
  • The question's issue is about the TextInputLayout's EditText's hint returning null. I verified that this issue still exists in v 23 of the library. Even when setting the hint on the TextInputLayout, the hint still returns null. – superuserdo Sep 02 '15 at 16:27