15 January 2015

ScrollView-Large number of items in single page.

What if you have to fit a large story in a page or fit 20-30 buttons, check-boxes in a page? Use a ScrollView to fit it there.

What we will do?
-> We will prepare an application with a page with lot of items and which scrolls down.

What we will need?
  • Adding ScrollView in activity_main.xml along with existing items.
  • Others remain same.
Step 1 : Create a new Project. Click on File->New_>Android Application Project.
file new

Step 2 : Give a specific name to your Project and also a package name(optional).Here I prefer it to be ScrollDemo. Then click Next... till Finish and click finish.
app name

Step 3 : In the activity_main.xml (res->layout folder), drag the ScrollView from the Palette as shown below. Then add different elements of your choice inside it.
drag scrollview

activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="240dp"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="252dp"\
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google. With a user interface based on direct manipulation, Android is designed primarily for touchscreen mobile devices such as smartphones and tablet computers, with specialized user interfaces for televisions (Android TV), cars (Android Auto), and wrist watches (Android Wear). "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Android is the most widely used mobile OS and, as of 2013, the highest selling OS overall. Android devices sell more than Microsoft Windows, iOS, and Mac OS X devices combined,[13][14][15][16][17] with sales in 2012, 2013 and 2014[18] close to the installed base of all PCs.[19] As of July 2013 the Google Play store has had over 1 million Android apps published, and over 50 billion apps downloaded.[20] A developer survey conducted in April–May 2013 found that 71% of mobile developers develop for Android.[21] At Google I/O 2014, the company revealed that there were over 1 billion active monthly Android users, up from 538 million in June 2013.[22]"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.11"
        android:text="Click Me" /> 

</LinearLayout>

</ScrollView>

Dont make any changes in other files.

Step 4 : Run your Project.
run project

Explanation of the Code:
A ScrollView Element covers all the elements under it.
diagram explain

Stay Tuned with Made In Android

Previous Page Next Page Home
Top