2

I copied the entire code from here

Android custom numeric keyboard

and used it inside an AlertDialog. Now when I debug the application, the onClick() isn't getting invoked.

AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setView(R.layout.keypad_layout);
                builder.setCancelable(false).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                builder.setPositiveButton("Modify", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.create().show();\

The alert dialog is showing and positive and negative buttons work only problem is that I cannot access the views inside the layout

Community
  • 1
  • 1
Geet Choubey
  • 1,069
  • 7
  • 23

2 Answers2

6

Turns out, AlertDialog returns a view

AlertDialog dialog = builder.create();
dialog.show();
mPasswordField = (EditText) dialog.findViewById(R.id.password_field);

This did the trick for me.

Geet Choubey
  • 1,069
  • 7
  • 23
0

You need to inflate the layout to a View instance first, then you can set the OnClickListeners for the keys:

LayoutInflater inflater = LayoutInflater.from(context);
View mView = inflater.inflate(R.layout.keypad_layout, null);
builder.setView(mView);

final TextView key8 = (TextView) mView.findViewById(R.id.t9_key_8);
key8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // do something
            }
        });

repeat that what I have done for key8 with the other keys

Chris623
  • 2,464
  • 1
  • 22
  • 30