0

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?

brian
  • 6,802
  • 29
  • 83
  • 124
  • 2
    refer this http://stackoverflow.com/questions/4912687/android-get-the-height-of-the-textview – gandharva Apr 17 '12 at 09:00
  • I am not a Java expert. But when a method/function is being run and filling TextView with a new text strings the TV will not be validated at once. Only after the function has finished. So you cant get the new TextView's height or Width on-the-fly. TexView however has got the invalidate() and refreshDrawableState() but they just DONT WORK as supposed to. It is a problem in Android Java. I hope KitKat will fix all the issues. – TomeeNS Nov 03 '13 at 17:52

3 Answers3

4

You can try getLineHeight() method. Such as:

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

@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();  
    }
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
0

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

Shubhayu
  • 13,402
  • 5
  • 33
  • 30