Tuesday, August 9, 2016

Android ScaleXSpan 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="#e6f2f7"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="18dp"
        android:textSize="30sp"
        android:gravity="start"
        android:textColor="#4673de"
        />
</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.ScaleXSpan;
import android.widget.TextView;


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

    private CoordinatorLayout mCLayout;
    private TextView mTextView;

    private String mText;
    private SpannableStringBuilder mSSBuilder;


    @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
        mText = "Test text to display ScaleXSpan example." +
                "This is 2X scaled text." +
                "This is a normal text." +
                "This is 3x scaled text." +
                "This is a normal text." +
                "This is half X scaled text." +
                "This is a normal text.";

        // Initialize a new SpannableStringBuilder instance
        mSSBuilder = new SpannableStringBuilder(mText);

        // Initialize a new ScaleXSpan instance to scale character 2x bigger
        ScaleXSpan scaleXSpan2X = new ScaleXSpan(2.0f);

        // Initialize a new ScaleXSpan instance to scale character 3x bigger
        ScaleXSpan scaleXSpan3X = new ScaleXSpan(3.0f);

        // Initialize a new ScaleXSpan instance to scale character half x smaller
        ScaleXSpan scaleXSpanHalfX = new ScaleXSpan(.5f);

        // Apply the ScaleXSpan
        mSSBuilder.setSpan(
                scaleXSpan2X, // Span to add
                mText.indexOf("This is 2X scaled text."), // Start of the span (inclusive)
                mText.indexOf("This is 2X scaled text.") +
                        String.valueOf("This is 2X scaled text.").length(), // End of the span (exclusive)
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE // Do not extend the span when text add later
        );

        // Apply the ScaleXSpan
        mSSBuilder.setSpan(
                scaleXSpan3X,
                mText.indexOf("This is 3x scaled text."),
                mText.indexOf("This is 3x scaled text.") + String.valueOf("This is 3x scaled text.").length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

        // Apply the ScaleXSpan
        mSSBuilder.setSpan(
                scaleXSpanHalfX,
                mText.indexOf("This is half X scaled text."),
                mText.indexOf("This is half X scaled text.") + String.valueOf("This is half X scaled text.").length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

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