Friday, November 9, 2018

Create a custom Style and Theme and apply it to your Android Project.

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

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

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

</manifest>
------------------------------------------------------------------------
style.xml
---------
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="t">
        <item name="android:textColor">#ff0000</item>

    </style>
    <style name="ts" parent="@style/Theme.AppCompat.NoActionBar">
        <item name="android:textSize">25dp</item>
    </style>
    <style name="t.tc">
        <item name="android:textStyle">bold</item>
    </style>
    <style name="th" parent="@style/Theme.AppCompat.NoActionBar">
        <item name="android:textSize">25dp</item>
        <item name="android:textStyle">italic</item>
    </style>
    <style name="Custom" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="android:textColor">#ff0000</item>
        <item name="android:textSize">20sp</item>

    </style>
    <style name="Customchid" parent="Custom">
        <item name="android:textColor">#00ff0d</item>
        <item name="android:textSize">50sp</item>

    </style>
</resources>
------------------------------------------------------------------------
activity_main.xml
-----------------
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.stylentheme.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        style="@style/ts"
        android:id="@+id/textView1" />

    <TextView
        android:text="hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_alignRight="@+id/textView1"
        android:layout_alignEnd="@+id/textView1"
        android:layout_marginRight="22dp"
        android:layout_marginEnd="22dp"
        android:layout_marginTop="55dp"
        android:id="@+id/textView4"
        style="@style/t"
        />

    <TextView
        android:text="world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="27dp"
        android:id="@+id/textView5"
        style="@style/t.tc"
        />

</RelativeLayout>
------------------------------------------------------------------------
MainActivity.java
-----------------
package com.example.test.stylentheme;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.th);
        setContentView(R.layout.activity_main);
    }
}
------------------------------------------------------------------------

No comments:

Post a Comment