Android Button & TextView Example
|
|
In this tutorial, I will show you how to display a normal button, add a click listener, when user click on the button the text of TextView will change. Please note that this project is developed in Eclipse 3.7, and tested with Android 2.2.
1. Adding a Button & TextView
Open “res/layout/main.xml” file, add a button & textview as-
(File: res/layout/main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click on the Button to change this text" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click on the Button to change this text" />
</LinearLayout>
2. Add click listener to the button
Attach a click listener to the button. When user click on it, the text of TextView will change.
(File: ButtonDemoActivity.java)
package com.sunil.buttondemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ButtonDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Creating Button variable
Button button = (Button) findViewById(R.id.bt);
//Adding Listener to button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Creating TextView Variable
TextView text = (TextView) findViewById(R.id.tv);
//Sets the new text to TextView (runtime click event)
text.setText("You Have click the button");
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ButtonDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Creating Button variable
Button button = (Button) findViewById(R.id.bt);
//Adding Listener to button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Creating TextView Variable
TextView text = (TextView) findViewById(R.id.tv);
//Sets the new text to TextView (runtime click event)
text.setText("You Have click the button");
}
});
}
}
3. Demo
Run the application.
1. Result, a normal button & textview.
2. Click on the button, text of TextView will change as-
That's it ! If you like this tutorial let me know by your comments. Thanks !










nice tutorial for dummies. please bring more tutorial. Thanks
Thanks. As said super for dummies like me. My first attempt :) for Android
Post a Comment