Change Theme of Layout Runtime by Button Click in Android

Introduction

 
In this article, we will see how to change the layout theme of an Android application at runtime by clicking on a button.
 

What is Theme?

 
In your mobile, in the display settings, there is an option to set your whole mobile themes like a light blue color theme, red color theme or any other. This will set your full mobile functionality with this theme setting.
 
In Android, to set the theme of an Android application, you need to understand what style is.
 
Style is an XML file residing in the "project/res/values" directory. This file generally contains settings of appearances. The general structure of "style.xml" is like below:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="nameOfTheTheme">  
  4.         <item name="android:attributeSets">value</item>  
  5.     </style>  
  6. </resources>  
Here, you can see that in the style tag, there is one attribute named "name" which has the value that determines the "name of the theme", for example, "MyTheme".
 
Next is an "item" tag. This tag contains the various attribute sets of views such as "android:textColor"," android:textSize" etc. First, define this attribute and then write the value corresponding to it. Let's see an example of a style.xml file:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="MyTheme">  
  4.         <item name="android:textColor">#FF0000</item>  
  5.     </style>  
  6. </resources>  
Here, in the example, you can see that the name of the style is "MyTheme" and the item tag has the attribute "textColor" having a value "#FF0000" which is the color "Red".
 
Now to set the theme while your activity is loading, write the following code before setting your layout file in "setContentView".
  1. activity.setTheme(R.style.MyTheme);  
  2. setContentView(R.layout.main);  
This will set your application theme before loading the layout. Now you see what style is and how to set the style as the theme of your application. Now follow the steps to create this application.
 
Step 1
 
Create an Android project and name it "ChangeTheme" and then right-click on the project. Select "New -> Android XML File". This will open the following dialog box to give the name of that XML file. First select type as "Values" and give "style.xml" as the name of the file.
 
AndXML1.jpg
 
Now, open that file, and add the following code into that file:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="FirstTheme" >  
  4.         <item name="android:textColor">#FF0000</item>  
  5.     </style>  
  6.     <style name="SecondTheme" >  
  7.         <item name="android:textColor">#00FF00</item>  
  8.     </style>  
  9.     <style name="Thirdheme" >  
  10.         <item name="android:textColor">#0000F0</item>  
  11.     </style>  
  12. </resources>  
In this file, you can see that I have created 3 styles namely "FirstThem", "SecondTheme" and "ThirdTheme" with the same attribute "textColor" but with different color values.
 
Step 2
 
Now, open your "main.xml" layout file which resides in "res/layout" directory.
 
And put the following code to add 3 buttons and 1 label in it.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:id="@+id/textView1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Hello C-Sharp"  
  11.  android:textAppearance="?android:attr/textAppearanceLarge" />  
  12.     <Button  
  13.         android:id="@+id/button1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="First Theme" />  
  17.     <Button  
  18.         android:id="@+id/button2"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="Second Theme" />  
  22.     <Button  
  23.         android:id="@+id/button3"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="Third Theme" />  
  27. </LinearLayout>  
After writing this code into it, go to the "graphical" view and you can see the following screen.
 
AndXML2.jpg
 
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
  1. import android.app.Activity;    
  2. import android.os.Bundle;    
  3. import android.view.View;    
  4. import android.view.View.OnClickListener;    
  5. public class ChangeThemeActivity extends Activity implements OnClickListener    
  6. {    
  7.     /** Called when the activity is first created. */    
  8.     @Override    
  9.     public void onCreate(Bundle savedInstanceState)     
  10.     {    
  11.         super.onCreate(savedInstanceState);    
  12.         Utils.onActivityCreateSetTheme(this);    
  13.         setContentView(R.layout.main);    
  14.               
  15.                     findViewById(R.id.button1).setOnClickListener(this);    
  16.           findViewById(R.id.button2).setOnClickListener(this);    
  17.           findViewById(R.id.button3).setOnClickListener(this);    
  18.     }    
  19.      @Override    
  20.      public void onClick(View v)     
  21.      {    
  22.           // TODO Auto-generated method stub    
  23.           switch (v.getId())    
  24.           {    
  25.           case R.id.button1:    
  26.           Utils.changeToTheme(this, Utils.THEME_DEFAULT);    
  27.           break;    
  28.           case R.id.button2:    
  29.           Utils.changeToTheme(this, Utils.THEME_WHITE);    
  30.           break;    
  31.           case R.id.button3:    
  32.           Utils.changeToTheme(this, Utils.THEME_BLUE);    
  33.           break;    
  34.           }    
  35.      }    
  36. }    
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
  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. public class Utils  
  4. {  
  5.      private static int sTheme;  
  6.      public final static int THEME_DEFAULT = 0;  
  7.      public final static int THEME_WHITE = 1;  
  8.      public final static int THEME_BLUE = 2;  
  9.      /** 
  10.       * Set the theme of the Activity, and restart it by creating a new Activity of the same type. 
  11.       */  
  12.      public static void changeToTheme(Activity activity, int theme)  
  13.      {  
  14.           sTheme = theme;  
  15.           activity.finish();  
  16. activity.startActivity(new Intent(activity, activity.getClass()));  
  17.      }  
  18.      /** Set the theme of the activity, according to the configuration. */  
  19.      public static void onActivityCreateSetTheme(Activity activity)  
  20.      {  
  21.           switch (sTheme)  
  22.           {  
  23.           default:  
  24.           case THEME_DEFAULT:  
  25.               activity.setTheme(R.style.FirstTheme);  
  26.               break;  
  27.           case THEME_WHITE:  
  28.               activity.setTheme(R.style.SecondTheme);  
  29.               break;  
  30.           case THEME_BLUE:  
  31.               activity.setTheme(R.style.Thirdheme);  
  32.               break;  
  33.           }  
  34.      }  
  35. }  
Description
  1. 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:
    1. public final static int THEME_DEFAULT = 0;  
    2. public final static int THEME_WHITE = 1;  
    3. public final static int THEME_BLUE = 2;  
    Because we have 3 themes, to identify each of them, we declare 3 constants.
     
  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.
 
AndXML3.jpg
 
AndXML4.jpg
 

Summary

 
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.


Similar Articles