androidandroid-layoutandroid-scrollviewandroid-wrap-content

ScrollView and its content use 'wrap_content'. How to assign constraint priority?


I need a horizontal ScrollView with a fixed height of 100dp and dynamic content of ImageViews.

Currently I the ScrollView has a horizontal LinearLayout with ImageViews. The ImageViews have wrap_content enabled for their width, while the height is on the fixed height for all of them. The ScrollView itself has its height on wrap_content.

If the ScrollViews content is bigger than what the view can display without scrolling, the ImageViews images are scaled down. The ImageViews layout_hight works in terms of its bounds, but the image itself is not.

Changing scaleType to something else doesn't help. Setting a fixed width for the LinearLayout works, but as the content should be dynamic that's not an option. This seems like a default use case. Isn't this possible in xml?

Example with a manually given exact ScrollView-width:

The view looks as I need it, but won't allow dynamic content.

enter image description here

Example with width on wrap_content:

The ImageView

enter image description here

ScrollView code below:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#CCCCFF">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#AAAAFF"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/so1"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:background="#FF0000"
            android:scaleType="fitCenter"
            android:src="@drawable/so" />

        <Space
            android:layout_width="10dp"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/so2"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:background="#FF0000"
            android:scaleType="fitCenter"
            android:src="@drawable/so" />

        <Space
            android:layout_width="10dp"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/so3"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:background="#FF0000"
            android:scaleType="fitCenter"
            android:src="@drawable/so" />

    </LinearLayout>
</ScrollView>

Solution

  • Ok, that was way easier to fix, than i thought:

    ScrollViews are always vertical. There is no orientation attribute in ScrollView. To get a horizontal ScrollView instead use HorizontalScrollView and ditch the orientation.

    So this

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    

    should instead be:

    <HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">