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.
TextInputLayout hint doesn't get refreshed upon recreate() method call
877 Views Asked by Feroz Khan At
2
There are 2 best solutions below
0
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)
}
Update July 2022
Starting from material version
1.7.0the bug should be fixed.This is known bug of material library
TextInputLayoutalready reported here.PS: A possible workaround is to manually call
textInputLayout.setHint(R.string.your_string)again ononRestoreInstanceStateto update the text. (call it aftersuper.onRestoreInstanceState(bundle)) or call it inonViewStateRestored