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.
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();
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With