Jul 5, 2015

Create custom text style

Example to create custom text style:


Modify /res/values/styles.xml to create our custom text style "LargeRedText", "InverseMediumBlueText", "GreenText", "ItalicGrayText" and "Bold50BlackText".
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    
    <style name="LargeRedText" parent="@android:style/TextAppearance.Large">
        <item name="android:textColor">#FF0000</item>
    </style>
    <style name="InverseMediumBlueText" parent="@android:style/TextAppearance.Medium.Inverse">
        <item name="android:background">#0000FF</item>
    </style>
    <style name="GreenText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#00FF00</item>
    </style>
    <style name="ItalicGrayText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#A0A0A0</item>
        <item name="android:textStyle">italic</item>
    </style>
    <style name="Bold50BlackText">
        <item name="android:textColor">#000000</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">50dp</item>
    </style>

</resources>


Example to use our custom text style in layout xml.
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.androidtextappearance.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="24dp"
        android:textStyle="bold" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearance"
        android:textAppearance="?android:textAppearance" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceLarge"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceMedium"
        android:textAppearance="?android:textAppearanceMedium" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="textAppearanceSmall"
        android:textAppearance="?android:textAppearanceSmall" />
    
 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style LargeRedText"
        style="@style/LargeRedText" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style InverseMediumBlueText"
        style="@style/InverseMediumBlueText" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style GreenText"
        style="@style/GreenText" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style ItalicGrayText"
        style="@style/ItalicGrayText" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom style Bold50BlackText"
        style="@style/Bold50BlackText" />
    
</LinearLayout>


No comments:

Post a Comment

Infolinks In Text Ads