26

I want setTheme to an activity at runtime, I have search some solutions by google. someone said call setTheme before onCreate and setContentView can works, the code section like

public void onCreate(Bundle savedInstanceState) {
    setTheme(android.R.style.Theme_Translucent_NoTitleBar);
    super.onCreate(savedInstanceState);
    ...
    setContentView(...)
}

but it not works, I want to know, is there another solution can setTheme to activity?

Ahmed Alnabhan
  • 608
  • 1
  • 9
  • 13
herman brain
  • 285
  • 1
  • 4
  • 6
  • 3
    Just try this - set your theme after `super.onCreate(savedInstanceState);` and before `setContentView(...)` – Praveenkumar Jun 21 '12 at 09:14
  • @hermanbrain : Always call `super.onCreate(...)` first in your `onCreate(...)` method unless you're intending to modify the `Bundle` you pass to it. As SpK says, call `setTheme(...)` after you call `super.onCreate(...)`. – Squonk Jun 21 '12 at 09:18
  • Same problem, i even tried placing it as praveen said but no success. I made a custom theme in style.xml and added the line setTheme(R.style.MyCustomTheme); do i need to do anything else? – Srujan Barai Sep 06 '15 at 02:04

4 Answers4

23

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 here
    setContentView(...)
}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
9

Actually this only worked for me if I set it before calling super.onCreate(savedInstanceState);

public void onCreate(Bundle savedInstanceState)
{
    final int themeRes = getIntent().getIntExtra(EXTRA_THEME_ID, 0);
    if (themeRes != 0) setTheme(themeRes);
    super.onCreate(savedInstanceState);
    //ect...
}
Chris.Jenkins
  • 13,051
  • 4
  • 60
  • 61
4
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

Umesh
  • 41
  • 2
  • I use this technique to change the theme on the Starting Activity of the app. It is because in the onCreate there is no View(i.e no action bar loaded or any view in activity) in which the theme can be set. Setting the theme before setContentView() is like Setting a theme to null View. My Analogy. It's always on you to decide. Thanks – Umesh Aug 11 '16 at 11:44
3

To set the theme at runtime and fix the "black background" problem:

  1. the theme should be set before onCreate().

    public void onCreate(Bundle savedInstanceState) {
        setTheme(android.R.style.Theme_Translucent_NoTitleBar);
        super.onCreate(savedInstanceState);
        ...
        setContentView(...)
    }
    
  2. 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 an AppCompat theme:

      <?xml version="1.0" encoding="utf-8"?>
      <manifest
          xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.app">
          ...
          <application
              ...>
              ...
              <activity
                  android:name=".TranslucentActivity"
                  android:theme="@style/Theme.AppCompat.DayNight.Dialog"
                  .../>
              ...
          </application>
      </manifest>
      
Eric
  • 16,397
  • 8
  • 68
  • 76
  • Thanks, setting the theme in AndroidManifest works when your third module sets the theme dynamically and the `app` module uses `Material Theme`. – rosuh Dec 02 '20 at 03:02