'Anyway to keep the height of a layout as wrap content, but not let it exceed a certain height

I have a ScrollView that contains a TextView which is fetched dynamically from a data base. If the text is small in length, I want my ScrollView layout_height to wrap content, however if the text length is big, I want the ScrollView layout_height to be fixed to a certain value (such as 200dp). Is there anyway to achieve this?



Solution 1:[1]

Perhaps you're looking for

 android:minHeight="12dp" 
 android:maxHeight="200dp"

just some example values, you don't have to use both, but you can set maxHeight to a certain value so that the view never exceeds those values and perhaps minHeight so that it doesn't collapse entirely, unless you want that, in which case just remove minHeight

Solution 2:[2]

You can wrap your code into LinearLayout with wrap_content option and on inner structure you can set android:layout_weight="1"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 a_local_nobody
Solution 2 user3913101