7

I'm using TextInputLayout. I set it's hint from string.xml to apply localization. So after changing the language from the drop down I use recreate() method which refreshes the whole activity components with selected language resources but TextInputLayout hint doesn't get a refresh.

MikeMcC399
  • 173
  • 1
  • 10
Feroz Khan
  • 2,396
  • 5
  • 20
  • 37

2 Answers2

9

Update July 2022

Starting from material version 1.7.0 the bug should be fixed.


This is known bug of material library TextInputLayout already reported here.

PS: A possible workaround is to manually call textInputLayout.setHint(R.string.your_string) again on onRestoreInstanceState to update the text. (call it after super.onRestoreInstanceState(bundle)) or call it in onViewStateRestored

MatPag
  • 41,742
  • 14
  • 105
  • 114
  • I tried this trick but this didn't work for me. – Feroz Khan Jul 26 '21 at 12:51
  • @FerozKhan I've update the workaround, this works for sure but this depends if you have access to that lifecycle callback where you have the TIL view – MatPag Jul 26 '21 at 12:56
  • I tried to do it like this in a fragment: override fun onViewStateRestored(savedInstanceState: Bundle?) { super.onViewStateRestored(savedInstanceState) binding.etLastName.setHint(R.string.text_hint_recipient_last_name) binding.etCity.setHint(R.string.text_hint_recipient_city) } - it didn't work for me – Krushik Aug 18 '21 at 13:28
  • I don't think `onViewStateRestored` can work. You can try launching a callback from the `onRestoreInstanceState` activity, which the fragment implements with an interface (or event) to signal it to update your TIL – MatPag Aug 18 '21 at 13:43
  • @MatPag I tried this under onViewStateRestored() and it worked. I am using TextInputLayout + TextInputEditText (instead of EditText) – Shikhar Deep Dec 29 '21 at 12:00
  • Works for me in onViewStateRestored, but I don't use view binding, I stored TIL in a local var in OnCreateView. – hdort Mar 23 '22 at 12:57
  • [1.7.0](https://github.com/material-components/material-components-android/releases/tag/1.7.0) of [Material Components for Android (MDC-Android)](https://github.com/material-components/material-components-android) is released. – MikeMcC399 Oct 19 '22 at 23:48
0

Based on @MatPag answer,you could add this function in your MainActivity.

private fun findCurrentVisibleFragment(): BaseFragment? {
    val navHost = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
    return navHost?.childFragmentManager?.primaryNavigationFragment as? BaseFragment?
}

where all your Fragments extend BaseFragment. Then, your onRestoreInstanceState of your activity must be like this this:

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    (findCurrentVisibleFragment() as? YourFragment)?.setHints()
} 

where setHints() is a function in YourFragment like this:

fun setHints() {
    binding.editUserNameContainer.setHint(R.string.gen_user_name)
    binding.editPasswordContainer.setHint(R.string.gen_password)
}
Nikos M
  • 194
  • 6