Android ScrollView Example

By | August 3, 2013
Scrollview is one of important view class in android that always you to scroll the content of the layout ,when the content of the layout goes bigger than the physical device size  

In fact the scrollview is a special kind of framelayout that contains a single child view , and the childview that is used in it is a linear layout in  vertical orientation which can be scrollable is vertical direction 

Lets See An Example

Project Detail

Project Name Scrollbar
Package com.example.scrollbar
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

Create Xml layout with ScrollView , Inside the scrollview we are adding vertical linearlayout containing textview

  file:-activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/scroll_text"
android:textSize="25dp" />

<View
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_marginTop="20dp"
android:background="#000000" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/scroll_text"
android:textSize="25dp" />

<View
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_marginTop="20dp"
android:background="#000000" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/scroll_text"
android:textSize="25dp" />
</LinearLayout>

</ScrollView>

2. Activity

  file:-MainActivity.java

package com.example.scrollbar;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

}

3. RUN

Android ScrollView Example