Monday, August 1, 2016

android - Spannable bold, italic and underline text style

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="#7a8679"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:layout_gravity="center"
        android:layout_margin="25dp"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
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.StyleSpan;
import android.text.style.UnderlineSpan;
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 String variable
        String text = "This is a text to test Bold, BoldItalic, Italic and Underline style.";

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

        // Initialize a new StyleSpan to display bold text
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);

        // Initialize a new StyleSpan to display bold italic text
        StyleSpan boldItalicSpan = new StyleSpan(Typeface.BOLD_ITALIC);

        // Initialize a new StyleSpan to display italic text
        StyleSpan italicSpan = new StyleSpan(Typeface.ITALIC);

        // Initialize a new UnderlineSpan
        UnderlineSpan underlineSpan = new UnderlineSpan();

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

        // Apply the bold italic text style span
        ssBuilder.setSpan(
                boldItalicSpan,
                text.indexOf("BoldItalic,"),
                text.indexOf("BoldItalic,") + String.valueOf("BoldItalic").length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        // Apply the italic text style span
        ssBuilder.setSpan(
                italicSpan,
                text.indexOf(" Italic") + 1,
                text.indexOf(" Italic") +1 + String.valueOf("Italic").length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        // Apply the underlined text style span
        ssBuilder.setSpan(
                underlineSpan,
                text.indexOf("Underline"),
                text.indexOf("Underline") + String.valueOf("Underline").length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        // Display the spannable text to TextView
        mTextView.setText(ssBuilder);
    }
}