Android EditText Demonstration

Screenshot of the app

Screenshot of the app running in the Gingerbread emulator

The code of MainActivity.pas

Notice the following points in the code.

  • This and all the other examples pass a Bundle in the OnCreate method of the Activity (screen). This is a bundle of resources for passing information between activities. We do not use it in our code in these simple examples, but to learn more visit Passing-Bundles-Around-Activities
  • The signatures of the event-handling methods ButtonOnClick and EditOnKey must conform to the interface method declarations.
  • The useful setOnClickListener method for a Button takes the address of the onClick event-handler as an argument.
  • The resources in xml files are converted to objects during translation so they are referred to by R. ... e.g. ContentView := R.layout.main;

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

namespace org.me.android_edit;
        
interface

uses
  java.util,
  android.app,
  android.content,
  android.os,
  android.util,
  android.view,
  android.widget;
type
  MainActivity = public class(Activity)
  private
    tvGreeting : TextView;
    edtName : EditText;
    btnClear : Button;
  public
    method onCreate(savedInstanceState: Bundle); override;
    method ButtonOnClick(v: View);
    method EditOnKey(v : View ; keyCode : Integer; key_event : KeyEvent): Boolean;
  end;

implementation

method MainActivity.ButtonOnClick(v: View);
begin
  btnClear.Text := R.string.button_text_2;
  edtName.Text.clear;  //Clear the edit box
  tvGreeting.setText('');  //Clear the label
end;

method MainActivity.EditOnKey(v : View; keyCode : Integer; key_event : KeyEvent): Boolean;
begin
  Result := false;   
  // if key is down and "enter" is pressed
  if ((key_event.getAction = KeyEvent.ACTION_DOWN) and (keyCode = KeyEvent.KEYCODE_ENTER)) then 
    begin     
      tvGreeting.setText('Hello '+ edtName.getText + '!');     
      btnClear.setText('Clear');
      Result := true;
    end;
end;

method MainActivity.onCreate(savedInstanceState: Bundle);
begin
  inherited;
  // Set view from the "main" layout resource.
  ContentView := R.layout.main;
  // Get Button and EditText from the layout resource,
  // then attach an event to each.
  btnClear := Button(findViewById(R.id.btn1));
  edtName := EditText(findViewById(R.id.edt1));
  btnClear.setOnClickListener(@ButtonOnClick);  
  edtName.setOnKeyListener(@EditOnKey);
  // Get TextView from the layout resource.
  tvGreeting := TextView(findViewById(R.id.tv2));
end;

end.

Layout File

<?xml version="1.0" encoding="utf-8"?>
  <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/button_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <TextView
      android:text="First Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />    
    <EditText
      android:id="@+id/edt1"
      android:width="100px"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>  
    <TextView
      android:id="@+id/tv2"
      android:text="               "
      android:textSize="18sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </LinearLayout>

Strings File

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">Using TextViews, EditText and Button</string>
  <string name="button_text">Clear</string>
  <string name="button_text_2">Cleared</string>
</resources>

RemObjects C# Code of Android EditText Demonstration

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

namespace org.me.android_edit_cs
{ 
  public class MainActivity: Activity
  {
    private Button btnClear;
    private EditText edtName;
    private TextView tvGreeting;

    public void ButtonOnClick(View v)    
    { 
      btnClear.Text = R.@string.button_text_2;
      edtName.Text.clear();  //Clear the edit box
      tvGreeting.setText("");  //Clear the label
    }
    public  Boolean EditOnKey(View v ,Integer keyCode, KeyEvent key_event)
    {
      // if key is down and "enter" is pressed
      if ((key_event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) 
      {    
        tvGreeting.setText("Hello " + edtName.getText() + "!");     
        btnClear.setText("Clear");
        return true;
      }
      else
        return false;
    }

    public override void onCreate(Bundle savedInstanceState)
    {
      base.onCreate(savedInstanceState);
      // Set our view from the "main" layout resource.
      ContentView = R.layout.main;
      btnClear = (Button)(findViewById(R.id.btn1));
      edtName = (EditText)(findViewById(R.id.edt1));
      btnClear.OnClickListener = ButtonOnClick;
      edtName.OnKeyListener = EditOnKey;
      // Get TextView from the layout resource.
      tvGreeting = (TextView)(findViewById(R.id.tv2));
    }
  }
}


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