96

I know how to apply a theme to a whole application, but where would I go to apply a theme to just a single activity?

Squonk
  • 48,735
  • 19
  • 103
  • 135
Willy
  • 1,055
  • 2
  • 8
  • 6

3 Answers3

173

You can apply a theme to any activity by including android:theme inside <activity> inside manifest file.

For example:

  1. <activity android:theme="@android:style/Theme.Dialog">
  2. <activity android:theme="@style/CustomTheme">

And if you want to set theme programatically then use setTheme() before calling setContentView() and super.onCreate() method inside onCreate() method.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
37

To set it programmatically in Activity.java:

public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setTheme(R.style.MyTheme); // (for Custom theme)
  setTheme(android.R.style.Theme_Holo); // (for Android Built In Theme)

  this.setContentView(R.layout.myactivity);

To set in Application scope in Manifest.xml (all activities):

 <application
    android:theme="@android:style/Theme.Holo"
    android:theme="@style/MyTheme">

To set in Activity scope in Manifest.xml (single activity):

  <activity
    android:theme="@android:style/Theme.Holo"
    android:theme="@style/MyTheme">

To build a custom theme, you will have to declare theme in themes.xml file, and set styles in styles.xml file.

bcorso
  • 45,608
  • 10
  • 63
  • 75
live-love
  • 48,840
  • 22
  • 240
  • 204
  • 1
    What about disable theme? on a single activity – Yousha Aleayoub Sep 21 '15 at 10:17
  • 2
    Why have you added two `android:theme` attributes? – Flame of udun Oct 18 '15 at 23:11
  • @Vineet Kaushik, `android:theme="@android:style/Theme.Holo"` is the syntax for adding an Android built-in theme. `android:theme="@style/MyTheme"` is the syntax for adding a custom theme described in your `styles.xml` file. In your actual `AndroidManifest.xml` file you would only use one or the other for each section, not both. – Soren Stoutner Jun 09 '16 at 17:21
  • 1
    @Yousha Aleayoub, to disable the theme, create a blank theme in `styles.xml` and then use the syntax `android:theme=@style/MyBlankTheme`. – Soren Stoutner Jun 09 '16 at 17:23
  • It seems putting more than one custom theme in the manifest don't work. If you add a theme at application level and a second at activity level, only the application one is used. I tried to add one theme for each activity with different "look" but without good result. – Peter Sep 26 '16 at 14:06
8

Before you call setContentView(), call setTheme(android.R.style...) and just replace the ... with the theme that you want(Theme, Theme_NoTitleBar, etc.).

Or if your theme is a custom theme, then replace the entire thing, so you get setTheme(yourThemesResouceId)

jcw
  • 5,132
  • 7
  • 45
  • 54