Android Button

In this session will learn to create Button.

Create new project in eclipse (Android-First-Programm).

Click on res folder and then layout, now open required xml where you want to use button.

eclipse-res-layout

We can create any control like button using both layouts

1) Graphical Layout

2) Xml Layout

 

1) Using “Graphical Layout” Drag button from “Form Widgets”.

button-graphical-layout

Double click on button and set its properties in xml layout.

2) In Xml Layout write this code to create button.

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

Now by reference we can use button in code and can use its events.

Open Activity (.java file) to use that button.

Activity is located in src folder -> package and choose activity where you want to use button.

eclipse-folder-src-activity

To access button from xml we use its reference using this code.

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

 

Using “findViewById();” we can access reference of any control like button from xml layout.

“button1” is id of that button that we want to use and it must be exist in xml layout that we have assigned in “setContentView(R.layout.main);”. So complete code will be

public class AndroidButtonActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btn=(Button) findViewById(R.id.button1);
}

 

button1 is exists in main layout. We can set reference of layout using “setContentView(R.layout.main);

 

Button click event:

To set buttons click event use this code.

public class AndroidButtonActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
});

}

 

To check is button is clicked show message using Toast.

public class AndroidButtonActivity extends Activity {
/** Called when the activity is first created. */
Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mContext=this;

Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "button1 is clicked", Toast.LENGTH_SHORT).show();
}
});
}
}

Other way to set click event in button is, we can use function name in xml.

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

And define “button1Click” function in activity, without using button reference.

For your understanding I make two buttons and use button click events by two types.

See complete xml or java files.

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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

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

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button2Click"
        android:text="Button2" />

</LinearLayout>

 

AndroidButtonActivity.jave

package com.itPearl.androidButton;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidButtonActivity extends Activity {
    /** Called when the activity is first created. */
    Context mContext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mContext=this;

        Button btn=(Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "button1 is clicked", Toast.LENGTH_SHORT).show();
            }    
        });

    }

    public void button2Click(View view){
        Toast.makeText(mContext, "button2 is clicked", Toast.LENGTH_SHORT).show();
    }
}

 

Leave a comment

Your email address will not be published.