Ad
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.
Ad
Answer
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);
}
}
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad