Pages Navigation Menu

Coding is much easier than you think

How To Create Scroll View in an Android Application

How To Create Scroll View in an Android Application

In this Tutorial, you will Learn, How to view the activity using scroll view

Here you go!!!!

 

Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.

Key terms involved in this project:

ScrollView: A ScrollView is a special type of FrameLayout in that it allows users to scroll through a list of views that occupy more space than the physical display. The ScrollView can contain only one child view or ViewGroup, which normally is a LinearLayout.

Note: Do not use a ListView together with the ScrollView. The ListView is designed for showing a list of related information and is optimized for dealing with large lists.

The tutorial is about how to view the activity using scroll view.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to android application project, to create a new project in Eclipse refer Creation of Android Project.

Coding:

Now let’€™s go to the coding section. This project requires following files.

Source Code:

  1. Scrollview.java

Activity Files:

  1. activity_scrollview.xml – scrollview activity (Main activity)

res – Values:

  1. strings.xml -€“ strings available in scroll view activity

Manifest file:

  1. AndroidManifest.xml

Here is the coding for the above files.

Scrollview.java:

package com.simplecodestuffs.scrollview; //package scroll view
import android.os.Bundle; // A mapping from String values to various Parcelable types
import android.app.Activity; // Required to create an activity
import android.view.Menu;  // for menu settings
public class Scrollview extends Activity {// all classes extends activity
	@Override
	protected void onCreate(Bundle savedInstanceState) {// create a new activity
		super.onCreate(savedInstanceState);
// displays scroll view activity when app starts
		setContentView(R.layout.activity_scrollview); 
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_scrollview, menu);
		return true;
	}
}

activity_scrollview.xml:


<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <LinearLayout
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 1"
            />
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 2"
            />
        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 3"
            />
       
        <Button
            android:id="@+id/button4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 4"
            />
        <TextView 
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:textSize= "30sp"
            android:textStyle="bold"
            android:gravity="center"
            android:textColor="#0000FF"
            android:text="simplecodestuffs.com"
            />
        
        <Button
            android:id="@+id/button5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 5"
            />
        <Button
            android:id="@+id/button6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 6"
            />
        
    </LinearLayout>
</ScrollView> 

Menu : activity_listview.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="100"
        android:title="@string/menu_settings"/>

</menu> 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.simplecodestuffs.scrollview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.simplecodestuffs.scrollview.Scrollview"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest> 

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Scrollview</string>
    
    <string name="menu_settings">Settings</string>

</resources> 

Styles. xml:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources> 

Run the android application:

Android applications can be run on the android devices. You can either connect hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

To run the application/ project, Build your project using Project -> Build Project.

Build project

This will show errors in Console window at the bottom of the working area in case your project contains.

If your build is successful, Run your application using Run -> Run or Press Ctrl +F11.

Run Project

Upon running the application, the emulator will be launched which displays the AVD on your screen.

 

 

You can see your app with the image set during the android project creation in AVD. Upon running the application, the emulator will be launched with the selected/ available AVD created on your screen.

Start Emulator

Emulator loading home page

To test your application, unlock the screen and double click on your app.

AVD Home screen

Before scrolling:

Scroll view - Before scrolling

After scrolling the screen,

Scroll view - After scrolling

Thus the android application project is executed successfully.

 


Click Here To Share This..!!
Share

  • HIREN PITHWA

    VERY GOOD

  • Dinesh

    You produced some decent points there. I looked on the net for the issue and found most people will go along with together with your web page.