Using Scrollview When Text Is Not Visible

This is a sample activity which shows how to create a scrollview when text is not visible on one page.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project ScrollView.
2.) Put the following code snippet in res/layout/main.xml.

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

 

 

3.) Put some scrollable text in res/values/string.xml.

 

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

Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!Hello World, ScrollViewActivity!
ScrollViewSample

 

 

4.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. ScrollView. Enter following information:
Project name: ScrollView
Build Target: Google APIs
Application name: ScrollViewSample
Package name: com.sample. ScrollView
Create Activity: ScrollViewActivity

On Clicking Finish ScrollView code structure is generated with the necessary Android Packages being imported along with ScrollViewActivity.java. ScrollViewActivity class will look like following:

 

package com.sample.ScrollView;

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

public class ScrollViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

 

 

Output –The final output:

Leave a Comment:

4 comments
beth says June 15, 2011

How could you add to the above program to add a horizontal scroll as well?

Reply
Sushant says June 20, 2011

Replac your strings.xml and scroll_view.xml with below code:

strings.xml:

[code lang="xml"]
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="hello">Hello World, ScrollViewActivity! Hello World, ScrollViewActivity!n Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! nHello World, ScrollViewActivity! Hello World, ScrollViewActivity! </string>
<string name="app_name">ScrollViewSample</string>
</resources>
[/code]

and scroll_view.xml:

[code lang="xml"]
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android&quot;
android:layout_width="fill_parent" android:layout_height="wrap_content">

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android&quot;
android:layout_width="wrap_content" android:layout_height="fill_parent">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/hello" />
</HorizontalScrollView>
</ScrollView>
[/code]

and check the results.

Reply
lemus2307 says October 27, 2011

How to create a autoscroll in a HorizontalScrollView?

Reply
lemus2307 says October 27, 2011

How to create a auto scroll in a HorizontalScrollView?

Reply
Add Your Reply