How To Settheme To An Activity At Runtime? It Doesn't Work Call Settheme Before Oncreate And Setcontentview
Solution 1:
Just try this - set your theme after super.onCreate(savedInstanceState); and before setContentView(...)
Like below code -
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent_NoTitleBar); // Set heresetContentView(...)
}
Solution 2:
Actually this only worked for me if I set it before calling super.onCreate(savedInstanceState);
publicvoidonCreate(Bundle savedInstanceState)
{
finalintthemeRes= getIntent().getIntExtra(EXTRA_THEME_ID, 0);
if (themeRes != 0) setTheme(themeRes);
super.onCreate(savedInstanceState);
//ect...
}
Solution 3:
setContentView(...);
setTheme(R.style.MyTheme);
setContentView(...);
It must work well..
More on Themes, Read this http://entertheinfinity.blogspot.in/2016/06/designing-android-interface-themes.html
Solution 4:
To set the theme at runtime and fix the "black background" problem:
the theme should be set before
onCreate().public void onCreate(Bundle savedInstanceState) { setTheme(android.R.style.Theme_Translucent_NoTitleBar); super.onCreate(savedInstanceState); ... setContentView(...) }the theme of the transparent activity in the android manifest should be set to any theme that has a transparent background (e.g. a dialog theme).
this tells the Android OS continue to draw the activities behind the transparent activity so that you don't end up with a black background.
i'm using an
AppCompatActivity; i need to use anAppCompattheme:<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.app"> ... <application...> ... <activityandroid:name=".TranslucentActivity"android:theme="@style/Theme.AppCompat.DayNight.Dialog".../> ... </application></manifest>
Post a Comment for "How To Settheme To An Activity At Runtime? It Doesn't Work Call Settheme Before Oncreate And Setcontentview"