Android ScrollView Demonstration

Screenshot

Screenshot of the app running in the Gingerbread emulator

Points to note in the following files:
  • All of the strings including the long one are in the strings xml file. The text on the Button is set in the layout file with a reference to the strings file. The long string is retrieved from the layout file with the getString method.
  • The LinearLayout containing the Button and TextView widgets is nested inside the ScrollView.

The code of MainActivity.pas

namespace org.me.scrollview;

interface

uses
  java.util,
  android.app,
  android.content,
  android.os,
  android.util,
  android.view,
  android.widget;

type
  MainActivity = public class(Activity)
  private
    tv : TextView;
    myButton : Button;
  public
    method onCreate(savedInstanceState : Bundle); override;
    method ButtonOnClick(v : View);
  end;

implementation

method MainActivity.onCreate(savedInstanceState : Bundle);
begin
  inherited;
  // Set view from "main" layout resource.
  ContentView := R.layout.main;

  // Get button from the layout resource
  // and attach an event to it
  myButton := Button(findViewById(R.id.btn1));
  tv := TextView(findViewById(R.id.tv1));
  myButton.OnClickListener := new interface View.OnClickListener(onClick := @ButtonOnClick);
end;

method MainActivity.ButtonOnClick(v : View);
var
  OldText : CharSequence;
  TextToAdd, NewText, Waffle : String;
begin
  TextToAdd:= getString(R.string.waffle);
  OldText := tv.getText;
  NewText := OldText.toString.trim + TextToAdd; 
  tv.setText(NewText);
end;

end.

Layout File

Notice the way that the LinearLayout is nested inside the ScrollView layout.

<?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="wrap_content" >
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal">
    <Button
      android:id="@+id/btn1"
      android:text="@string/my_button_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <TextView
      android:id="@+id/tv1"
      android:text="@string/waffle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </LinearLayout>
</ScrollView>

Strings File

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">Output Demo</string>
  <string name="my_button_text">Insert more text</string>
  <string name="waffle">This is a lot of text to insert into a TextView to see what happens as the screen fills up. It gets quite boring pasting the same text over and over, but at least you did not have to type it in the first place. What will happen when there is no room on the screen? Keep clicking to find out.</string>
</resources>

Programming - a skill for life!

How to use EditText, Toast, DatePicker, ListView, RadioGroup, ScrollView and ImageView widgets, sound and files in Oxygene for Java Android apps