Android Toast Demonstration

Screenshot

Screenshot of the program running in the Gingerbread emulator

Notice the following points in the source.
  • We needed to search for the code to remove the onscreen keyboard and wonder how many developers would have the knowledge and intuition to predict it!
  • We use nested RelativeLayouts in the layout file to obtain a row of two widgets under a column of widgets.

You can compare this code with the RemObjects C# version below it.

The code of MainActivity.pas

namespace org.me.android_toast;

interface

uses
  android.app,
  android.os,
  android.view,
  android.widget,
  android.view.inputmethod;
type
  MainActivity = public class(Activity)
  private
    edtName : EditText;
    btnToast : Button;
  public
    method onCreate(savedInstanceState: Bundle); override;
    method onClick(v : View);
  end;

implementation

method MainActivity.onClick(v : View);
var
  imm : InputMethodManager;
begin
  //Remove on-screen keyboard.
  imm := InputMethodManager(getSystemService(
                            Activity.INPUT_METHOD_SERVICE)); 
  imm.hideSoftInputFromWindow(edtName.getWindowToken, 0);

  // Get string entered into the EditText. 
  var csName: CharSequence := edtName.getText;
  // Display the Toast.
  Toast.makeText(self, 'Hello ' + csName + '!', Toast.LENGTH_SHORT).show
end;

method MainActivity.onCreate(savedInstanceState: Bundle);
begin
  inherited;
  setContentView(R.layout.main);  
  var title : String := getString(R.string.title);
  setTitle(title);  // Change title from default.
  // Assign EditText and Button.
  edtName := EditText(findViewById(R.id.edt1));
  btnToast := Button(findViewById(R.id.btn1));
  // Set listener for button click.
  btnToast.setOnClickListener(@onClick);
end;

end.

Layout File

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center_horizontal" >
  <TextView
    android:id="@+id/greeting"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="18sp"
    android:text="@string/hello"/>
  <TextView
    android:layout_below="@id/greeting"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/mainTextView"
    android:text="@string/mainText"
    android:textStyle="italic"
    android:typeface="serif"
    android:gravity="center"
    android:background="@color/White"></TextView>
  <TextView
    android:layout_below="@id/mainTextView"
    android:id="@+id/instructions"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:padding="10sp"
    android:text="@string/subText"
    android:textColor="@color/White"
    android:textStyle="bold"
    android:typeface="monospace"
    android:background="@color/Green"></TextView>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:layout_below="@id/instructions">
    <EditText
      android:text=""
      android:id="@+id/edt1"
      android:layout_width="140sp"
      android:layout_height="wrap_content"></EditText>
    <Button
      android:layout_below="@id/instructions"
      android:layout_toRightOf="@id/edt1"
      android:text="Click for Toast!"
      android:id="@+id/btn1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></Button>
  </RelativeLayout>
</RelativeLayout>

Strings File

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Greetings!</string>
  <string name="app_name">org.me.android_toast</string>
  <string name="title">Toast Demo</string>
  <string name="mainText">Demo of EditText, Button and Toast</string>
  <string name="subText">Type your forename into the edit box then click on the button to see the onscreen keyboard clear and toast pop up.</string>
  <color name="White">#FFFFFF</color>
  <color name="Green">#00FF00</color>
</resources>

RemObjects C# Version of Android Toast Demonstration

using android.util;
using android.app;
using android.content;
using android.os;
using android.util;
using android.view;
using android.widget;
using android.view.inputmethod;

namespace org.me.android_toast_cs
{
  public class MainActivity: Activity
  {
    private 
      EditText edtName;
      Button btnToast;
    public void onClick(View v)
    { 
      //Remove on-screen keyboard.
      InputMethodManager imm = (InputMethodManager)(getSystemService(
                                                  Activity.INPUT_METHOD_SERVICE)); 
      imm.hideSoftInputFromWindow(edtName.getWindowToken(), 0);

      // Get string entered into the EditText. 
      CharSequence csName= edtName.getText();
      // Display the Toast.
      Toast.makeText(this, "Hello " + csName + "!", Toast.LENGTH_SHORT).show();
    }

    public override void onCreate(Bundle savedInstanceState)
    {
      base.onCreate(savedInstanceState);
      // Set our view from the "main" layout resource
      ContentView = R.layout.main;
      String title = getString(R.@string.title);
      setTitle(title);  // Change title from default.
      // Assign EditText and Button.
      edtName = (EditText)(findViewById(R.id.edt1));
      btnToast = (Button)(findViewById(R.id.btn1));
      // Set listener for button click.
      btnToast.OnClickListener = onClick;
    }
  }
}


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