how to add AlertDialog in OnPause() method? - java

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

Related

Android Studio alertDialog in onItemClick

I have a little problem with a simple app in Android Studio, the app is simple, you click a color and the background changes to that color, but I wanted to add an alertDialog so it asks before it changes to that color. When I click a color, the dialog appears, but the operation that changes the color doesn't, I want it to stop so if I choose yes , it will continue, if I choose No, it will not change the color.
I don't know how to cancel the operation after No is clicked.
Here is the code in MainActivity:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to choose this color?")
.setTitle("Change color")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
String msg=((TextView) view).getText().toString();
ShowMessage(msg);
int[]colorsCodes = new int[]{
getResources().getColor(R.color.White),
getResources().getColor(R.color.Black),
getResources().getColor(R.color.Red),
getResources().getColor(R.color.Blue),
getResources().getColor(R.color.Green),
getResources().getColor(R.color.Yellow),
getResources().getColor(R.color.Orange),
getResources().getColor(R.color.Pink),
getResources().getColor(R.color.Violet),
getResources().getColor(R.color.Brown)};
myListView.setBackgroundColor(colorsCodes[i]);
Edit:
I added myListView.setBackgroundColor(colorsCodes[i]); function in the "Yes" option of the alert Dialog, and also declared ColorCodes outside the onItemClick(), everything is working fine except when I click Yes, the app crashes...
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to choose this color?")
.setTitle("Change color")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
myListView.setBackgroundColor(colorsCodes[i]);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
edit:
thank you, I solved it now, the problem was that the function
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
was taking int i, along with the "yes" option, so I just renamed it to j
public void onClick(DialogInterface dialogInterface, int j) {
myListView.setBackgroundColor(colorsCodes[i]);
}
You have to place the color changing function
myListView.setBackgroundColor(colorsCodes[i]);
inside the onClick function of setPositiveButton function. You can leave it blank inside the setNegativeButton function.
P.S.- don't forget to define the colorsCodes before the onItemClick function.

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

AlertDialog not showing in Fragment

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?

redirect after ok is pressed in android

I have the following code:
if(isSolved()){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("You solved the puzzle! Congratulations!")
.setCancelable(false)
.setPositiveButton("Thanks.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(context, MainMenuActivity.class);
startActivity (intent);
}
});
}
It's a puzzle and when the puzzle it resolved it shows the above message:"Congrats! you finished the game!". But when I press ok it doesn't do anything. I would want after pressing ok to redirect to another page. i'm using java with eclipse.
You need to add OnClickListener to the PositiveButton and handle your redirect in onclick:
builder.setPositiveButton("Thanks.", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Do your redirect here
}
});
builder.setMessage("Congrats! you finished the game!")
.setCancelable(false)
.setPositiveButton("Thanks.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do something
}
});
AlertDialog.Builder.setPositiveButton()
You can use alert.setButton("Okay", listener);
And in your listener catch the click event.

Categories

Resources