Wednesday, August 3, 2016

Android TextAppearanceSpan example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d4c8b5"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="25dp"
        android:textSize="40sp"
        android:gravity="center"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private TextView mTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mTextView = (TextView) findViewById(R.id.tv);

        // Initialize a new String variable
        String text = "Test text to display TextAppearanceSpan in a TextView.";

        // Initialize a new SpannableStringBuilder instance
        SpannableStringBuilder ssBuilder = new SpannableStringBuilder(text);

        // Initialize a new TextAppearanceSpan to display custom styled text
        TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(mContext, R.style.CustomTextStyle);

        // Initialize another new TextAppearanceSpan to display custom styled text
        TextAppearanceSpan anotherTextAppearanceSpan = new TextAppearanceSpan(mContext, R.style.AnotherCustomTextStyle);

        // Apply the custom text appearance to the span
        ssBuilder.setSpan(
                textAppearanceSpan, // Span to add
                text.indexOf("TextAppearanceSpan"), // Start of the span (inclusive)
                text.indexOf("TextAppearanceSpan") + String.valueOf("TextAppearanceSpan").length(), // End of the span (exclusive)
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE // Do not extend the span when text add later
        );

        // Apply another custom text appearance to the span
        ssBuilder.setSpan(
                anotherTextAppearanceSpan,
                text.indexOf("TextView"),
                text.indexOf("TextView") + String.valueOf("TextView").length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        // Display the spannable text to TextView
        mTextView.setText(ssBuilder);
    }
}
res/vales/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>

        <!-- Important for CoordinatorLayout  -->
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="CustomTextStyle" parent="@android:style/Widget.TextView">
        <item name="android:textColor">#ff0000</item>
        <item name="android:textStyle">bold</item>
    </style>

    <style name="AnotherCustomTextStyle" parent="@android:style/Widget.TextView">
        <item name="android:textColor">#187117</item>
        <item name="android:textStyle">italic</item>
        <item name="android:fontFamily">sans-serif-thin</item>
        <item name="android:textSize">60sp</item>
    </style>

</resources>