Wednesday, July 20, 2016

Create styling string from resources

This example show how to create partial styling string from resources:


Edit values/strings.xml to add string resources with styling:
<resources>
    <string name="app_name">AndroidStylingStringResources</string>
    <string name="normal_name">It is Normal String</string>
    <string name="italic_name">Partial <i>Italic String</i> from resources</string>
    <string name="bold_name">Partial <b>Bold String</b> from resources</string>
    <string name="underline_name">Partial <u>Underline String</u> from resources</string>
</resources>


Edit layout/activity_main.xml to use the string resources:
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
    android:orientation="vertical"
    tools:context="com.blogspot.android_er.androidstylingstringresources.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:text="@string/normal_name"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:textStyle="italic"
        android:text="Whole TextView in italic style"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:text="@string/italic_name"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:text="@string/bold_name"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:text="@string/underline_name"/>

</LinearLayout>