AlertDialog or Custom Dialog - android

I'm developing an Android 2.2 application.
I want to show a text with an Ok button but I want to add my custom style.
What is the better choice an AlertDialog with a custom layout or a Dialog?

I'd go for AlertDialog:
It's easier to implement.
The only custom thing you have to do is an XML layout and then inflate it.
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(getLayoutInflater().inflate(R.layout.custom_dialog, null))
.create();
In order to listen for UI events:
View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
Button btn = (Button)view.findViewById(R.id.the_id_of_the_button);
btn.setOnClickListener(blah blah);
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(view)
.create();
You can check in android dialog docs:
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
when would i use an Alert Dialog?
-When i just want to inform something to the user .
-When we want use for a prompt like Do you want to go back (Yes, No, Cancel. Alert dialog comes with 3 buttons which are positive, negative and neutral which are provided by default).
-When i want to prompt user for a simple value (number/date/string...)
when would i use a Dialog?
-When i want to carry on a complex process with more buttons and widgets .
-Example:

Related

Android standalone Alert Dialog

The normal way to create an alert dialog is to attach it to the current activity, like in How do I display an alert dialog on Android?
However, what I want to achieve is to create an alert dialog that is not attached to a particular activity. So that when the activity is destroyed (e.g. using finishAndRemoveTask), my dialog is still here.
Is there a way to achieve that? Also, I would like the area outside of the alert dialog to be unclickable, i.e. the user must click on the ok button of the dialog in order to navigate to other activities.

Android default alertdialog background color that is not created by me

I wanna change only default alert dialog backgound but i didn'tfind anything usefull. I don't wanna change custom alert dialog them. I did it . The only thing i want to change default alert dialog background color. How can i change alert dialog backgroudn that is not created by me ?
In short on run time permissions dialog, you can't customize it.
When your app calls requestPermissions(), the system shows a standard dialog box to the user. Your app cannot configure or alter that dialog box. If you need to provide any information or explanation to the user, you should do that before you call requestPermissions(), as described in Explain why the app needs permissions.’
If you want to change the background of alertdialog, use belwo code:
Add this line and you will able to change the background color of alert dialog.
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Say Hello!");
builder.setMessage("Are you want to do this?");
AlertDialog dialog = builder.create();
dialog.show();
dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.Blue));
You want to change runtime permission request dialog colors I suppose. You can't customize those alert dialogs. They are default.
Take a look into this answer:
How can I customize permission dialog in Android?

When does touching the screen outside an AlertDialog cancel the AlertDialog?

Suppose you have an AlertDialog with two buttons A and B. I have found that on some devices and some versions of android, if you touch any area of the screen around the dialog, the AlertDialog disappears. On other devices you are forced to select either A or B, so there is no way I can allow the user to cancel the action without adding a third ('Cancel') option to the AlertDialog. Is there any way to determine programmatically whether the third option is required?
You can control it with dialog.setCanceledOnTouchOutside(true);
For more information :
How to dismiss the dialog with click on outside of the dialog?
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);

Android: How to set Text size of Button on a Dialog programatically?

I am writing an Android app where I need to put bigger text on the Buttons of Dialog windows programatically.
I saw that there are two options to set (positive, negative or neutral) buttons on an AlertDialog.Builder:
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
setPositiveButton(int textId, DialogInterface.OnClickListener listener) and
In option 1, I can only set the text and in option 2, I can use resource id of the text value.
But none of the options allow me to add a styled button.
Is there some other way?
There is no need for you to create a custom view.
The dialog builder create method returns a AlertDialog object that gives you access to the buttons.
AlertDialog.Builder alert = new AlertDialog.Builder(YOUR_CONTEXT);
AlertDialog dialog = alert.create();
// Positive
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextSize(THE_SIZE);
// Negative
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextSize(THE_SIZE);
OF course, you can always go for your custom View to show to the user when the dialog is created, by using:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(yourCustomView);
However, take on count that creating your own custom View, means you have to inflate your own layout, create your buttons inside of it and handle the proper click actions, just like you do in a regular activity, this is the way to go if you want to modify any of the "default" views in your dialog...
Hope this helps...
Regards!

Keep AlertDialog's DialogInterface opened

I have a custom AlertDialog where a user has to set password. There are two edittext views.
I compare them first if they match and if they are more than 3 characters long. If they don't match, I display a toast to alert the user. But after submitting and checking the dialog closes. How can I keep it opened until user inserts the correct values?
I was looking at doing something similar and I could not find a way do this with the standard AlertDialog as it is.
One possible way I found was to not specify any button listeners in your AlertDialog and instead place a view with your own custom buttons that perform the checks and then dismiss the dialog if necessary. I've not yet tried this to see how it works.
Another option is to create your own Custom dialog by subclassing Dialog.
You have to set a global variable like
boolean showAlert = true;
And attach a onClick listener to the AlertDialog and after cheking to see if there is need to show the alert again. If there is a need, you should show it again. You can`t keep it open if the user clicks a button from the AlertDialog. You have to recreate it again.

Categories

Resources