Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

Dialog onClick not responding

I have this custom dialog class:

public class CustomDialog extends Dialog implements android.view.View.OnClickListener{

      public Activity c;
      public Dialog d;
      public Button cancelButton;
      public LinearLayout smsButton, emailButton;

      public CustomDialog(Activity a) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
      }

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        smsButton = (LinearLayout)findViewById(R.id.smsButton);
        emailButton = (LinearLayout)findViewById(R.id.emailButton);
        cancelButton = (Button)findViewById(R.id.cancelButton);

      }

      @Override
      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.smsButton:
            Toast.makeText(getContext(), "SMS", Toast.LENGTH_SHORT).show();
            break;
        case R.id.emailButton:
            Toast.makeText(getContext(), "EMAIL", Toast.LENGTH_SHORT).show();
            break;
        case R.id.cancelButton:
            dismiss();
            break;
        default:
        break;
        }
        dismiss();
      }

}

and in my main activity I am implementing this as:

btn = (Button)findViewById(R.id.reportVAW);

        btn.setOnClickListener(new OnClickListener() {

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


                CustomDialog cd = new CustomDialog(MainActivity.this);
                cd.show();

            }
        });

Everything seems to run fine but the onClickListeners for the buttons don't seem to work. In the custom dialog class as seen in the code:

@Override
      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.smsButton:
            Toast.makeText(getContext(), "SMS", Toast.LENGTH_SHORT).show();
            break;
        case R.id.emailButton:
            Toast.makeText(getContext(), "EMAIL", Toast.LENGTH_SHORT).show();
            break;
        case R.id.cancelButton:
            dismiss();
            break;
        default:
        break;
        }
        dismiss();
      }

All of this does not work at all, any idea on why?

Upvotes: 0

Views: 68

Answers (1)

flx
flx

Reputation: 14226

You never set the onClickListener to the Buttons. That's what's missing:

onCreate(Bundle savedInstanceState) {
    // your existing code
    smsButton.setOnClickListener(this);
    emailButton.setOnClickListener(this);
    cancelButton.setOnClickListener(this);
}

Upvotes: 1

Related Questions