Multiple Event Listener Issue

+1 Vedh Koutha · November 16, 2015
Whenever I try to create multiple click listeners, it always gives me an error with the second event listener. 
For example, when I create an OnLongClickListener, it works fine, but when I do a OnClickListener after that, it gives me an error at the code 

new Button.OnClickListener () {

This is the entire code I'm using - 

package com.example.(disclosed).random;

import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

import static android.widget.Button.*;

public class RANDOM extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(;

Button MyButton = (Button) findViewById(;

new OnLongClickListener(){
public boolean onLongClick(View v) {
TextView MyText = (TextView) findViewById(;
MyText.setText("Stop holding down button dude, your hurting it. " +
"you murderer!");
return true;
Button.OnClickListener() {
public void OnClick(View v){
TextView MyText = (TextView) findViewById(;

FloatingActionButton fab = (FloatingActionButton) findViewById(;
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();

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

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 == {
return true;

return super.onOptionsItemSelected(item);

Blue = Parts focused on      Red = Error part

Any clarification on the subject would be appriciated!


Post a Reply


Oldest  Newest  Rating
0 Gary Whitney · November 18, 2015
You really should include the error when something is in red text.
Your error was
Error:(29, 46) error: <anonymous path is not abstract and does not override abstract method onClick(View) in OnClickListener

This translates into you need to have your activity implement the onClickListener and the subsequent onClick method.
public class MainActivity extends AppCompatActivity implements Button.OnClickListener{
or more generally
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

(Apparently long click doesn't have this interface requirement.)

Then you can declare the method after the onCreate() like this:
public void onClick(View v) {
TextView MyText = (TextView) findViewById(;

The next problem you are going to have is if you put more than one button in the layout.
How does your method know which button was clicked?
You will have to use a switch and v.getId() call to know which view item was clicked.

I find it is a lot easier to just use the onClick attribute of the button(s) and create the matching method.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator