brian
brian

Reputation: 6912

How get textview height

My layout as below:

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

My code as below:

TextView title = (TextView)findViewById(R.id.title);
title.setText("ABCDE...");
title.setTextSize(20);

TextView info = (TextView)findViewById(R.id.info);
info.setText("abcde...");
info.setTextSize(16);

LinearLayout layout = (LinearLayout)findViewById(R.id.layout);

I want to get the height of TextView title and info, or the height of layout. How to do it?

Upvotes: 0

Views: 5052

Answers (3)

Samir Mangroliya
Samir Mangroliya

Reputation: 40406

@Ovveride onWindowFocusChanged method in your activity

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        int x = title.getHeight();
        int y = info.getHeight();
        int z = layout.getHeight();  
    }

Upvotes: 1

Shubhayu
Shubhayu

Reputation: 13552

use the getHeight() function of the View. so in ure case, info.getHeight(), layout.getHeight() etc.

Upvotes: 0

user1155300
user1155300

Reputation:

You can try getLineHeight() method. Such as:

title.getLineHeight();
info.getLineHeight();

Upvotes: 4

Related Questions