-2

I am intending to assign a click listener to a TextView. First I implemented View.OnClickListener in MainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ...
    TextView title = findViewById(R.id.title);
    title.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
        }
    });
}

Then I set android:clickable="true" on the TextView.

However, despite overriding the onClick mehtod, an error pops up saying:

Class 'MainActivity' must either be declared abstract or implement abstract method 'onClick(View)' in 'OnClickListener'

What can I do to dodge this error?

Wais Kamal
  • 5,858
  • 2
  • 17
  • 36

4 Answers4

1

The root of the issue is with your activity declaration and there are two ways to deal with it.

  • Let your activity implement the View.onClickListener and you pass the activity to the textview's setOnClickListener
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ...
    TextView title = findViewById(R.id.title);
    title.setOnClickListener(this);

    TextView title2 = findViewById(R.id.textview2);
    title2.setOnClickListener(this);


   // implement the onClick method in your MainActivity class
        @Override
        public void onClick(View view) {
            switch(view.getId()){
               case R.id.TextView1 : //logic for textview1
                   break;

               case R.id.TextView2: // logic for textview2
                   break;
            }
        }
}
  • Modify your Activity function declaration and create a a new instance of View.OnClickListener in setOnClickListener method.
public class MainActivity extends AppCompatActivity {
    ...
    TextView title = findViewById(R.id.title);
    title.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
        }
    });
}
Sekiro
  • 1,537
  • 1
  • 7
  • 21
  • Regarding the first method, I would like to do it this way but the problem is I have more than one button to which I want to assign a click listener. Can you please clarify how to assign a different event listener for each button? – Wais Kamal Jan 22 '21 at 11:51
  • @WaisKamal check the answer now and passing the `clickListener` will be the same – Sekiro Jan 22 '21 at 11:57
  • @WaisKamal is your problem solved mate? – Sekiro Jan 22 '21 at 14:23
1

Since you are implementing View.OnClickListener you can make minor changes to your code to work,

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   {
        ...
        TextView title = findViewById(R.id.title);
        title.setOnClickListener(this);
    }
    //Outside of onCreate()
    @Override
    public void onClick(View v){ 
    
    //Your code here 
    
      }
    }
Kidus
  • 454
  • 5
  • 10
0

Remove implements View.OnClickListener from your class declaration.

Håkon Schia
  • 931
  • 1
  • 7
  • 12
0

According to my understanding, once you implement an interface you have to override all its methods. So is either you remove the local declaration of the onClick inside the onCreate and override the methods from the interface you implemented.