Skip to content
Advertisement

how to add button click event in android studio

So I have done some research, and after defining you button as an object by the code

JavaScript

here is my problem

JavaScript

then your OnClick() event…

Problem:

When I type in the “this”, it says:

JavaScript

I have no idea why?

here is the code from the .java file

JavaScript

Advertisement

Answer

SetOnClickListener (Android.View.view.OnClickListener) in View cannot be applied to (com.helloandroidstudio.MainActivity)

This means in other words (due to your current scenario) that your MainActivity need to implement OnClickListener:

JavaScript

This:

JavaScript

means that you want to assign listener for your Button “on this instance” -> this instance represents OnClickListener and for this reason your class have to implement that interface.

It’s similar with anonymous listener class (that you can also use):

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement