When I setNegativeButton and setPositiveButton in the alert dialog, when I run it on my mobile device which is a Hauweii phone. runs perfect, but when I run it on the emulator, the alertDialog shows but doesn't display the Ok or cancel but I can still click them, it's like there invisible or something, can anyone please help me?
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder alertD=new AlertDialog.Builder(Report.this);
alertD.setTitle("Delete?");
alertD.setMessage("Are you sure you want to delete your Bmi ");
final int positionToRemove = position;
alertD.setNegativeButton("Ok", null);
alertD.setPositiveButton("Cancel", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
bmis.remove(positionToRemove);
b.notifyDataSetChanged();
}});
alertD.show();
}
});
Inside your onItemClick
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Report.this);
final int positionToRemove = position;
alertDialog.setTitle("Delete?");
alertDialog.setMessage("Are you sure you want to delete your Bmi "));
// Setting Positive "YES" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
bmis.remove(positionToRemove);
b.notifyDataSetChanged();
dialog.dismiss();
}
});
// Setting Negative "CANCEL" Button
alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
Related
I'm trying to open 2 alert dialogs like permissions, the first one pops up and dismiss when I press the positiveButton, then It inflates the second one, but when I press positiveButton on second alertdialog it does not dosmiss and keeps showing it
private void requestXiaomiBootPermission(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Automatic boot");
builder.setMessage("we need this permission to work");
builder.setCancelable(false);
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
addAutoStartup();
requestXiaomiBackgroundPermission();
dialog.dismiss();
}
}).show();
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
builder.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
builder.show();
}
private void requestXiaomiBackgroundPermission(){
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
builder2.setTitle("Popup window");
builder2.setMessage("we need this permission to work");
builder2.setCancelable(false);
builder2.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
addPermissionsOnBackgroud();
dialog.dismiss(); // --> Here it does not dismiss when this dialog inflates
}
}).show();
builder2.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
builder2.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
builder2.show();
}
So the circuit is this
I open the app and the first dialog pops up ( requestXiaomiBootPermission() ) , then when I press the positiveButton allow it opens the second dialog ( requestXiaomiBackgroundPermission() ) but when I press allow on this one, dialog.dismiss() does not dismiss my dialog and keeps there, any solution?
Don't invoke show() after every setXXXXXButton() call. Just invoke it once, at the end. You're creating 4 boot permission dialogs, and 4 background permission dialogs, and they're all getting stacked on top of each other.
You are opening both dialog 4 times. Remove show() method after all three button.just keep show() method at end.
private void requestXiaomiBootPermission(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Automatic boot");
builder.setMessage("we need this permission to work");
builder.setCancelable(false);
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
I want my AlertDialog to pop up as soon as my EditText has focus. Right now, I have to click on my EditText twice.
The first time I click my EditText, the soft keypad appears (for typing directly in my Edittext). Then, the second time I click the EditText, the AlertDialog appears.
How can I have it so only one click on the EditText is needed for the AlertDialog to pop up?
Here's my code:
In my onCreate...
//when user clicks on "commentName" EditText we want a new AlertDialog to open
commentName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
builder.setTitle("Ur Comment:");
//start the following xml file/ layout
View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
I got it to work according to your description with an OnFocusChangeListener.
Just replace the entire snippet from your question with the following. And paste in the AlertDialog code below the comment.
// commentName is your EditText
commentName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
return; // Prevents alert from being shown when losing focus.
}
// Your AlertDialog code goes here
}
});
The if (!hasFocus) prevents the alert from being shown when the user clicks something else that makes the EditText lose focus.
This did it for me, OnTouchListener:
//when user clicks on "commentname" edittext we want a new textbox to open
commentname.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
builder.setTitle("Ur Comment:");
//start the following xml file/ layout
View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return true;
}
return false;
}
});
Another way you could do this with least code is by simply disabling focus on the EditText. You can do that by adding below attribute to your EditText...
<EditText
...
android:clickable="true"
android:focusable="false"/>
Then set OnClickListener on your EditText, and show the dialog in onClick() method.
i have this:
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(updatedActivity.this);
builder.setTitle("Choose the currency you would like to exchange.");
//list of items
String[] items = getResources().getStringArray(R.array.countries_array);
builder.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
String positiveText = getString(android.R.string.ok);
builder.setPositiveButton(positiveText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//So here i wanna a code like : If pressbutton = buttonto, then buttonto.setText(selecteditem..)
}
});
String negativeText = getString(android.R.string.cancel);
builder.setNegativeButton(negativeText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// negative button logic
}
});
AlertDialog dialog = builder.create();
// display dialog
dialog.show();
}
In the on click event, i wanna know which the button was pressed, so for example if buttonto.ispressed then buttonto.settext(the selected item)
So how to do it
Thanks
I am currently using an app that needs to utilize a lot of AlertDialogs. I've currently coded a basic one here:
protected void StopButton () {
AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainActivity.this);
StopDialog.setTitle(R.string.Stop_Title);
StopDialog.setMessage(R.string.Stop_Message);
StopDialog.setPositiveButton(R.string.Yes_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
finish();
}
});
StopDialog.setNegativeButton(R.string.No_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
finish();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
}
The StopButton works and the Dialog comes up when I call for it. However, the finish(); function does not work.
Upon review, I found that finish(); did not finish the Dialog, rather the entire app. I know I need to get a AlertDialog.cancel in there.
The problem is this: as you can see, the AlertDialog is only created AFTER the StopDialog is finished.
How can I set an AlertDialog.finish() before StopDialog is finished?
Replace finish() with dialog.dismiss() like this:
AlertDialog.Builder StopDialog = new AlertDialog.Builder(TestActivity.this);
StopDialog.setTitle("Title");
StopDialog.setMessage("Stop");
StopDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
dialog.dismiss();
}
});
StopDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
dialog.dismiss();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
There is no such method finish() for AlertDialog. finish() refers to Activity class and will finish the current Activity. You should instead use dismiss().
You can use the DialogInterface object like dialog.dismiss() to dismiss the AlertDialog.
You can go through the documentation for a broader idea :)
http://developer.android.com/guide/topics/ui/dialogs.html
I want to show confirmation dialog box to user that when user click on yes button open listview to select.
Use this link..............................
http://rajeshvijayakumar.blogspot.in/2013/04/alert-dialog-dialog-with-item-list.html
You can go like this
Listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
build = new AlertDialog.Builder(YourActivity.this);
build.setTitle("Title name");
build.setMessage("?");
build.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//IF yes button pressed then write here
}
dialog.cancel();
}
});
build.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return true;
}
});