Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a theme to an activity in Android?

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?

like image 803
Willy Avatar asked Oct 01 '13 20:10

Willy


2 Answers

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.

like image 137
Paresh Mayani Avatar answered Oct 05 '22 01:10

Paresh Mayani


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.

like image 43
live-love Avatar answered Oct 05 '22 03:10

live-love