Sh R S
Sh R S

Reputation: 51

Toast is showing after clicking the button two times

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

Answers (2)

Benkerroum Mohamed
Benkerroum Mohamed

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

Bruno
Bruno

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

Related Questions