Monday, March 14, 2016

android - Get TextView number of lines

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="#c3d1c9"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#1d63bf"
        android:background="#f8fcfd"
        android:text="This is a sample TextView to test line counter"
        />
    <TextView
        android:id="@+id/tv_second"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#b61a77"
        android:background="#ffc8f8"
        android:layout_below="@id/tv"
        android:text="This is a sample TextView to test line counter"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get TextView Line Numbers"
        android:layout_below="@id/tv_second"
        />
</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;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private TextView mTextViewSecond;
    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);
        mTextView = (TextView) findViewById(R.id.tv);
        mTextViewSecond = (TextView) findViewById(R.id.tv_second);
        mButton = (Button) findViewById(R.id.btn);

        // Set a click listener for button
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    public int getLineCount ()
                        Return the number of lines of text, or 0 if the internal Layout
                        has not been built.

                    Returns
                        int
                */
                // Get the first TextView Line numbers
                int lines = mTextView.getLineCount();

                // Count second TextView number of lines
                int moreLines = mTextViewSecond.getLineCount();

                // Display the number of lines
                Toast.makeText(mContext,
                        "First TextView: " + lines + "\nSecond TextView : " + moreLines,
                        Toast.LENGTH_SHORT)
                        .show();

            }
        });

    }
}