Monday, July 17, 2017

android - Auto sizing TextView in support library

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#adf9ce"
    android:padding="16dp"
    android:orientation="vertical"
    >
    <!--
        android:autoSizeTextType

            Specify the type of auto-size. Note that this feature is not supported by EditText,
            works only for TextView.

            Must be one of the following constant values.

            Constant : Value : Description
            none : 0 : No auto-sizing (default).
            uniform : 1 : Uniform horizontal and vertical text size scaling to fit within the container.

    -->
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="Auto Size Text."
        android:textColor="#e92121"
        android:background="#e9ebf1"
        app:autoSizeTextType="uniform"
        />
    <TextView
        android:id="@+id/text_view2"
        android:layout_width="350dp"
        android:layout_height="250dp"
        android:text="This is another auto size text."
        android:textColor="#1679ca"
        android:background="#e9e1e1"
        app:autoSizeTextType="uniform"
        />
    <TextView
        android:id="@+id/text_view3"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:textColor="#48792e"
        android:background="#eff7f2"
        android:text="Auto sizing text view programmatically."
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


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

    private TextView mTextView;
    private TextView mTextView2;
    private TextView mTextView3;

    @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
        mTextView = (TextView) findViewById(R.id.text_view);
        mTextView2 = (TextView) findViewById(R.id.text_view2);
        mTextView3 = (TextView) findViewById(R.id.text_view3);

        /*
            TextViewCompat
                Helper for accessing features in TextView in a backwards compatible fashion.

        */

        /*
            setAutoSizeTextTypeWithDefaults

                void setAutoSizeTextTypeWithDefaults (TextView textView, int autoSizeTextType)

                Specify whether this widget should automatically scale the text to try to perfectly
                fit within the layout bounds by using the default auto-size configuration.

                Parameters
                    textView : TextView
                    autoSizeTextType : int: the type of auto-size. Must be one of
                        AUTO_SIZE_TEXT_TYPE_NONE or AUTO_SIZE_TEXT_TYPE_UNIFORM
        */

        // Programmatically set auto sizing text view
        TextViewCompat.setAutoSizeTextTypeWithDefaults(
                mTextView3,TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM
        );
    }
}