Search code examples
androidandroid-intentandroid-widgetdialogcustomdialog

Custom DialogBox Alert in Android is not Prompting


Following is the code I have created for 2 dialog boxes: namely Date Picker and Custom Dialog Box on submit of 2 buttons namely btnselDate and btnAlertDialog.

Date Picker dialog is working properly but there is some problem with Custom Dialog Box. My custom dialogbox displays login form for the user.

Kindly please review it and suggest appropriate suggestions for the same.

Here goes the code:

 public void onClick(View view)
    {
        if(view.getId() == R.id.btnselDate)
        {
            // Date Picket DialogBox

            showDialog(1);
        }
        else if(view.getId()==R.id.btnAlertDialog)
        {
            // Alert Dialog Box

            Context mContext = getApplicationContext();
            Dialog dialog = new Dialog(mContext);

            dialog.setContentView(R.layout.custom_activity);
            dialog.setTitle("Custom Dialog");

            TextView text = (TextView) dialog.findViewById(R.id.tvPwd);
            text.setText("Enter the Password");

            final EditText pwd=(EditText) dialog.findViewById(R.id.etPwd);

            Button btnlogin=(Button) dialog.findViewById(R.id.btnOK);

            btnlogin.setOnClickListener(new View.OnClickListener() {

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

                    //Login Button

                    if(pwd.getText().toString().equals("abc"))
                    {
                        Intent intent=new Intent(MainActivity.this,WelcomeUser.class);
                        startActivity(intent);
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this, "Wrong Password, Try Again", Toast.LENGTH_SHORT).show();
                    }

                }
            });
        }
        else
        {
            Toast.makeText(MainActivity.this, "No Dialog Selected yet", Toast.LENGTH_SHORT).show();
        }
    }

Solution

  • instead of this

      dialog.setContentView(R.layout.custom_activity);
    

    for a coustom layout for dialog you ll have to inflate the layout check the code here

        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        return new AlertDialog.Builder(AlertDialogSamples.this)
            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(R.string.alert_dialog_text_entry)
            .setView(textEntryView)
            .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
    
                    /* User clicked OK so do some stuff */
                }
            })
            .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
    
                    /* User clicked cancel so do some stuff */
                }
            })
            .create();