how to set an onclick listener for an imagebutton in an alertdialog

Yvonne picture Yvonne · Apr 28, 2011 · Viewed 50.9k times · Source

I have a layout with an ImageButton that is inflated in an AlertDialog, where/how should I set an onClick listener?

Here's the code I tried using:

    ImageButton ib = (ImageButton) findViewById(R.id.searchbutton);
    ib.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(TravelBite.this, "test", Toast.LENGTH_SHORT).show();
        }
    });

Answer

Jaydeep Khamar picture Jaydeep Khamar · Apr 28, 2011

Try to put like this in ur code

e.g:-if your alertdialog's object is ad,then

 ImageButton ib = (ImageButton) ad.findViewById(R.id.searchbutton);
    ib.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(TravelBite.this, "test", Toast.LENGTH_SHORT).show();
        }
    });