Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to call more than one onclick event for more than one button in android

Tags:

android

button

I have small query in android. I have one activity in which it has 3 buttons and on one button click I am displaying the AlertDialog box and on second button click I am displaying the List Dialog Box and On 3rd button click I am toggle the OnScreen Key board in android.

my problem is that I was able to call the first button and when I click the 2nd and 3rd button they are not responding and also I have checked by inserting breakpoints on respective click event functions, the control flow is not at all going inside the functions. simple it skips the fun and going to the end of the main OnCreate function.

here is the code I have used for the first 2 buttons...

        Button btn=(Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            ShowListDialog();
        }
    });

    btn=(Button) findViewById(R.id.btnShowList);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ShowOneDialog();
        }
    });

I am calling the functions ShowOneDialog() and ShowListDialog() functions and hope that functions are not needed and for the 3rd button click event is here below...

btn=(Button) findViewById(R.id.btnToggle); btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            InputMethodManager inmgr=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inmgr.toggleSoftInput(0, 0);
        }
    });
like image 719
G K Avatar asked Dec 07 '25 06:12

G K


1 Answers

Create an separate OnClickListener and add this Listener to all buttons as:

 Button btn=(Button) findViewById(R.id.button1); 
 Button btn1=(Button) findViewById(R.id.btnShowList);
 ....

 btn.setOnClickListener(buttonclickhandler);
 btn1.setOnClickListener(buttonclickhandler);
 ....

 View.OnClickListener buttonclickhandler = new View.OnClickListener() {
    public void onClick(View v) {
       @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            switch(v.getId())
            {
            case R.id.button1:ShowListDialog();
                break;
            case R.id.btnShowList: ShowOneDialog();
            break;
            }
        }
    }
  }
like image 180
ρяσѕρєя K Avatar answered Dec 09 '25 19:12

ρяσѕρєя K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!