Skip to content Skip to sidebar Skip to footer

Android | Multiple Onclicklistener In Dialog

Inside my Activity I start a simple dialog. final Dialog myDialog = new Dialog(this); myDialog.setContentView(R.layout.testing); ... My testing.xml Layout consists of nothing but

Solution 1:

Make your Activity implement OnClickListener and then process the onClick event like below:

@OverridepublicvoidonClick(View v) {
    switch (v.getId()) {
    case R.id.img1:
        ...
        break;
    case R.id.img2:
        ...
        break;
    }
}

Solution 2:

  1. You should let your Activity/Fragment implement the OnClickListener.
  2. When you do that you will have to override the onClick method in that particular activity/fragment.
  3. Set the onClickListeners on the images as follows:

    img_1.setOnClickListener(YourActivity.this);
    
  4. Then in that onClick method you can put a switch case or an if else if case as follows

    @Override  
    public void onClick(View v)
    {   
        if(v==img_1) {  
              //do this  
        } else if(v==img_2) {  
            //do that  
        }...  
    }
    

    or

    @OverridepublicvoidonClick(View v)
    {
        switch (v.getId()) {
            case img_1.getId(): // do thisbreak;
            case img_2.getId(): // do thatbreak;
            .
            .
            .
            default : break;
        }
    }
    

Post a Comment for "Android | Multiple Onclicklistener In Dialog"