Applying Custom Themes In Your Application

This example shows how you can apply your own theme which is inherited from built-in themes on your application/activity in android.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it ApplyCustomTheme.

2.) Write following into main.xml:

 

<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Applying Custom Theme" />
<TextView
android:layout_width="fill_parent"
android:text="Using style of MyStyle.GreenBold defined in style.xml" />

</LinearLayout>

 

 

3.) Write following into your values/styles.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyStyle"
parent="@android:style/Theme.Light">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:textColor">#AAA</item>
<item name="android:textSize">18dip</item>
<item name="android:background">#222</item>
<item name="android:padding">5dip</item>
</style>
<style name="MyStyle.GreenBold">
<item name="android:textColor">#00FF00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>

 

4.) Write following into your manifest file:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.applycustomtheme"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyStyle.GreenBold" >
<activity
android:name="com.example.applycustomtheme.ApplyCustomThemeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

5.) Run for output.

Steps:

1.) Create a project named ApplyCustomTheme and set the information as stated in the image.

Build Target: Android 4.4
Application Name: ApplyCustomTheme
Package Name: com.example.ApplyCustomTheme
Activity Name: ApplyCustomThemeActivity

applytheme1

2.) Open ApplyCustomThemeActivity.java file and write following code there:

 

package com.example.applycustomtheme;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class ApplyCustomThemeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.apply_custom_theme, menu);
return true;
}
}

 

 

3.) Compile and build the project.

Output

applytheme2