Home > front end >  SetTheme dose not work when is use viewBinding
SetTheme dose not work when is use viewBinding

Time:09-26

I have two themes in my app (dark and light mode) and I could change the themes without any problems. But since I used the viewBinding, the setTheme() function did not work and I can't change the activity's theme any more. Does anyone knows what's the problem?

This is my old code (which worked)

setTheme(getAppTheme());
setContentView(R.layout.my_layout);

and the new code (which does not works)

setTheme(getAppTheme());
setContentView(binding.getRoot());

Thank you for taking the time to read, and sorry for my bad English :)

CodePudding user response:

You must be initializing your binding variable before you're setting your theme. You have to initialize your activity binding after you've set the theme, for it to work correctly.

setTheme(getAppTheme());
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.getRoot());
  • Related