Wednesday, October 9, 2013

Button Click Listener

Implement Click Listener for your Button




In this tutorial we are implementing click listener for two buttons.

File: res/layout/main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txt_head"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/text_value"
        android:textSize="20sp"
        android:padding="20dp"
        android:textColor="#7B68EE" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnOK"
        android:layout_alignBottom="@+id/btnOK"
        android:layout_alignRight="@+id/txt_head"
        android:layout_marginRight="58dp"
        android:text="Cancel" />

    <Button
        android:id="@+id/btnOK"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt_head"
        android:layout_toLeftOf="@+id/btnCancel"
        android:text="OK" />


</RelativeLayout>



1. Separate listeners for two buttons

In this case we will assign separate listeners for two different buttons, and we will have to define the response of that button click.

File: src/MainActivity.java


package com.example.eventlistener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

private Button btn_Ok,btn_Cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_Ok = (Button)findViewById(R.id.btnOK);
btn_Cancel = (Button)findViewById(R.id.btnCancel);
btn_Ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub 
Toast.makeText(getApplicationContext(), "OK button was Click", Toast.LENGTH_LONG).show();
}
});
btn_Cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Cancel button was Click", Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}


2. One listener for two buttons

In this we will assign one listener to multiple buttons, and we will have to define inside the listener which button has been clicked.

File: src/MainActivity.java


package com.example.eventlistener;

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

public class MainActivity extends Activity {

private Button btn_Ok, btn_Cancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn_Ok = (Button) findViewById(R.id.btnOK);
btn_Cancel = (Button) findViewById(R.id.btnCancel);

// create a listener
OnClickListener oclBtn = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
        case R.id.btnOK:
          // OK button
          Toast.makeText(getApplicationContext(), "OK was clicked", Toast.LENGTH_LONG).show();
          break;
        case R.id.btnCancel:
          // Cancel button
         Toast.makeText(getApplicationContext(), "Cancel was clicked", Toast.LENGTH_LONG).show();
          break;
        }
}
};
btn_Ok.setOnClickListener(oclBtn);
btn_Cancel.setOnClickListener(oclBtn);

}
}

The difference in implementation between this and previous code is that now we have created one listener-object for both buttons, but on previous case - we had two listener objects, one for each button. There is a rule - the less objects you create the better, as memory is allocated for each object and it is a quite limited resource, especially for mobile devices. 

There is one more way of creating a listener, which does not require creating an object. We will use an object that is already created - Activity.

Activity as a listener

Here we are making an Activity implementing View.OnClickListener interface and fill its onCreate method.

File: src/MainActivity.java


package com.example.eventlistener;

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

public class MainActivity extends Activity implements OnClickListener {

private Button btn_Ok, btn_Cancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn_Ok = (Button) findViewById(R.id.btnOK);
btn_Cancel = (Button) findViewById(R.id.btnCancel);

btn_Ok.setOnClickListener(this);
btn_Cancel.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// define the button that invoked the listener by id
switch (v.getId()) {
case R.id.btnOK:
// OK button
Toast.makeText(getApplicationContext(), "OK was clicked",
Toast.LENGTH_LONG).show();
break;
case R.id.btnCancel:
// Cancel button
Toast.makeText(getApplicationContext(), "Cancel was clicked",
Toast.LENGTH_LONG).show();
break;
}
}
}



0 comments:

Post a Comment

    Blogger news

    MIND MEDIA INNOVATIONS PVT LTD
    25/2953(2), Old GPO Building,
    Near Ayurveda College,
    Kunnumpuram,
    Thiruvananthapuram-695001
    Phone: +91 471 257 1001

    Email us @ :
    mindmediateam@gmail.com

    Total Pageviews

    Share us

    Cool Social Media Sharing Touch Me Widget by Blogger Widgets

    About

    Our hands with you, towards Android Technology