Saturday, March 12, 2016

android - How to set TextView line spacing

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#b0bfc7"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Line One\nLine Two\nLine spacing default"
        android:background="#fdfbd0"
        android:textSize="18dp"
        android:layout_margin="10dp"
        android:padding="10dp"
        />
    <!--
        android:lineSpacingExtra
            Extra spacing between lines of text.

            Must be a dimension value, which is a floating point number appended with a unit such
            as "14.5sp". Available units are: px (pixels), dp (density-independent pixels),
            sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).

            This may also be a reference to a resource (in the form "@[package:]type:name") or
            theme attribute (in the form "?[package:][type:]name") containing a value of this type.

            This corresponds to the global attribute resource symbol lineSpacingExtra.
    -->
    <TextView
        android:id="@+id/tv_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Line One\nLine Two\nLineSpacingExtra = 10dp"
        android:background="#eaa3a3"
        android:textSize="18dp"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:layout_toRightOf="@id/tv"
        android:layout_toEndOf="@id/tv"
        android:lineSpacingExtra="10dp"
        />
    <!--
        android:lineSpacingMultiplier
            Extra spacing between lines of text, as a multiplier.

            Must be a floating point value, such as "1.2".

            This may also be a reference to a resource (in the form "@[package:]type:name") or
            theme attribute (in the form "?[package:][type:]name") containing a value of this type.

            This corresponds to the global attribute resource symbol lineSpacingMultiplier.
    -->
    <TextView
        android:id="@+id/tv_third"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Line One\nLine Two\nLineSpacingMultiplier = 2"
        android:background="#a3eabb"
        android:textSize="18dp"
        android:layout_below="@id/tv_second"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:lineSpacingMultiplier="2"
        />
    <TextView
        android:id="@+id/tv_fourth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Line One\nLine Two\nChange the line spacing"
        android:background="#ef85e4"
        android:textSize="18dp"
        android:layout_below="@id/tv_second"
        android:layout_toRightOf="@id/tv_third"
        android:layout_toEndOf="@id/tv_third"
        android:layout_margin="10dp"
        android:padding="10dp"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Fourth TextView Line Spacing"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextViewSecond;
    private TextView mTextViewThird;
    private TextView mTextViewFourth;
    private Button mButton;


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

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextViewSecond = (TextView) findViewById(R.id.tv_second);
        mTextViewThird = (TextView) findViewById(R.id.tv_third);
        mTextViewFourth = (TextView) findViewById(R.id.tv_fourth);
        mButton = (Button) findViewById(R.id.btn);

        // Set a click listener for button
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    public void setLineSpacing (float add, float mult)
                        Sets line spacing for this TextView. Each line will have its height
                        multiplied by mult and have add added to it.

                    Parameters
                        add : float
                        mult : float
                */

                // Programmatically set the line spacing of fourth TextView
                mTextViewFourth.setLineSpacing(0,1.5f);
            }
        });
    }
}