Expand ScrollView vertically if height is smaller than screen // Android Dev Diary

Written by

on

in

,

I have the following XML layout code in an Android app that I’ve been tinkering with:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="viewModel"
            type="net.catzie.sampleapp.MainViewModel" />
    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#aaa"
            android:columnCount="4"
            android:rowCount="5">

            ...

        </GridLayout>
    </ScrollView>
</layout>

I wrapped my GridLayout in a ScrollView because on the landscape orientation of the Android app’s screen, my grid contents are cut from the view because they’re too tall, and there’s no way to see them without a scrollbar, which the ScrollView provides.

That gave birth to another problem though. The landscape view looks fine, but the vertical view became too short!

After a few clicks at Google search results I found something that helped:

android:fillViewport="true"

I added the above attribute to my ScrollView, and voila! A GridLayout that scrolls vertically on landscape view, and fits the height of the Android device screen perfectly on portrait view~

Related Posts

About the author:

Catzie Avatar

The homo sapiens behind these blog posts is “Catzie“

Explore my top blog posts:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *