I am having a problem right now with setOnClickListener
.
When i put this following line:
button.setOnClickListener(this);
And run the application then it does not run and shows a message that "Application closed forcefully".
Could you please help me how I can set button onclick event in Android 2.2?
You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Save this answer.
One of the most usable methods in android is setOnClickListener method which helps us to link a listener with certain attributes. setOnClickListener is a method in Android basically used with buttons, image buttons etc. You can initiate this method easily like, public void setOnClickListener(View.OnClickListner)
setOnClickListener(this); 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. If you have more than one button click event, you can use switch case to identify which button is clicked.
Kotlin setOnClickListener for Button Android Button widget is a UI element generally used to receive user actions as input. You can click on a Button, long press, etc. In this tutorial, we shall learn to set OnClickListener for Button.
See if the code below works for you...
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show();
}
});
Remember to add });
at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With