tags:

views:

115

answers:

2

Hi Gurus,

I have some critical issue related to height of TextView. I want to set the height of the TextView based on the content which is set at runtime. Here is my xml code:-

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

Here is the java code to set the height of the TextView:-

`TextView tv = (TextView) findViewById(R.id.tv);

tv.invalidate();

int height_in_pixels = tv.getLineCount() * tv.getLineHeight();

tv.setHeight(height_in_pixels);`

But the problem is; it displays only 1 line, and not the rest of the lines.

A: 

But the problem is; it displays only 1 line, and not the rest of the lines.

I think the best solution is to define your textview inside the "ScrollView".

For example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <ScrollView 
        android:id="@+id/ScrollView01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">

    <TextView 
            android:text="@+id/TextView01" 
            android:id="@+id/TextView01" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
    </TextView>
    </ScrollView>

</LinearLayout> 
PM - Paresh Mayani
A: 

In Java Code:

TextView tv = (TextView) findViewById(R.id.tv);

tv.invalidate();

int height_in_pixels = tv.getLineCount() * tv.getLineHeight();

tv.setHeight(height_in_pixels);

these lines are unwanted in java code. In xml itself you have mentioned the layout_height="wrap_content". then why you are setting again the height of the TextView. and Make sure that android:singleLine="false" in the <textview>.if you not set anything, its default value is false.

Praveen Chandrasekaran
I tried this, but it doesn't works still.
RATTLESNAKE
@RATTLESNAKE: Then Problem is in your input :)). can you post your screenshot on your question?
Praveen Chandrasekaran