How to create a custom theme and use it in android application

Android – How to create the custom Android theme. let’s see how to create the custom material design theme as you want for your enterprise application. you can be used you custom color as you want on your application so let’s see how to customize the application background and font color as you want.

So let’s starts first of open your colors configuration file from “res/values/colors.xml” file and change or add the color as you want. My color configuration file as below.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="colorText">#FFFFFF</color>
</resources>

Then after creating the new theme configuration file at “res/values/themes.xml” for creating new theme configuration like below.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="Theme.AppCompat.Light">

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:windowBackground">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/colorText</item>

    </style>

</resources>

The above is my theme configuration I set the “colorPrimary”, “colorPrimaryDark”, “android:windowBackground”, “android:textColorPrimary” for customize the App screen as i want.

The third step applies theme on your activity so let’s see first open “AndroidManifest.xml” file and change your custom configuration file like below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thecodingstuff.background">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:theme="@style/MyTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

As per above file, I add my custom theme on main activity like ‘ android:theme=”@style/MyTheme” ‘ You can add your custom theme on your activity for changing the app theme.

So you can customize the App background and font color as you want using the themes configuration.

If you face any issue after using this code please write the comment below.

Android – How to change the window background color

Android – How to change the activity window background color using “android:background”. there are two ways change the window background color. The first way you can change the window background color using your own theme. Follow this tutorial to create your own theme.  The second way you can change the window background color from your activity like below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="#4A148C"
    tools:context="com.thecodingstuff.first.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="The Coding Stuff"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

I just add “android:background” color on RelativeLayout you can add your custom background color as you want.

If you face any issue after using above code please write the comment below.