Android Button Click

Android Button Click

In Android development, hope you are familiar with hello world example and you want to know how to perform action on button click event. In below tutorial we will take input from user and display text in textview control.

Android Button click event.

There are 3 controls we would be using for this tutorial. Namely, TextView, EditView and Button Control. On click of button, we will take text entered by user and display in label.

You can refer the layout used for GUI representation. You can drag and drop appropriate controls to generate user interface.

<?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:layout_gravity="center_horizontal"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawHorizontalTrack="true"
    android:scrollbarAlwaysDrawVerticalTrack="true" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

The code behind Java package look like below one.

package satalaj.demo.LayoutDemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LayOutDemoActivity extends Activity {
    /** Called when the activity is first created. */
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button  go = (Button) findViewById(R.id.button1);
        
        go.setOnClickListener( new View.OnClickListener() {
            
            public void onClick(View v) {
                
                EditText input = (EditText) findViewById(R.id.editText1);
                TextView tv = (TextView) findViewById(R.id.textView1);            
                            
                tv.setText( input.getText() );
                
            }
        });
        
    }
    
}

Description:

  Button  go = (Button) findViewById(R.id.button1);

Finds button control by it’s view ID.

go.setOnClickListener

Attach click event to button control.

input.getText();

Returns the string text entered by user.

tv.setText(“User Entered string is ”);

Set a text to TextView control.

Tags:

Android Button ClickAndroid screen navigation example using Activity and IntentAndroid WebView ExampleJquery button click eventAndroid EditText TutorialAndroid Tutorial For BeginnersJQuery Radio Button CheckedJquery Radio Button CheckedBest Android ApplicationsJQuery On Click Change Div ContentHow to remove android applications

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.