28 Jul

Implement OnClickListener for a view item in Android

Share Button

Implement OnClickListener for a view item in Android

In this topic, I cover a sample application on implementation of OnClickListener for a view item, logging the button click event and a Toast message. Activity in this example holds 4 buttons, for which we need to capture the button clicks within the activity. Activity class here has to implement OnClickListener.
OnClickListener needs to be registered for the northButton view. This is achieved by making a call to the setOnClickListener() method of the button view. Since the task needs to be performed when activity is created, it is appropriate to place in onCreate() method.
Log is for printing messages to the LogCat.  Toast is a notification message that is displayed for certain amount of time. I am providing both log and toast in the example, just for the demo purpose.

Following are the interfaces & code which perform our targets we have mentioned above.

image

image

<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” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”><Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:id=”@+id/northButton”
android:text=”North”/><Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_centerVertical=”true”
android:id=”@+id/eastButton”
android:text=”East”/>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentRight=”true”
android:layout_centerVertical=”true”
android:id=”@+id/westButton”
android:text=”West”/>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:id=”@+id/southButton”
android:layout_alignParentBottom=”true”
android:text=”South”/>
</RelativeLayout>

 

package com.example.yes.clicknlog;import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;public class MainActivity extends ActionBarActivity implements OnClickListener {
private final String TAG = getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button northButton;
Button eastButton;
Button southButton;
Button westButton;
northButton = (Button)findViewById(R.id.northButton);
eastButton = (Button)findViewById(R.id.eastButton);
westButton = (Button)findViewById(R.id.westButton);
southButton = (Button)findViewById(R.id.southButton);
northButton.setOnClickListener(this);
eastButton.setOnClickListener(this);
westButton.setOnClickListener(this);
southButton.setOnClickListener(this);}
public void onClick(View v) {switch (v.getId()){
case R.id.northButton:
Log.i(TAG, “North Button Clicked!”);
Toast.makeText(getApplicationContext(),
“North Button is clicked”, Toast.LENGTH_LONG).show();
break;
case R.id.eastButton:
Log.i(TAG, “East Button Clicked!”);
Toast.makeText(getApplicationContext(),
“East Button is clicked”, Toast.LENGTH_LONG).show();
break;
case R.id.westButton:
Log.i(TAG, “West Button Clicked!”);
Toast.makeText(getApplicationContext(),
“West Button is clicked”, Toast.LENGTH_LONG).show();
break;
case R.id.southButton:
Log.i(TAG, “South Button Clicked!”);
Toast.makeText(getApplicationContext(),
“South Button is clicked”, Toast.LENGTH_LONG).show();
break;
}

}

}

Share Button