TextInputLayout hint doesn't get refreshed upon recreate() method call

877 views Asked by At

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.

2

There are 2 answers

7
MatPag On BEST ANSWER

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

0
Nikos M On

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)
}