4

I found crash in crashlog of "NullPointerException: Attempt to invoke virtual method 'void android.view.View.getBoundsOnScreen(android.graphics.Rect)' on a null object reference" and I verified some stackoverflow suggestions for this crash. I found below link suggest to use hint in TextInputLayout instead of TextInputEditText, so I want to know what makes it different?

Below link suggests to use hint in TextInputLayout: SO Google shows to use hint in TextInputEditText: Google_Recommendation

I tried hint in both place of TextInputLayout and TextInputEditText. Hint worked well in both place similarly.

I would like to know what is the difference between both method and which one is more appropriate to use.

Navinpd
  • 772
  • 7
  • 15

1 Answers1

9

In design support library TextInputLayout was introduced to display the floating label on EditText or TextInputEditText . The EditText or TextInputEditText has to be wrapped by TextInputLayout in order to display the floating label.

Rule of Thumb : TextInputLayout should wrap TextInputEditText

where should you place hint

according to the official documentation

The hint should be set on the TextInputLayout, rather than the EditText. If a hint is specified on the child EditText in XML, the TextInputLayout might still work correctly; TextInputLayout will use the EditText's hint as its floating label. However, future calls to modify the hint will not update TextInputLayout's hint. To avoid unintended behavior, call setHint(CharSequence) and getHint() on TextInputLayout, instead of on EditText.

Note that TextInputEditText is special sub-class of EditText designed for use as a child of TextInputLayout.

hope it's helpful

ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
  • Thanks for your response. What if I have **TextInputEditText** inside **TextInputLayout**? – Navinpd May 30 '19 at 06:10
  • 1
    Its like you have EditText since textInputEdittext is subclasd of edit text so you apply the same rules on TextInputEdittext – ismail alaoui May 30 '19 at 06:13
  • I notice if im using the android studio layout editor to drop in a TextInputLayout element it is putting the hint in the nested TextInputEditText. Is the editor doing it wrong or is that the correct placement? – Dan N. Jan 05 '20 at 16:30