Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get hint text value in android Edittext [duplicate]

Tags:

java

android

I have to change theTypeface of EditText dynamically and also process the hint text if the Typeface changed. I simply do as 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 is as follow:

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 runt the program, I got 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 to solve this problem.

like image 468
Thiha Zaw Avatar asked Jan 29 '17 15:01

Thiha Zaw


1 Answers

We need to look at your_layout.xml first to determine that, but you're probably using the design support lib.

If so, you can get your hints as follows:

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

Note

This has already been answered here. (Although we needed to make sure that you were using the design support lin to mark your question as duplicate)

like image 166
Anis LOUNIS Avatar answered Nov 10 '22 14:11

Anis LOUNIS