Wednesday, July 1, 2015

[Android] android make layout scrollable

Embed your existing xml code into a ScrollView.

For example, from
<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/wallpaper"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/avatar"
            android:layout_width="124dp"
            android:layout_height="124dp"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:layout_marginTop="50dp"
            android:layout_below="@id/wallpaper" />
        />

        <TextView
            android:id="@+id/username"
            android:text="@string/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_below="@id/avatar" />
        />
    </RelativeLayout>
To
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/wallpaper"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/avatar"
            android:layout_width="124dp"
            android:layout_height="124dp"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:layout_marginTop="50dp"
            android:layout_below="@id/wallpaper" />
        />

        <TextView
            android:id="@+id/username"
            android:text="@string/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:layout_below="@id/avatar" />
        />

    </RelativeLayout>
</ScrollView>

Reference:
http://stackoverflow.com/questions/22271307/how-to-make-a-layout-to-be-scrollable-android

No comments :

Post a Comment