Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
        }
    });
like image 729
Yvonne Avatar asked Apr 28 '11 01:04

Yvonne


2 Answers

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();
        }
    });
like image 173
Jaydeep Khamar Avatar answered Oct 03 '22 05:10

Jaydeep Khamar


The code above proved useful but I used "this" (not "ad") for the context:

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

This is easier for copying and pasting ;-)

Thanks for the previous code I woulnd have found the solution above without it.

like image 43
Ev Ert Avatar answered Oct 03 '22 06:10

Ev Ert