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

6
  • Check this stackoverflow.com/questions/4054567/… Commented Sep 11, 2014 at 15:18
  • This won't work for me because I have views below the scrollview. This issue was also commented by sreedhu madhu on the given thread Commented Sep 11, 2014 at 15:23
  • Then why not implement your own ScrollView and override onMeasure method? Commented Sep 11, 2014 at 15:24
  • Could you give some example please, I have never overriden onmeasure method before. Commented Sep 11, 2014 at 15:25
  • 1
    Check whizzle answer on the given thread. Commented Sep 11, 2014 at 15:28

2 Answers 2

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> 
Sign up to request clarification or add additional context in comments.

4 Comments

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
Have you tried setting the height of your TextView with maxHeight and wrap it within my ScrollView example above?
Yes, but it just won't work this way. But it is ok, i have found a solution now, thanks
This doesn't work, it disables the scroll function and the text is truncated.
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); 

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.