Introduction
What is Theme?
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="nameOfTheTheme">
- <item name="android:attributeSets">value</item>
- </style>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="MyTheme">
- <item name="android:textColor">#FF0000</item>
- </style>
- </resources>
- activity.setTheme(R.style.MyTheme);
- setContentView(R.layout.main);

- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="FirstTheme" >
- <item name="android:textColor">#FF0000</item>
- </style>
- <style name="SecondTheme" >
- <item name="android:textColor">#00FF00</item>
- </style>
- <style name="Thirdheme" >
- <item name="android:textColor">#0000F0</item>
- </style>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello C-Sharp"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="First Theme" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Second Theme" />
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Third Theme" />
- </LinearLayout>
After writing this code into it, go to the "graphical" view and you can see the following screen.
Step 3
Open your activity file. In my case, it is the "ChangeThemeActivity.java" file which resides in the "src/package" directory.
To set the theme at runtime, we need to bind buttons and give "OnClickListener" to them. So to do that, write down the following code into that file.
ChangeThemeActivity.java

- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- public class ChangeThemeActivity extends Activity implements OnClickListener
- {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- Utils.onActivityCreateSetTheme(this);
- setContentView(R.layout.main);
- findViewById(R.id.button1).setOnClickListener(this);
- findViewById(R.id.button2).setOnClickListener(this);
- findViewById(R.id.button3).setOnClickListener(this);
- }
- @Override
- public void onClick(View v)
- {
- // TODO Auto-generated method stub
- switch (v.getId())
- {
- case R.id.button1:
- Utils.changeToTheme(this, Utils.THEME_DEFAULT);
- break;
- case R.id.button2:
- Utils.changeToTheme(this, Utils.THEME_WHITE);
- break;
- case R.id.button3:
- Utils.changeToTheme(this, Utils.THEME_BLUE);
- break;
- }
- }
- }
You might think about what is "Utils"? The "Utils" class has code to change themes at runtime by calling some activity methods. To create that class, right-click on the project then select "new->class". This will ask you to enter the name of that Java file. Give "Utils" as the name of that Java file. This will create a Java file in the "src" directory under your "package".
Now write the following code in the "Utils" file:
Utils.java
- import android.app.Activity;
- import android.content.Intent;
- public class Utils
- {
- private static int sTheme;
- public final static int THEME_DEFAULT = 0;
- public final static int THEME_WHITE = 1;
- public final static int THEME_BLUE = 2;
- /**
- * Set the theme of the Activity, and restart it by creating a new Activity of the same type.
- */
- public static void changeToTheme(Activity activity, int theme)
- {
- sTheme = theme;
- activity.finish();
- activity.startActivity(new Intent(activity, activity.getClass()));
- }
- /** Set the theme of the activity, according to the configuration. */
- public static void onActivityCreateSetTheme(Activity activity)
- {
- switch (sTheme)
- {
- default:
- case THEME_DEFAULT:
- activity.setTheme(R.style.FirstTheme);
- break;
- case THEME_WHITE:
- activity.setTheme(R.style.SecondTheme);
- break;
- case THEME_BLUE:
- activity.setTheme(R.style.Thirdheme);
- break;
- }
- }
- }
Description
-
changeToTheme ( Activity activity, int theme )This method has two arguments. First is "Activity", which gives this method to the identity of which the activity has called this method and of whose theme to change. And the second argument is "Theme" id which is already declared at the beginning of the class. Which is:Because we have 3 themes, to identify each of them, we declare 3 constants.
- public final static int THEME_DEFAULT = 0;
- public final static int THEME_WHITE = 1;
- public final static int THEME_BLUE = 2;
-
onActivityCreateSetTheme ( Activity activity )This method is responsible for setting the theme of the activity. After setting the "theme" id into "changeToTheme", this method will check which theme to set. Using a switch case, this will identify it. And as described at the beginning of this tutorial, it will call the "setTheme" method.
Step 4
Now, you are ready to execute this application. But before execution, be sure that you have "AVD" (Android Virtual Device) added in your eclipse. If not, then go to "Window -> AVD Manager" and add a new AVD device to it.
To start execution, right-click on your "project" and select "Run As -> Android Application". This will execute your application in the emulator.
In this brief tutorial, we discussed how to create styles and how to use it as a theme of an application and also how to change the theme of an application at runtime.



abdul rehmanPosted Apr 18, 2017, 4:56 AM
Can you please tell me how to change Activity Theme while replacing fragment...i mean activity Theme should be different for all fragment
Ad KiplPosted May 18, 2015, 9:05 AM
Hey Thats Very Nice But Can U Please Explain How to stored chosen file in preference so that theme setting will be available next time for user
Deepak DwijPosted Apr 8, 2012, 7:12 AM
Very useful features in the world of Layouts, changing theme is the wonderful concept as concern to the mobile application. Keep posting more !