0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

3 Answers3

1

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);
  }
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
1

You are missing below code i guess

alertDialog.create().show();

instead of

alertDialog.show();

Uday Ramjiyani
  • 1,407
  • 9
  • 22
0

Well,you only need to add some codes in if-else:(take a try)

@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_delete:
            new AlertDialog.Builder(this)
                    .setTitle("Confirm Deleteing!")
                    .setMessage("Are you sure you want to delete?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //Delete menu item
                            itemlist.remove(item_id);
                            myAdapter.notifyDataSetChanged();
                        }
                    }).setNegativeButton("No", null).show();
            break;
        case R.id.menu_edit:
            break;}