Adam Adam - 9 months ago 22
Android Question

ScrollView shows content in Android Studio but not in app

I'm trying to have a layout like this
Layout in Android Studio

however when I install it onto my S7 edge it looks like this:

Galaxy S7 edge in portrait, ScrollView not displaying

I'm not too sure what I am doing wrong, since it works when the device is in landscape.

Galaxy S7 edge in landscape, works fine

Here's my layout file

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="visible"
android:weightSum="1">
<TextView
android:id="@+id/HttpResponse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable Features"
android:textAlignment="center"
android:textSize="24sp"
android:visibility="visible" />

<ScrollView
android:layout_width="456dp"
android:layout_height="wrap_content"
android:visibility="visible">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="visible">

<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:visibility="visible" />

<Switch
android:id="@+id/switch2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:visibility="visible" />

<Switch
android:id="@+id/switch3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch"
android:visibility="visible" />

</LinearLayout>
</ScrollView>

<Button
android:id="@+id/RequestKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:visibility="visible" />
</LinearLayout>


Here is the "Layout and bounds" for the layout:

enter image description here

(The area on the right hand side is for the edge display, disregard it.)

Answer Source

That happens because the width of your ScrollView is too large for the portrait orientation, try setting it to match_parent :

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible">

EDIT

To apply side margins to your ScrollView use :

android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"