Search code examples
androidandroid-linearlayoutandroid-custom-viewandroid-scrollview

Android match_parent not working as expected layout


I have a custom view, TriggerView. When I override onDraw in the view, I draw a graph that sometimes overflows outside the viewable bounds of the view. So I want to embed the view inside a ScrollView so that the graph can be scrollable.

Here's my code without any ScrollViews:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.roberts.croberts.mantis.ShotTriggerFragment"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:clipChildren="false">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        ...
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageContainer"
        android:orientation="horizontal"
        android:layout_weight="1">

        <com.roberts.croberts.mantis.TriggerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/triggerView" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        ...

    </LinearLayout>
</LinearLayout>

See how in this screenshot that the triggerView fills all the remaining screen space:

Screenshot

So then I try to embed it in a ScrollView:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageContainer"
        android:orientation="horizontal">

        <com.roberts.croberts.mantis.TriggerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/triggerView" />

    </LinearLayout>
</ScrollView>

And then the TriggerView height gets shrunk to 0. I've tried every variation of match_parent and wrap_content I can think up, and can't get it to work:

Screenshot

edit - another interesting thing I just found is when I remove the container ScrollView and LinearLayout and just have the TriggerView:

<com.roberts.croberts.mantis.TriggerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/triggerView"
        android:layout_weight="1"/>

Then the design tab says that the parent class is a ScrollView?:

TriggerView (ScrollView)


Solution

  • <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageContainer"
            android:orientation="horizontal">
    
            <com.roberts.croberts.mantis.TriggerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/triggerView" />
    
        </LinearLayout>
    </ScrollView>
    

    Some issues here: ScrollView with height defined as match_parent has no sense - it always should be set to wrap_content. LinearLayout height is set to match_parent what is also wrong, as this element should define height of containing ScrollView - change it to wrap_content. End last - your custom view - it also should have height set to wrap_content (or some fixed value). If it still doesn't work well, look for issues inside this view - hard to say - inflated layout, or onMeasure() method