Geet Choubey
Geet Choubey

Reputation: 1077

Unable to access views inside an AlertDialog

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

Upvotes: 2

Views: 1748

Answers (2)

Geet Choubey
Geet Choubey

Reputation: 1077

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.

Upvotes: 6

Chris623
Chris623

Reputation: 2532

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

Upvotes: 1

Related Questions