i've this dialog
case DIALOGO_EDIT:
final EditText editText = new EditText(context);
builder.setView(editText);
builder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
}
});
builder.setNegativeButton("Close", null);
break;
But when i rotate the device the dialog dismiss... How can i solve this problem and mantein the dialog during rotation? Or display the edit text at "full screen" like WhatsApp
When the screen is rotated, the activity actually is restarted and killed, so you need to save and be able to restore the data using the lifecycle methods. Have a look here: Saving Persistent State
You may want to have a look to this question here
The best way of avoid this problem is to use DialogFragment.
Create a new class extended to DialogFragment. Override onCreateDialog and return your old Dialog or an AlertDialog.
Them you can show it with DialogFragment.show(fragmentManager, tag).
Here an example with the activity like listener:
public class MyDialogFragment extends DialogFragment {
public interface YesNoListener {
void onYes();
void onNo();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof YesNoListener)) {
throw new ClassCastException(activity.toString() + " must implement YesNoListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialog_my_title)
.setMessage(R.string.dialog_my_message)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onYes();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onNo();
}
})
.create();
}
}
And in the Activity you call:
new MyDialogFragment().show(getSupportFragmentManager(), "tag"); // or getFragmentManager() in API 11+
This kind of questions already asked,and there are also solution for it, these three questions are matched with your problem (and they answered):
Android Best way of avoid Dialogs to dismiss after a device rotation
Android DialogFragment vs Dialog
How can I show a DialogFragment using compatibility package?
Related
I'm looking at how to remove the effect of the touch around a dialog
I created a dialog when there is no connection detected
Code
#Override
public void onErrorResponse(String _param1, String _param2) {
final String _tag = _param1;
final String _message = _param2;
nc.setTitle("No connection");
nc.setIcon(R.drawable.ic_launcher);
nc.setMessage("Turn on your connection and then relaunch the app");
nc.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface _dialog, int _which) {
i.setClass(getApplicationContext(), SplashActivity.class);
startActivity(i);
}
});
nc.setNegativeButton("Close App", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface _dialog, int _which) {
finish();
}
});
nc.create().show();
}
};
}
When the dialog appears it must make the 350x100
When I click on the screen around the dialog it removes
I would like to know if it is possible to remove the touch around the dialog when it is displayed ?
Try this: dialog.setCanceledOnTouchOutside(false);. It will still allow you to cancel the dialog with back button, but it will prevent the dialog from closing when touching around it.
EDIT
In your code you should change the nc.create().show(); at the end to:
AlertDialog alertDialog = nc.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
You probably need to add setCancelable(false) during creating
https://developer.android.com/reference/android/app/AlertDialog
I have dialog class, in that class I have one method that opens a dialog.
providerDialog.showProvidersDialog(customCategory.getCustCategoryId());
In that method I open another dialog (that is written in another class) to fill some information. Now dialog open and user filled details and I dismissed dialog from that method. Now I want to know in this current dialog class that another last opened dialog is closed so that I can get that details which I share using singleton and want to show in this class.
Current class method:
private void showCategoryInformationDialog() {
LayoutInflater li = LayoutInflater.from(context);
informationPromptsView = li.inflate(R.layout.row_category_information_layout, null);
android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(context, R.style.dialogBoxStyle);
// alertDialogBuilder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
alertDialogBuilder.setView(informationPromptsView);
inputBillProvider = informationPromptsView.findViewById(R.id.inputBillProvider);
inputConsumerNumber = informationPromptsView.findViewById(R.id.inputConsumerNumber);
name = informationPromptsView.findViewById(R.id.name);
inputBillProvider.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
alertDialogBuilder.setPositiveButton(context.getString(R.string.add), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialogBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialogBuilder.show();
}
//I want to know that this below dialog is closed providerDialog.showProvidersDialog(customCategory.getCustCategoryId());
Another last opened dialog code:
Link: Cancel/dismiss alertdialog builder from any other method in same class in android?
So i'm developing Android app where user is often asked what he wants to do on some actions (Button click etc.). For that i was using AlertDialog and wrote text on it and added Buttons i needed. It was working super until i realized, that on device rotation, an opened AlertDialog would disappear.
I found on the web that the proper way to handle rotation is to use Fragment, so i choose to make class extending DialogFragment:
public class MyDialogFragment extends DialogFragment {
public interface YesNoListener {
void onYes();
void onNo();
void onNeu();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String[] data = getArguments().getStringArray("data"); //vzame vrednosti, ki smo jih nastavili preden se pokliče .show();
if(data != null) {
switch (data.length) {
case 3:
return new AlertDialog.Builder(getActivity())
.setTitle(data[0])
.setMessage(data[1])
.setNeutralButton(data[2], new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onNeu();
}
})
.create();
case 4:
return new AlertDialog.Builder(getActivity())
.setTitle(data[0])
.setMessage(data[1])
.setPositiveButton(data[2], new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onYes();
}
})
.setNegativeButton(data[3], new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onNo();
}
})
.create();
case 5:
return new AlertDialog.Builder(getActivity())
.setTitle(data[0])
.setMessage(data[1])
.setPositiveButton(data[2], new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onYes();
}
})
.setNegativeButton(data[3], new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onNo();
}
})
.setNeutralButton(data[4], new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onNeu();
}
})
.create();
default:
return null;
}
}
else
return null;
}
}
I used interface too so i could implement Button.onClick() behaviour in the class where i would use MyDialogFragment. With getArguments() i passed all texts that would be used in Dialog.
The problem is that i can only use this class for one Dialog, since i have to Override interface functions, but i have multiple Dialogs with different behaviour.
I wanted to solve this issue with three public Objects Runnable, where i would just initialize Runnable where i need to change the behaviour of the Button.onClick()
...
Runnable runnablePositive, runnableNegative, runnableNeutral;
...
#Override
public void onYes(){
threadPositive.start();
}
#Override
public void onNo(){
threadNegative.start();
}
#Override
public void onNeu(){
threadNeutral.start();
}
static MyDialogFragment newInstance(String[] arg) {
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putStringArray("data", arg);
f.setArguments(args);
return f;
}
...and on usage:
threadPositive = new Thread()
{
public void run()
{
//do A
}
};
threadNegative = new Thread()
{
public void run()
{
//do B
}
};
threadNeutral = new Thread()
{
public void run()
{
//do C
}
};
newInstance(new String[]{title, besedilo, nevtralno}).show(getFragmentManager(), "tag");
It is working good untill i open Dialog and rotate (this is the main problem, other things somehow work) the device (reason why i use DialogFragment in the first place). All the variables are "deleted" on the rotation and i already passed all variables i needed for further work, but there comes new issue, which i have no idea how to solve: i can't pass Objects on rotation, whether i try with onRetainCustomNonConfigurationInstance() or onSaveInstanceState() all in vain...
So i have no idea how to solve this, i had gone through hundreds of question regarding similar issue, but had no luck... And i would be grateful for any helpful advice or answer regarding this problem (even if it is a different way to solve the problem).
Thanks in advance!
If you have multiple dialogs with different behavior, then you should simply create more instances of your dialog and assign them different tags. Instead of doing just
newDialogFragment(someArgs).show(getFragmentManager(), "tag")
you can do
newDialogFragment(someArgs).show(getFragmentManager(), "dialogWithArgs")
newDialogFragment(someOtherArgs).show(getFragmentManager(), "dialogWithOtherArgs")
and so on. Your interface should be changed to
public interface YesNoListener {
void onYes(String tag);
void onNo(String tag);
void onNeu(String tag);
}
When you call its methods from the dialog, pass the fragment tag so you know which dialog called the method. That way you can handle any number of dialogs easily.
As to saving the objects, those that don't change, go into the arguments, those that do, should be made Parcelable and saved to Bundle, if you can't, then you can create a separate fragment and call setRetainInstance(true) on it, then store the objects in it.
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();
}
}
I am validating an AlertDialog, and I would like to raise a Toast on top of the AlertDialog display.
I have this code, but the Toast is displayed on the activity
new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
.setView(addView).setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
if (wrapper.getTitle().length()>0)
{
processAdd(wrapper);
} else {
Toast.makeText(getApplicationContext(), "Name is required", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
}).show();
Instead of using AdvertDialog.Builder, you can create a custom dialog which will behave like a dialog, but is in fact a normal activity. Your toasts should be drawn normally on top of this.
Had this problem myself as well, when I wanted to show a validation message within a dialog.
The answer that seanhodges gave is probably the cleaner and better way. But a seperate activity wasnt practical for me, so i came up with this solution.
Anyway, you can use the AlerDialog.Builder, and show a toast.
If you override the OnClickListener of the button the you want to trigger the toast, you can show a toast on top of a dialog.
An example;
public void showToastOnDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing, you will be overriding this anyway
}
});
builder.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// You can implement code here, because you wont be
// overriding this
}
});
final AlertDialog dialog = builder.create();
// Make sure you show the dialog first before overriding the
// OnClickListener
dialog.show();
// Notice that I`m not using DialogInterface.OnClicklistener but the
// View.OnClickListener
dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(context,
"I`m a toast on top of a dialog.",
Toast.LENGTH_LONG);
toast.show();
// Because you are overriding the OnClicklistener, the
// dialog will not auto dismiss
// after clicking
dialog.dismiss();
}
});
}
Try this:
AlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);