Published using Google Docs
View OnClickListener Interface
Updated automatically every 5 minutes

View OnClickListener Interface

Using Android Studio 1.4

1) Create New Project

Application Name: MyOnClickListener1

Select API 14:Android 4.0 (IceCreamSandwich)

Follow the wizard to create New Blank Activity.

2) Edit res/layout/content_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

   xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:tools="http://schemas.android.com/tools"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   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"

   app:layout_behavior="@string/appbar_scrolling_view_behavior"

   tools:showIn="@layout/activity_main"

   tools:context=".MainActivity">

   <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:id="@+id/changingTextView"

           android:layout_width="fill_parent"

           android:layout_height="wrap_content"

           android:text="Hello"

           android:textSize="26px"

           android:gravity="center_horizontal"

           android:textColor="#23cf34" />

       <RelativeLayout

           android:id="@+id/relativeLayout1"

           android:layout_width="fill_parent"

           android:layout_height="fill_parent" >

           <Button

               android:id="@+id/leftButton"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_alignParentLeft="true"

               android:layout_alignParentTop="true"

               android:layout_marginLeft="50dp"

               android:layout_marginTop="50dp"

               android:text="Left"

               />

           <Button

               android:id="@+id/rightButton"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_alignParentRight="true"

               android:layout_alignParentTop="true"

               android:layout_marginRight="50dp"

               android:layout_marginTop="50dp"

               android:text="Right"

                />

       </RelativeLayout>

   </LinearLayout>

</RelativeLayout>

OUTCOME.

3) Edit MainActivity.java

package com.notarazi.myonclicklistener1;

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   // Declare UI elements

   private TextView changingTextView;

   private Button firstButton;

   private Button secondButton;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

       setSupportActionBar(toolbar);

       setButons();

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

       fab.setOnClickListener(new View.OnClickListener() {

           @Override

           public void onClick(View view) {

               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

                       .setAction("Action", null).show();

           }

       });

   }

   private void setButons() {

       // Initialize the UI components

       changingTextView = (TextView) findViewById(R.id.changingTextView);

       firstButton = (Button) findViewById(R.id.leftButton);

       // When we creating a button and if we expect that to use for event handling we have to set the listener

       firstButton.setOnClickListener(this);

       secondButton = (Button) findViewById(R.id.rightButton);

       secondButton.setOnClickListener(this);

   }

   @Override

   public boolean onCreateOptionsMenu(Menu menu) {

       // Inflate the menu; this adds items to the action bar if it is present.

       getMenuInflater().inflate(R.menu.menu_main, menu);

       return true;

   }

   @Override

   public boolean onOptionsItemSelected(MenuItem item) {

       // Handle action bar item clicks here. The action bar will

       // automatically handle clicks on the Home/Up button, so long

       // as you specify a parent activity in AndroidManifest.xml.

       int id = item.getItemId();

       //noinspection SimplifiableIfStatement

       if (id == R.id.action_settings) {

           return true;

       }

       return super.onOptionsItemSelected(item);

   }

   // Have to implement with the OnClickListner

   // onClick is called when a view has been clicked.

   @Override

   public void onClick(View v) {

       // getId() returns this view's identifier.

       if(v.getId() == R.id.leftButton){

           // setText() sets the string value of the TextView

           changingTextView.setText("You clicked Left");

       }else if(v.getId() == R.id.rightButton){

           changingTextView.setText("You clicked Right");

       }

   }

}

OUTCOME.

DOWNLOAD

MyOnClickListener1.zip

REFERENCES

http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html 

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html 

http://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference 

https://anujarosha.wordpress.com/2011/11/13/how-to-implements-onclicklistener-for-a-view-item-in-android/ 

http://programmers.stackexchange.com/questions/110106/what-is-the-proper-way-to-implement-the-onclicklistener-interface-for-many-butto