I have a Dialog in my android app and I want to set positive/negative buttons in the dialog. It is a Dialog, not an AlertDialog. I know that I could actually use an AlertDialog, but I would like to know, whether the same is possible within a dialog. If there is a possibility, I appreciate all of you for an example.
you can add anything you want in dialog. For an example
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
dialog_btn.setOnClickListener(new View.OnClickListener()
{
// TODO:
}
Here custom_dialog is a custom layout for dialog which has a button dialog_button. similarly you can add any widget and handle that.
Related
I'm setting a message in a simple alert dialog. I need to know if the user has read all the text inside the dialog or not. Is there any way to set a scroll listener or something in an alert dialog?
Create your custom dialog and then set some listener to your custom View. Here's a good example: http://www.mkyong.com/android/android-custom-dialog-example/
and
https://stackoverflow.com/a/10713501
I'm trying to create a popup window similar to programmatically. Unfortunately I couldn't figure out any helpful resource so far for creating a similar popup.
You can do it with Dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.chat_custom_dialog);
dialog.setTitle("custom dialog");
dialog.show();
where dialog.setContentView(R.layout.chat_custom_dialog); will set the layout of the dialog.
And if you want to update the dialog's layout views you can call the dialog.findViewById(id) to get the view inside of the layout same as what you are doing in the activity
i want to show dialog (not alert dialog) but i don't want dialog show modal.
How to set dialog to show modal or not?
this is my code
Dialog dialog = new Dialog(CategoryList.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(actionview);
dialog.show()
you can use PopupWindow instead, and setOutsideTouchable(true).
or try this
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!
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: