Khushboo
Khushboo

Reputation: 3173

How to determine the height of TextView programmatically?

I have created an xml for a row of listview. It has just one Textview.

<?xml version="1.0" encoding="utf-8"?>
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="@color/white"
    android:text="A">
</TextView>

I need to determine the height of this textview programmatically. If I code textview.getTextSize(): this just returns the size of text.

Edit:

I am want to retrieve the height of the textview prior to rendering this on the screen. I mean, I want to inflate this:

LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.row, null);
TextView row_textview=((TextView)view);

and then calculate the height of "row_textview" instance.

Upvotes: 0

Views: 6959

Answers (4)

Khushboo
Khushboo

Reputation: 3173

I cannot find any answer to this question, so I am not accepting any answer provided so far on this thread. Since I am trying to calculate the height of the textview before it is rendered on the screen, by reading the xml layout, this is not supported in Android. In this case, we have to hard core the height measure and then convert this dp measure to pixel at runtime. Get the height of a TextView This link can be useful for others who are searching the answer for this.

Upvotes: 0

Hulk
Hulk

Reputation: 2561

Try this to get height and width for textView--

 textView.getMeasuredWidth();

 textView.getMeasuredHeight();

Upvotes: 0

Renjith
Renjith

Reputation: 5803

Did you try

textview.getHeight();

Return the height of your view.

Or this

textview.getMaxHeight();

the maximum height of this TextView expressed in pixels, or -1 if the maximum height was set in number of lines instead using or setLines(int).

Upvotes: 4

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Did you check this and this?

txtview.getHeight();

Returns The height of your view, in pixels.

Or

txtview.getLineHeight();

Returns the height of one standard line in pixels. Note that markup within the text can cause individual lines to be taller or shorter than this height, and the layout may contain additional first- or last-line padding.

Upvotes: 2

Related Questions