Issue
How to change the theme by programmatically?
- Style
<resources>
<style name="Pet.Theme.Transparent" parent="AppTheme.NoBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
</resources>
- Manifest
<activity android:name="com...PetTransparentActivity" />
- Activity
class PetTransparentActivity : PetBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
if (missionArgs.isTransparent) {
Log.d("Activity", "Transparent")
setTheme(R.style.Pet_Theme_Transparent)
} else {
Log.d("Activity", "Not Transparent")
setTheme(R.style.AppTheme_NoBar)
}
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
}
setContentView(R.layout.pet_activity_transparent)
startScreenNavigation()
}
}
When I run above code, the theme not changed. But If changed the theme from manifest
, it's working.
<activity
android:name="com...PetTransparentActivity"
android:theme="@style/Pet.Theme.Transparent"/>
How to make my code work so I can change the theme from PetTransparentActivity
?
Solution
It's not best but work for me.
I fix it with create another activity that support for transparent or not transparent.
- ActivityA (Transparent using AndroidManifest.xml)
- ActivityB (Not Transparent using AndroidManifest.xml)
Both activity replace same Fragment.
Answered By - R Rifa Fauzi Komara
Answer Checked By - David Goodson (JavaFixing Volunteer)