Set Custom Theme Inherited From Built-in Android Theme

This example shows how you can set your own theme which is inherited from built-in themes in android.

Algorithm:

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

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="Hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The Style is inherited from build-in theme" />

</LinearLayout>

 

 

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

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyStyle"
parent="@android:style/Theme.Dialog">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</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.customthemeexample"
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.customthemeexample.CustomThemeExampleActivity"
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 CustomThemeExample and set the information as stated in the image.

Build Target: Android 4.4
Application Name: CustomThemeExample
Package Name: com.example.CustomThemeExample
Activity Name: CustomThemeExampleActivity

CustomThemeExample1

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

 

package com.example.customthemeexample;

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

public class CustomThemeExampleActivity 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.custom_theme_example, menu);
return true;
}
}

 

3.) Compile and build the project.

Output

CustomThemeExample2