Search code examples
androidandroid-alertdialog

Alert dialog on menu item click


Code

public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.name) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(Profile.this);
        alertDialog.setTitle("Enter Name");
        final EditText input = new EditText(Profile.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        input.setLayoutParams(lp);
        alertDialog.setView(input);
        alertDialog.setPositiveButton("Save",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String mName = input.getText().toString();
                        mDatabaseReference.child("UserData").child(UID).child("Name").setValue(mName);
                        dialog.cancel();
                        Toast.makeText(getApplicationContext(),
                                        "Name successfully changed", Toast.LENGTH_SHORT).show();
                        }
                });
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();

    }
    return super.onOptionsItemSelected(item);
  }
}

Never worked with alert dialog which has an EditText so not really sure on why this code isn't working. I want an EditText to be displayed on menu item click.


Solution

  • Try it with switch (item.getItemId()) case instead of if. Try with below mentioned changes and with that i can able to see Dialog as you want.

    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.name:
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(Profile.this);
                alertDialog.setTitle("Enter Name");
                final EditText input = new EditText(Profile.this);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                input.setLayoutParams(lp);
                alertDialog.setView(input);
                alertDialog.setPositiveButton("Save",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                String mName = input.getText().toString();
                                mDatabaseReference.child("UserData").child(UID).child("Name").setValue(mName);
                                dialog.cancel();
                                Toast.makeText(getApplicationContext(),
                                                "Name successfully changed", Toast.LENGTH_SHORT).show();
                                }
                        });
                alertDialog.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                alertDialog.show();
                return true;
    
        }
        return super.onOptionsItemSelected(item);
      }
    }