Sitemap

Member-only story

Change Android Theme Programmatically

1 min readAug 23, 2020

--

Press enter or click to view image in full size

Sometime we want to change theme based on menu that we click.. so to make it easy.. you can change theme programmatically? How to do it?

There is some ways to do it.. but the best what i know is by override getTheme method in Activity…

override fun getTheme(): Resources.Theme {
val theme = super.getTheme()
when (intent.getStringExtra(KEY_THEME)) {
BLUE -> {
theme.applyStyle(R.style.BlueTheme, true)
}
GREEN -> {
theme.applyStyle(R.style.GreenTheme, true)
}
ORANGE -> {
theme.applyStyle(R.style.OrangeTheme, true)
}
}
return theme
}

This is just an example when we change Theme based on data that we send to this Activity..

Befit of using this code make Fragment it also have the same Theme.. Let’s try..

I looked at all types of sustenance, but did not find a better sustenance than patience. — Umar Ibn Al Khattab

--

--

Ahmad Arif Faizin
Ahmad Arif Faizin

Written by Ahmad Arif Faizin

Curriculum Developer Lead at Dicoding Indonesia