Search code examples
androidonclickonclicklistenertoast

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();
            }
        });

    }

Solution

  • 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.