Nested AlertDialog - java

I'm facing a unreasolvable (for me) problem with nested AlertDialog using the following code
final AlertDialog.Builder button_cook_action = new AlertDialog.Builder(this);
final EditText cookMl = new EditText(this);
cookMl.setInputType(InputType.TYPE_CLASS_NUMBER);
button_cook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button_cook_action.setTitle(R.string.kitchen_recipe_button_cook)
.setMessage(R.string.kitchen_recipe_button_cook_volume)
.setView(cookMl)
.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AlertDialog.Builder builderCooking = new AlertDialog.Builder(RecipeActivity.this);
builderCooking.setTitle(recipe.getName())
.setMessage("message");
builderCooking.show();
}
})
.setNegativeButton(R.string.No, null)
.show();
}
});
The first call works fine, but when i call it for a second time it gave me :
FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I already search in this forum but without any success.
If someone has a clue. Thanks in advance :)

You can do it like this - the problem was before if you use the EditText a second time it already has a parent - you need to create a new one each time inside your onClick() :
button_cook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final AlertDialog.Builder button_cook_action = new AlertDialog.Builder(this);
final EditText cookMl = new EditText(this);
cookMl.setInputType(InputType.TYPE_CLASS_NUMBER);
button_cook_action.setTitle(R.string.kitchen_recipe_button_cook)
.setMessage(R.string.kitchen_recipe_button_cook_volume)
.setView(cookMl)
.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AlertDialog.Builder builderCooking = new AlertDialog.Builder(RecipeActivity.this);
builderCooking.setTitle(recipe.getName())
.setMessage("message");
builderCooking.show();
}
})
.setNegativeButton(R.string.No, null)
.show();
}
});

The problem is in the setView of your alertDialog. You have to inflate the layout everytime your create your dialog. In your case, your are inflating an EditText. So either you should create your EditText inside button_cook onClickListener or adopt the solution as posted by #ligi.

Related

Android Adapter AlertDialog error

I have the following code inside an Adapter class I use for a listview in another class.
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Introduceti parola:");
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
hash.put("name", Rooms.name);
hash.put("parola", input.getText().toString());
site = siteul + "/join";
new ATask((ViewHolder) v.getTag()).execute(site);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
The problem is that when I trigger the action I get this error at the builder.show() line
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
I thought the getContext() was at fault, but it doesn't generate any error in this code in the same Adapter class:
Toast.makeText(getContext(), "Cool message!", Toast.LENGTH_LONG).show();
What could be the problem then?
Replace getContext() with ActivityName.this.

NullPointerException when focusing on EditText in AlertDialog

I've got a ListView where the cells/rows in that listView contains a button that presents an AlertDialog but when I tap the EditText to enter some text I get a NullPointerException when the ArrayAdapter's getView method returns.
I've tried
android:descendantFocusability="afterDescendants" & "beforeDescendants" on my ListView
and added android:windowSoftInputMode="adjustPan" which is what the similar topics has marked as correct answers but it's not working for me.
AlertDialog.Builder alert = new AlertDialog.Builder(Menu.this);
alert.setTitle(SubApp.pManager.getLanguageObject().getString("AddNoteToFavTitle"));
final EditText input = new EditText(Menu.this);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveInfoToPrefs(editor, id, input.getText().toString());
}
});
alert.setNegativeButton(SubApp.pManager.getLanguageObject().getString("No"), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
saveInfoToPrefs(editor, id, "");
}
});
alert.show();
Does anyone know any other solution to this annoying issue?
Cheers!

alert box in android is not working

The code is not working. Please help me. It print the replace all string, but further code is not running.
when I debug this, there is no error in the code. It will show the code of alert box.
if(count>0)
{
System.out.println("replace all string name ");
// final Intent intent_ul=new Intent(this, UploadExcel.class);
AlertDialog.Builder alertDialogBuilder_ue = new AlertDialog.Builder(this);
alertDialogBuilder_ue.setTitle("Alert!!");
alertDialogBuilder_ue
.setMessage("Are you sure you want to Replace all the data related to this style ? ")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.delete_style_measurement(style_no);
Log.d("","yes click");
count=0;
mySQLiteAdapter.close();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Log.d("","No click");
count++;
dialog.cancel();
// startActivity(intent_ul);
//finish();
}
});
}
Add these lines before the end of if condition
AlertDialog alertDialog = alertDialogBuilder_ue.create();
alertDialog.show();
You need to add
alertDialogBuilder_ue.show();
in your code
Check with this code. This code working for me
Context context = CurrentActivity.this;
AlertDialog.Builder ad = new AlertDialog.Builder(context);
ad.setTitle("Application");
ad.setMessage("Do you want to proceed?");
ad.setPositiveButton("Yes", new OnClickListener()
{
public void onClick(DialogInterface dialog, int arg1)
{
}
});
ad.setNegativeButton("Cancel", new OnClickListener()
{
public void onClick(DialogInterface dialog, int arg1)
{
}
});
ad.setCancelable(false);
ad.show();
In your code adding alertDialogBuilder_ue.show(); should make the dialog appear.
By some people it has been suggested that you have to use alertDialogBuilder_ue.create(); to get a handle to the AlertDialog that you can then use the .show() method on.
Both are possibilities but you don't have to use the .create() option if you don't need a handle to the AlertDialog

Android AlertDialog how to block second click on the ok button?

Example:
System.out.println("in!");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("test!!!");
dialog.setPositiveButton(R.string.dialog_ok,
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.out.println("Only one click!");
}
});
dialog.show();
Out:
in!
in!
Only one click!
Only one click!
Only one click!
Only one click!
Get the button (positive) and set enabled to false.
System.out.println("in!");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("test!!!");
dialog.setPositiveButton(R.string.dialog_ok,
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// disable on 1st click;
final AlertDialog alertDialog = (AlertDialog)dialog;
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
System.out.println("Only one click!");
}
});
dialog.show();
What i don't understand is why someone could click a second time on an alertdialog, because teh buttons are supposed to close the dialog after performing some action.
Why do you not close the dialog with
dialog.dismiss()
?
You need to disable it. I recommend a flag, which must be stored at the class level.
Boolean hasBeenClicked=false;
System.out.println("in!");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("test!!!");
dialog.setPositiveButton(R.string.dialog_ok,
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (!hasBeenClicked)
{
hasBeenClicked=true;
System.out.println("Only one click!");
}
}
});
dialog.show();

AlertDialog.Builder show(), crash in click handler

My app opens a new view from the main view with:
Intent ElementsIntent = new Intent(this, ElementList.class);
startActivityForResult(ElementsIntent, 0);
which shows a list of elements and when pushing 1 of these elements a view opens up the same way as before with a new Activity. Inside this view I would like to show a AlertDialog in a button click handler, but when I call show() the app crashes.
I am pretty sure it has got somthing to do with the Context not being correct according to where I try and open the dialog, but I have tried making a static context from the main view, I have tried with element.this, which is the class connected to the activity, and I have tried getApplicationContext, and all of these result in an app crash.
I hope someone can explain what I am doing wrong.
Thanks.
Here is the AlertDialog code which crashes:
public void GoBackClickHandler(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(ElementItem.this);
builder.setMessage("Skal ændringer i besvarelse gemmes?")
.setCancelable(false)
.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
if(inputIsValue())
{
UpdateELement task = new UpdateELement();
task.applicationContext = ElementItem.this;
task.execute(1);
}
}
})
.setNegativeButton("Nej", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
If I move this code to the OnCreate, then the alert shows just fine and no app crash. It is only if I place it in a ClickHandler it crashes.
I finally found a soloution to this issue.
I had to save the context of the Activity in a variable in the onCreate method and then use this in the ClickHandler AlertDialog call, then everything works.
Hope this will be of help to someone else with this annoying problem.
public class SplashActivity extends AppCompatActivity implements DialogInterface.OnClickListener {
//Object to hold the listener instance
DialogInterface.OnClickListener listener;
#Override
protected void onCreate(Bundle savedInstanceState) {
//Assign this to listener.
listener = this;
handler.postDelayed(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
builder.setTitle("Alert");
builder.setMessage("Alert Message...!");
//Here pass the listener object.
builder.setPositiveButton("OK", SplashActivity.this.listener);
builder.show();
}
});
}
#Override
public void onClick(DialogInterface dialog, int which) {
SplashActivity.this.finish();
}
}

Categories