views:

558

answers:

2

Hi all,

In my application I need to set dynamic text to my textview so I want it to get resized dynamically. I have set:

<TextView
android:id="@+id/TextView02" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="normal"
android:text="Frontview"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#0099CC"
android:singleLine="false"
android:maxLines="4"
android:ellipsize="marquee"
/>

My textview height is not going beyond 2 lines and the text is getting cut. Can anybody please help?

Thanx in advance.

A: 

Remove android:maxLines="4", it restricts height of your view to 4 lines of text.

Konstantin Burov
My textview height is not going beyond 2 lines and the text is getting cut.
neha
+1  A: 

As Konstantin said, this code will probably be ignored after you exceed 4 lines unless you remove android:maxLines="4"

This is how you would set the height in code:

TextView tv = (TextView) findViewById(R.id.TextView02);
int height_in_pixels = tv.getLineCount() * tv.getLineHeight(); //approx height text
tv.setHeight(height_in_pixels);

If you want to use dip units, which allows your app to scale across multiple screen sizes, you would multiply the pixel count by the value returned by getResources().getDisplayMetrics().density;

This depends on your desired behavior, but you might also consider having the TextView size fixed, and allowing the user to scroll the text:

TextView tv = (TextView)findViewById(R.id.TextView02);
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
Aaron C
How to get dynamic value for height_in_pixels?
neha
I edited my answer to get the approximate height of the text. You might want to add a small buffer so that characters that hang below the line aren't cut off ("g", "y", etc).
Aaron C
Thanx Aaron C, but the text has disappeared after setting its height. The height is getting set to 0 despite tv not being null.
neha
getLineCount() will return 0 if the text hasn't been rendered yet. You can call invalidate() to force a redraw, but setText() should be sufficient to make getLineCount() valid.
Aaron C