Reputation: 51
I am creating android app using eclipce , I used a click listener for a button to show a toast .Every thing is working ok but the problem that I need to click two times in the button to show the toast .Is there any way to force one clicking to show the toast? This is the code that use
public void showAnswer(View view) {
Button b;
b= findViewById(R.id.ans);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplication().getBaseContext(),"ال�يل",Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 0
Views: 500
Reputation: 1936
Use only this if you are assigning showAnswer(View view)
to your button
onclick
event:
public void showAnswer(View view) {
Toast.makeText(getApplication().getBaseContext(),"ال�يل",Toast.LENGTH_SHORT).show();
}
Upvotes: 1
Reputation: 4007
It costs you 2 taps because with the first one, you define your listener and the second one, your listener is called.
You have to define your ClickListener
only one time. Generally, it's done in the onCreate()
method.
Upvotes: 0