30
Jan
See the previous post to create a default Android project in Eclipse (with Android Development Kit).
Replace the res/layout/main.xml with these lines.
<?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"> <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Type your name here:"/> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_below="@id/label"/> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:text="OK" /> <Button android:id="@+id/xxl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /> </RelativeLayout>
Edit the main activity java source file eg: src/App1.Pkg1/Act1.java
and add/replace these lines (judicially).
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;
public class Act1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//TextView tv = new TextView(this);
//tv.setText("Hello Schogini");
//setContentView(R.layout.main);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//OK button
final Button button = (Button) findViewById(R.id.ok);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText et = (EditText) findViewById(R.id.entry);
//et.append(" Sree= " + et.getText().toString());
et.setText("Welcome to CETEX2010 " + et.getText().toString());
} });
//Cancel Button
final Button xxl = (Button) findViewById(R.id.xxl);
xxl.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
EditText et = (EditText) findViewById(R.id.entry);
//et.append(" Sree= " + et.getText().toString());
et.setText("");
}
}); } }






One Response to “Android: Adding Button Action and Reading Text Box”
[...] this article: Android: Adding Button Action and Reading Text Box | sree.cc Share and [...]