I am using an alert dialog to ask the user for confirmation of an item addition to an array. I am printing the array size before and after the alert dialog and it seems that both printings are done before the alert dialog shows up.
Log.d("before", ""+wayPoints.length);
AlertDialog.Builder ab = new AlertDialog.Builder(getContext());
ab .setTitle("Add entry");
ab .setMessage("Are you sure you want to add this entry?");
ab .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{wayPoints = ArrayHandler.addAtIndex(wayPoints, node, 1);}
});
ab .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
ab.setIcon(android.R.drawable.ic_dialog_alert);
ab.show();
Log.d("after", ""+wayPoints.length);
The ArrayHandler.addAtIndex method handles the addition of a new item to the provided array.
The problem is it is printing both logging lines before the alert dialog shows up. I need the alert dialog to be done before the printing of the second log.
This is correct behavior. The show method does not wait or block until the dialog is dismissed. Code execution continues after the show method immediately after the dialog is shown. (Actually, the dialog isn't even displayed yet - that happens after you yield control).
You already have setPositiveButton and setNegativeButton handlers, just put the code you want to execute in those handlers when user presses a button.
If you want certain code to run when the dialog is closed regardless of which button is tapped, use setOnDismissListener and put your code there.
Use:
AlertDialog alert1 = ab.create();
alert.show();
Instead of:
ab.show();
Try this
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder .setMessage("Click yes to exit!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish(); }
}) .setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
wayPoints = ArrayHandler.addAtIndex(wayPoints, node, 1);
Log.d("after", ""+wayPoints.length);
} });
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
Related
I'm a beginner in java and I have a question for you do you know how I can create the button of exit? This button can ask me before I close the application "Do you want to close this application? or "Are you sure to close it?" I need to do it for my project and I need help. Pls send me some code.
Your question is very broad, however, an AlertDialog is what you are looking for, this is the implementation:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
1.- Search how create a dialog on Android.
Link -> https://developer.android.com/guide/topics/ui/dialogs?hl=es-419
2.- In your view put a back button with onClickListener, insert showDialog created inside.
3.- Implement the override onBackPressed method, insert showDialogCreated inside.
Example onBackPressed
public void onBackPressed() {
var dialog = CustomDialog.newInstance();
dialog.setCancelable(false);
dialog.show(this.getSupportFragmentManager(), "TAG");
dialog.setOnClickListener((whichViewID, tag, args) => {
// Your Logic
// If is pressed positive button call super.onBackPressed else dialog.dismiss()
});
}
I'm implementing a singleChoiceItems alert dialog in android but i want to have a check such that some items in the array wont show depending on time.
Is this possible or is there any method i could use in place of the alert dialog ?
This is a simple example how to check the time and change the items if "true".
Dialog box is an object, you can call its properties by calling biulder.* in different locations.
String[] colurs ={"Red","Blue","Green"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick Colour")
.setItems(colurs, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
if(currentThreadTimeMillis()>1000){
colurs[0]="New1";
colurs[1]="New2";
builder.setTitle("Pick Colour").setItems(colurs, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
}
builder.create();
builder.show();
Please, i would like to show back details after the user must have input something, back on alert dialog box in Android studio. I used this code below:
editText = (EditText) findViewById(R.id.my_edit_txt);
editText.getText().toString();
But it doesn't show on the confirmation dialog box I created.
It looks like you didn't set the text of your AlertDialog, but this is just an assumption because there is not enough code in your question. Calling editText.getText().toString() does not do anything but return a String. It does not assign it to anything. An example with an AlertDialog would be the following:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(editText.getText().toString());
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
I've took this example from Android Developers and modified it so that it includes the text of your EditText. This code should work because you not only call the toString() method but also assign it's return value to the AlertDialog's message property.
This is my entire code for the alert dialog box:
public void alertdialog(View view){
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder cfmalt = new AlertDialog.Builder(Dashboard.this);
//cfmalt.setMessage("Do you want to quit?").setCancelable(false);
//editText.getText().toString();
cfmalt.setMessage(editText.getText().toString()+"\n"+ vol_edit2.getText().toString());
cfmalt.setMessage(dt.getMonth())
//cfmalt.setMessage("Name:").setMessage(vol_edit2.getText().toString());
cfmalt.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
cfmalt.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
I'm running ussd code from my code. After it returns the results the user will press the ok button to close the the dialog. I want to detect when the user does this.
Let's assume that this is your dialog code:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("dialog's title");
builder.setMessage("dialogs's text");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do stuff when user presses ok button
}
});
builder.setNeutralButton("CANCEL", null); //same here just add the listener
AlertDialog dialog = builder.create();
dialog.show();
//you can use neutralButton as well
I am making an app in which if a user selects a
submenu item I pop up an Alert dialog which asks his confirmation
whether he wishes to save that item in his list and saves it if he
presses yes and doesn't add it if he presses no.
You can use this to show alert:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure to do this?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
doSomeThing();
dialog.cancel();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
You add the element you get from the dialog to the ArrayList you use to feed your Adapter then feed the cursor = new yourAdapter(YourClass.this.getBaseContext(), android.R.layout.simple_list_item_1, dataList);
Call cursor.notifyDataSetChanged(); which tells the list to repopulate with the new data;