AlertDialog not showing in Fragment - java

I've got a really strange problem where on some KitKat devices, my simple yes/no AlertDialog will appear behind the current fragment and not in the foreground. The reason I say the dialog appears behind the current fragment is because the dialog appears in the foreground only after I rotate the device. The app has a MainActivity that switches between different fragments that take up most of the screen.
MainActivity.java
#Override
public void onBackPressed() {
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.exit_confirm_summary))
.setTitle(getString(R.string.exit_confirm_title))
.setCancelable(true)
.setPositiveButton(getString(R.string.ok),
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//...
}
})
.setNegativeButton(getString(R.string.cancel),
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//...
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
After doing some research I found that it is best to use DialogFragment when using Fragments in your app, so I changed my code to this:
MainActivity.java
#Override
public void onBackPressed() {
AlertDialogFragment adf = new AlertDialogFragment();
adf.setRetainInstance(true);
adf.show(getFragmentManager(), "dialog");
}
AlertDialogFragment.java
public class AlertDialogFragment extends DialogFragment {
public AlertDialogFragment() {}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setTitle("Alert DialogFragment")
.setMessage("AlertDialogFragment Test")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
}).create();
}
}
However, the effect is still the same. The dialog should appear when I press the back button, but is only visible after I press the back button and then rotate the device. It also becomes visible after I go home and come back into the app. I've noticed it only happens on a few devices but I'd like to get rid of this problem for good.
Note: this behavior happens for all dialogs in the app, not just this one.
Anyone have any ideas what is going on?

Related

how to add AlertDialog in OnPause() method?

I want to show an AlertDialog when a user click on Back, Home and Recent button in Android Navigation Bar for back button.
I used onBackPressed() method and it's working fine but for Home and Recent, I am using onPause() method and it's not working.
Kindly help me to resolve it.
#Override
public void onPause() {
builder = new AlertDialog.Builder(this);
builder.setTitle("Alert")
.setMessage("Do you want to close application?")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.show();
super.onPause();
}
For security reasons, Android wont let you override the default home button that easily, take a look at this discussion

How process result from Alert Dialog? (Android)

I'm working on a small class which can generate Alert dialog boxes. The constructor of the class looks like this:
void popupMessage(String title, String message, String pText, String nText, boolean cancelable) {
setPopupResult(999);
AlertDialog.Builder dialog = new AlertDialog.Builder(currentActivity);
dialog.setMessage(message).setCancelable(cancelable);
dialog.setNegativeButton(nText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setPopupResult(0);
}
});
dialog.setPositiveButton(pText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setPopupResult(1);
}
});
AlertDialog alert = dialog.create();
alert.setTitle(title);
alert.show();
}
as you see based on pressing the yes or no button the code sets the value of a private variable to 0 or 1 which can be accessed by a getter method. (the value is set to 999 at the top, this indicates that the user did no press anything yet)
The problem I'm facing is that from in the calling activity I somehow should be able to capture when the popupResult variable changes from 999 to either 0 or 1. How can I do that?
(I could be wrong handling the Alert dialog like this, feel free to educate me)
Since the user's clicking on your dialog buttons is asynchronous to when you're showing the dialog, one way to do it, would be to provide some kind of callback to your method, that is called when the buttons are clicked.
Example:
/* define this inside your dialog class */
public interface Callback {
void onOkClicked();
void onCancelClicked();
}
void popupMessage(String title, String message, String pText, String nText, boolean cancelable, Callback callback) {
...
/* positive button clicklistener, for negative button, use callback.onCancelClicked() */
public void onClick(DialogInterface dialog, int which) {
callback.onOKClicked();
}
...
}
/* Using the method */
popupMessage(..., new Callback() {
void onOKClicked() {
/* do something when OK was clicked */
}
void onCancelClicked() {
/* do something when Cancel was clicked */
}
});
I see that you already have the context of the activity in the variable currentActivity. Create the method setPopupResult() in your activity like this:
public void setPopupResult(int x) {
// your code goes here
}
and in popupMessage(), if the class of your activity is MainActivity:
dialog.setNegativeButton(nText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity) currentActivity).setPopupResult(0);
}
});
dialog.setPositiveButton(pText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity) currentActivity).setPopupResult(1);
}
});

How to show user input details back on alert dialog?

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();
}
});

How to remove view from AlertDialog Android

I am making an ALert Dialog with custom EditText Field.
I made a View variable and then associated it with my custom EditText field.
requestView = inflater.inflate(R.layout.send_request,null);
Then I added that view to my AlertDialog
alert.setView(requestView);
And after that I added the onClick Method To My Button to perform the alert Dialog action..
chatRequestbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
request = requestMsg.getText().toString();
send();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
}
});
It worked correctly. But after pressing cancel option on alert dialog when I press the button again to perform the alert dialog option.
It crashes with the following error.
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
at android.view.ViewGroup.addView(ViewGroup.java:4258)
at android.view.ViewGroup.addView(ViewGroup.java:4230)
at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:647)
at android.support.v7.app.AlertController.setupView(AlertController.java:463)
at android.support.v7.app.AlertController.installContent(AlertController.java:226)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
at android.app.Dialog.dispatchOnCreate(Dialog.java:395)
at android.app.Dialog.show(Dialog.java:294)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
at com.buckydroid.anonchat.User$3.onClick(User.java:86)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I though making the view null and adding the view again while clicking on the button will fix the issue. But same problem again and again..
If you want to use an existing View, use this.
alert.setOnDismissListener(new OnDismissListener(){
((ViewGroup)requestView.getParent()).removeView(requestView);
});
I converted the above code from Kotlin to Java by hand, plz check before use.
Your problem is here:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(requestView);
In this case alertis not the dialog, but a builder. So every time when you're trying to show it - it rebuilds this dialog and trying to add same view for it - requestView. Because it is cached in the builder. To fix it - move
requestView = inflater.inflate(R.layout.send_request,null);
alert.setView(requestView);
to your OnClick method where you're showing the dialog. So it should look like this:
chatRequestbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
requestView = inflater.inflate(R.layout.send_request,null);
alert.setView(requestView);
alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
request = requestMsg.getText().toString();
send();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
}
});
You want to set an on dismiss listener. I did something like:
The dialog should set an on Dismiss listener
alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
((ViewGroup)requestView.getParent()).removeView(requestView);
}
});

Button in AlertDialog not appearing

I have a NeutralButton that should cause an AlertDialog box I have to disappear. This issue is, when the AlertDialog box comes up, the text set appears just fine but the NeutralButton doesn't show at all. I tried it as a Positive and Negative button, but no difference was made. Here's the code:
Button infoG1;
infoG1 = (Button) findViewById(R.id.iG1);
infoG1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder info = new AlertDialog.Builder(game1.this);
info.setMessage("Text here");
info.show();
info.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
});
I would have expected a button with text "Ok" to appear at the bottom of the AlertDialogue box, which causes the box to disappear once the user presses it.
You shouldn't call setNeutralButton(...) after your show() method. You are doing this, which is causing the problem.
Instead of this:
dialog.show();
dialog.setNeutralButton(...);
Do this:
dialog.setNeutralButton(...);
dialog.show();
Note that you should put everything above your dialog.show(). Otherwise you will have problems.
try this
Button infoG1;
infoG1 = (Button) findViewById(R.id.iG1);
infoG1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder info = new AlertDialog.Builder(game1.this);
info.setMessage("Text here");
info.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
info.show();
}
});

Categories

Resources