Thursday, November 26, 2015

android - Get TextView width and height programmatically

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="#dee2db"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Measure Me"
        android:textSize="50dp"
        android:background="#fa3b3b"
        android:textColor="#000"
        android:padding="15dp"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:layout_below="@id/tv"
        android:layout_marginTop="5dp"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.graphics.Typeface;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Resources mResources;
    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private TextView mTextView2;

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

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

        // Get the Resources
        mResources = getResources();

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mTextView2 = (TextView) findViewById(R.id.tv2);

        /*
            public final void measure (int widthMeasureSpec, int heightMeasureSpec)
                This is called to find out how big a view should be. The parent supplies constraint
                 information in the width and height parameters.

                The actual measurement work of a view is performed in onMeasure(int, int), called by
                this method. Therefore, only onMeasure(int, int) can and must be overridden by subclasses.

            Parameters
                widthMeasureSpec : Horizontal space requirements as imposed by the parent
                heightMeasureSpec : Vertical space requirements as imposed by the parent
        */

        // Get the width and height of first TextView
        mTextView.measure(0,0);

        /*
            public final int getMeasuredWidth ()
                Like getMeasuredWidthAndState(), but only returns the raw width component (that is
                the result is masked by MEASURED_SIZE_MASK).

            Returns
                The raw measured width of this view.
        */
        /*
            public static final int MEASURED_SIZE_MASK
                Bits of getMeasuredWidthAndState() and getMeasuredWidthAndState() that provide
                the actual measured size.
        */
        /*
            public final int getMeasuredWidthAndState ()
                Return the full width measurement information for this view as computed by the most
                recent call to measure(int, int). This result is a bit mask as defined by
                MEASURED_SIZE_MASK and MEASURED_STATE_TOO_SMALL. This should be used during
                measurement and layout calculations only. Use getWidth() to see how wide a view
                is after layout.

            Returns
                The measured width of this view as a bit mask.
        */
        int width = mTextView.getMeasuredWidth();

        /*
            public final int getMeasuredHeight ()
                Like getMeasuredHeightAndState(), but only returns the raw width component (that
                is the result is masked by MEASURED_SIZE_MASK).

            Returns
                The raw measured height of this view.
        */
        int height = mTextView.getMeasuredHeight();

        mTextView2.setText(
                "Width and Height of first TextView\n" +
                        "Width = " + width + " pixels" +
                        "\nHeight = " + height + " pixels"
        );
    }
}
More android examples