13

I have put a TextView inside a ScrollView in Android:

<ScrollView
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content">

    <TextView
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content" />
</ScrollView>

With this configuration, the scrollview grows to wrap the textview-content. How can I achieve that the scrollview takes only as much space as needed (for few text), but at the same time limit the size to 100dp (for long texts) ?

  • If I set layout_height to 100dp, a lot of space is wasted when the text is short
  • If I set layout_height to wrap_content, the scrollview runs the risk to fill the whole screen, but I don't want the whole sreen to only contain this scrollview. It should be 100dp heigh as a maximum.

The solution which was found:

Thank you for alls answers. For all people that have the same issue, here goes the accepted solution: Create a custom ScrollView class an use this class inside your xml file instead ScrollView. Then override onMeasure() method:

public class ScrollMyVew extends ScrollView {
    public static final int maxHeight = 100; // 100dp
    // default constructors 

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(dpToPx(getResources(),maxHeight), MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    private int dpToPx(Resources res, int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,     res.getDisplayMetrics());
    }
}

This solution is taken from https://stackoverflow.com/a/23617530/3080611

Community
  • 1
  • 1
Brian
  • 1,318
  • 1
  • 16
  • 33

2 Answers2

2

Try something like this:

<ScrollView
    android:id="@+id/myScrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:fillViewport="true"/>
    <TextView
        android:id="@+id/myTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="your_value_here"/>
</ScrollView>
ChuongPham
  • 4,761
  • 8
  • 43
  • 53
  • Thanks for your answer, I just tried this out. Unfortunately, With this approach it seems impossible to limit the height. For me it works like wrapcontent and ignores the textview's height – Brian Sep 11 '14 at 15:21
  • 1
    Have you tried setting the height of your `TextView` with [maxHeight](http://developer.android.com/reference/android/widget/TextView.html#attr_android:maxHeight) and wrap it within my `ScrollView` example above? – ChuongPham Sep 11 '14 at 15:28
  • Yes, but it just won't work this way. But it is ok, i have found a solution now, thanks – Brian Sep 11 '14 at 15:48
  • Thank you for `android:fillViewport="true"` – Machado May 27 '15 at 12:11
  • This doesn't work, it disables the scroll function and the text is truncated. – Ernesto Vega Apr 06 '18 at 09:44
0

Dynamically change the size of the TextView if the number of characters exceeds a certain amount.

Ex:

TextView v = (TextView) findViewById(R.id.sometext);
LayoutParams lp = v.getLayoutParams();
lp.height = 50;
v.setLayoutParams(lp);
erad
  • 1,766
  • 2
  • 17
  • 26
  • I am looking for a xml solution, just because I'd like to avoid needing to set the height manually. On the contrary, I'd like the height to adjust automatically between the bounds of 0 and 100dp – Brian Sep 11 '14 at 15:24