Programmatically get hint text value in android Edittext

I have to dynamically change Typefaceof EditText, and also process the help text if Typeface has changed. I just do the following:

<android.support.design.widget.TextInputLayout
    android:id="@+id/input_layout_textBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/textBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_date" />
</android.support.design.widget.TextInputLayout>

And my code snippet looks like this:

EditText textBox = (EditText) findViewById(R.id.textBox);
Typeface tf = Typeface.createFromAsset(this, "myfont.ttf");
textBox.setTypeface(tf);
String hintText = textBox.getHint().toString(); //Got NullPointerException here....
String newText = processText(hintText); //This will process the hint text;
textBox.setHint(newText);

When I launched the program, I received the following exception:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference

Please help me solve this problem.

+4
source share
1 answer

We need to take a look at your XML first to determine this, but you are probably using the design support library.

If so, you can get your tips as follows:

((TextInputLayout)textBox.getParent()).getHint();

Note

. ( , linux , )

+1

Source: https://habr.com/ru/post/1668190/


All Articles