Showing posts with label AlertDialog.Builder. Show all posts
Showing posts with label AlertDialog.Builder. Show all posts

How to Add an WebView to an AlertDialog

public AlertDialog.Builder setView (View view) was added in API level 1
Set a custom view to be the contents of the Dialog.
Parameters: view The view to use as the contents of the Dialog.
Returns: This Builder object to allow for chaining of calls to set methods

You cannot type anything on the WebSite though as the soft keyboard does not display on an textbox on a webage. Also, navigating to other pages or clicking on items on the page may cause errors. It seems good for displaying text of a web page such as a terms of agreement page or FAQ.


1. Create an AlertDialog with Supplied Arguments named myAlertDialogBuilder.

2. In the showAlertDialog method, Instantiate a new WebView named myWebView, after the AlertDialog is instantiated.

3. Add Internet Permission to your Android Project

4. Add the below line to load the URL(website) to load in the WebView.

    myWebView.loadUrl("http://androidsbs.blogspot.com/");

5. Add the below import to the imports section.

import android.webkit.WebViewClient;

6. Add the below code to so it won't ask the user to open another browser.

    myWebView.setWebViewClient(new WebViewClient() { 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

7. Add the below line to link the WebView to the AlertDialog.

myAlertDialogBuilder.setView(myWebView);

8. Compile and run!

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(android.view.View)
http://www.codeofaninja.com/2011/07/android-alertdialog-example.html

How to Add a Widget/Custom View to an AlertDialog

public AlertDialog.Builder setView (View view) was added in API level 1
Set a custom view to be the contents of the Dialog.
Parameters: view - The view to use as the contents of the Dialog.
Returns: This Builder object to allow for chaining of calls to set methods


1. Create an AlertDialog with Supplied Arguments named myAlertDialogBuilder.

2. In the showAlertDialog method, instantiate the object(s) you would like to add to the AlertDialog. In this example I will Instantiate a new EditText named myEditText, after the AlertDialog is instantiated. Other example items you might instantiate are an ImageView, CheckBox,or a LinearLayout to include many widgets.

3. Add the below line to add the EditText to the AlertDialog. This example below will add the EditText myEditText to the Alertdialog named myAlertDialogBuilder.

myAlertDialogBuilder.setView(myEditText);

4. Compile and run!
Soft keyboard pop-ups when user presses EditText

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(android.view.View)
http://www.codeofaninja.com/2011/07/android-alertdialog-example.html

setMultiChoiceItems - How to Add a Multiple Choice List of Items to an AlertDialog.Builder

public AlertDialog.Builder setMultiChoiceItems (CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener) was added in API level 1
Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for each checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.
Parameters:
items - the text of the items to be displayed in the list.
checkedItems - specifies which items are checked. It should be null in which case no items are checked. If non null it must be exactly the same length as the array of items.
listener - notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns: This Builder object to allow for chaining of calls to set methods


1. Create an AlertDialog with Supplied Arguments

2. Initialize a CharSequence Array

3. Add the line below to the imports section.

import android.content.DialogInterface;

4. After your AlertDialog.Builder is instantiated in step 1, add the following code to set a list of items with a check box to choose one or many of the options. This will display the myCharSequence array in the list. Once you select an item, the dialog box does NOT automatically close. You may wish to Add a Positive Button to close the AlertDialog or maybe add a dismiss(). The null signifies that none of the items will be selected by default.

myAlertDialogBuilder.setMultiChoiceItems(myCharSequence, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//To test, Add a toast, with a context of getBaseContext() and a message such as
// "You selected "+ myCharSequence[which] + " Checked?: " + isChecked
}
});

5. Add an AlertDialog.Builder Positive Button Listener. This will enable the user to close the AlertDialog box after the item selections are made. 

6. Compile and run!

User selects the first option

Next Recommended Article:  Declare an AlertDialog object

Resources: http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setMultiChoiceItems(java.lang.CharSequence[], boolean[], android.content.DialogInterface.OnMultiChoiceClickListener)

How to Add a List of Items with Radio Buttons to AlertDialog.Builder

public AlertDialog.Builder setSingleChoiceItems (CharSequence[] items, int checkedItem,DialogInterface.OnClickListener listener) was added in API level 1
Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for the checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.
Parameters:
items - the items to be displayed.
checked -Item specifies which item is checked. If -1 no items are checked.
listener - notified when an item on the list is selected. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.
Returns: This Builder object to allow for chaining of calls to set methods



1. Create an AlertDialog with Supplied Arguments

2. Initialize a CharSequence Array

3. Add the line below to the imports section.

import android.content.DialogInterface;

4. After your AlertDialog.Builder is instantiated in step 1, add the following code to set a list of items with a radio button to an AlertDialog. This will display the myCharSequence array in the list. Once you select an item, the dialog box does NOT automatically close. You may wish to Add a Positive Button to close the AlertDialog or add dismiss, as in the example below. The 0 signifies that the first item in the list will be selected by default. To have non selected, change the 0 to -1.

myAlertDialogBuilder.setSingleChoiceItems(myCharSequence, 0, new DialogInterface.OnClickListener() {
    @Override
        public void onClick(DialogInterface dialog, int which) {
            // To test, Add a Toast with a context of getBaseContext() and a message such as
            // "You selected "+ myCharSequence[which]  //<- this will display the item selected
            //once selected, the dialog box does NOT automatically close.
           //To close the AlertDialog, you could Add a dismiss
}
});

5. Compile and run!

User selects the first option

Next Recommended Article: setMultiChoiceItems - How to Add a Multiple Choice List of Items to an AlertDialog.Builder

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setSingleChoiceItems(java.lang.CharSequence[], int, android.content.DialogInterface.OnClickListener)
http://stackoverflow.com/questions/6421196/how-to-dismiss-alertdialog-with-radio-buttons-in-android-sdk

How to Set a List of Items to an AlertDialog.Builder

public AlertDialog.Builder setItems (CharSequence[] items, DialogInterface.OnClickListenerlistener) was added in API level 1
Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.
Returns: This Builder object to allow for chaining of calls to set methods



1. Create an AlertDialog with Supplied Arguments

2. Initialize a CharSequence Array named myCharSequence. 

3. Add the line below to the imports section.

import android.content.DialogInterface;

4. After your AlertDialog.Builder is instantiated in step 1, add the following code to set a list of items to an AlertDialog. 

myAlertDialogBuilder.setItems(myCharSequence, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// To test, Add a Toast, with a context of getBaseContext() and a message such as        // "You selected " + myCharSequence[which]" //<- This will display the item in the list
       // "You selected " + which //<- this will just display the number order of the item selected in the list

}
});

5. Compile and run!

Next Recommended Article:  How to Add a List of Items with Radio Buttons to AlertDialog.Builder

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setItems(java.lang.CharSequence[], android.content.DialogInterface.OnClickListener)
http://androiddesk.wordpress.com/tag/alert-dialog-with-list-of-items/

How to Add an Icon/Drawable to an AlertDialog.Builder

public AlertDialog.Builder setIcon (Drawable icon) was Added in API level 1
Set the Drawable(icon, picture, image) to be used in the title.
Returns: This Builder object to allow for chaining of calls to set methods


1. Create an AlertDialog with Supplied Arguments

2. Set the Title in an AlertDialog.Builder. This must be done, if not, the Icon will not display in the AlertDialog. It will not give an error. It will just not display the image.

3. Add a Drawable to your project.

4. After your AlertDialog.Builder is instantiated in step 1, add the following line to add an icon/drawalbe of the AlertDialog. The line below will add a icon named myicon to the Title of the AlertDialog.

myAlertDialogBuilder.setIcon(R.drawable.myicon);

5. Compile and run!

6. Other icons:
Dialog_Time In API Level 10, I had to copy the ic_dialog_time.png file from the API Level 11 default icons to my drawable directory, and used "R.drawable.ic_dialog_time" to display the icon. Starting in API Level 11, you should be able to call is directly using: android.R.drawable.ic_dialog_time

Next Recommended Article: How to Set a List of Items to an AlertDialog.Builder

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setIcon(android.graphics.drawable.Drawable)
http://stackoverflow.com/questions/3781363/why-doesnt-this-alertdialog-builder-seticon-show-the-ic-menu-info-icon

How to Set an AlertDialog.Builder Neutral Button Listener

public AlertDialog.Builder setNeutralButton (CharSequence text,DialogInterface.OnClickListener listener) was added in API level 1
Set a listener to be invoked when the neutral button of the dialog is pressed.
Parameters:
text - The text to display in the neutral button
listener - The DialogInterface.OnClickListener to use.
Returns: This Builder object to allow for chaining of calls to set methods


1. Create an AlertDialog with Supplied Arguments

2. Add the line below to the imports section.

import android.content.DialogInterface;

3. After your AlertDialog.Builder is instantiated in step 1, add the following code to set up the neutral button. This will set the button text to Neutral and display an optional toast when the Neutral button is pressed. The AlertDialog will close automatically when the Neutral button is pressed.

myAlertDialogBuilder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
    @Override

public void onClick(DialogInterface dialog, int which) {
// To test, Add a Toast, with a context of getBaseContext() and message such as
        // "No opinion huh...."

     }
});

4. Compile and run!

5. The button order is defined by the OS itself. An OS change in button display order was made from API Level 10 to API Level 11.
In API Level 11+ the buttons will display in the order NEGATIVE|NEUTRAL|POSITIVE
In API Level 10 and below, the buttons will display int he order: POSITIVE|NEUTRAL|POSITIVE

API Level 10 Button order


Next Recommended Article: How to Add an Icon/Drawable to an AlertDialog.Builder

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setNeutralButton(java.lang.CharSequence, android.content.DialogInterface.OnClickListener)http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html
https://code.google.com/p/android/issues/detail?id=24138
http://stackoverflow.com/questions/20117732/dialog-box-positive-negative-buttons-in-different-position-depending-on-androi



How to Set an AlertDialog.Builder Negative Button Listener

public AlertDialog.Builder setNegativeButton (CharSequence text,DialogInterface.OnClickListener listener) was added in API level 1
Set a listener to be invoked when the negative button of the dialog is pressed.
Parameters:
text - The text to display in the negative button
listener - The DialogInterface.OnClickListener to use.
Returns: This - Builder object to allow for chaining of calls to set methods


1. Create an AlertDialog with Supplied Arguments

2. Add the line below to the imports section.

import android.content.DialogInterface;

3. After your AlertDialog.Builder is instantiated in step 1, add the following code to set up the negative button. This will set the button text to No and display an optional toast when the No button is pressed. The AlertDialog will close automatically when the Negative button is pressed.

myAlertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override

public void onClick(DialogInterface dialog, int which) {
// To test, Add a Toast, with a context of getBaseContext() and message such as
        // "No? Any way I can get you to change your mind?"

     }
});

4. Compile and run!

4. The button order is defined by the OS itself. An OS change in button display order was made from API Level 10 to API Level 11.
In API Level 11+ the buttons will display in the order NEGATIVE|NEUTRAL|POSITIVE
In API Level 10 and below, the buttons will display int he order: POSITIVE|NEUTRAL|POSITIVE

API Level 10 button order


Next Recommended Article: How to Set an AlertDialog.Builder Neutral Button Listener

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setNegativeButton(java.lang.CharSequence, android.content.DialogInterface.OnClickListener)
http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html
https://code.google.com/p/android/issues/detail?id=24138
http://stackoverflow.com/questions/20117732/dialog-box-positive-negative-buttons-in-different-position-depending-on-androi



How to Set an AlertDialog.Builder Positive Button Listener

public AlertDialog.Builder setPositiveButton (CharSequence text,DialogInterface.OnClickListener listener) was added in API level 1
Set a listener to be invoked when the positive button of the dialog is pressed.
Parameters:
text - The text to display in the positive button
listener - The DialogInterface.OnClickListener to use.
Returns: This Builder object to allow for chaining of calls to set methods


1. Create an AlertDialog with Supplied Arguments

2. Add the line below to the imports section.

import android.content.DialogInterface;

3. After your AlertDialog.Builder is instantiated in step 1, add the following code to set up the positive button. This will set the button text to Yes and display an optional toast when the Yes button is pressed. The AlertDialog will close automatically when the Positive button is clicked.

myAlertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override

public void onClick(DialogInterface dialog, int which) {
// To test, Add a Toast, with a context of getBaseContext() and message such as
        // "I can't believe you pressed me! You better sleep with one eye open tonight. Just sayin'."

     }
});

4. Compile and run!

5. The button order is defined by the OS itself. An OS change in button display order was made from API Level 10 to API Level 11.
In API Level 11+ the buttons will display in the order NEGATIVE|NEUTRAL|POSITIVE
In API Level 10 and below, the buttons will display int he order: POSITIVE|NEUTRAL|POSITIVE

API Level 10 button order

Next Recommended Article: How to Set an AlertDialog.Builder Negative Button Listener

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setPositiveButton(java.lang.CharSequence, android.content.DialogInterface.OnClickListener)
http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html
https://code.google.com/p/android/issues/detail?id=24138
http://stackoverflow.com/questions/20117732/dialog-box-positive-negative-buttons-in-different-position-depending-on-androi



How to Set the AlertDialog.Builder Non-Cancelable

public AlertDialog.Builder setCancelable (boolean cancelable) was added in API level 1
Sets whether the dialog is cancelable or not. The default of the AlertDialog.Builder is true if this item is not included in your AlertDialog.Builder upon creation.
Returns: This Builder object to allow for chaining of calls to set methods

1. Create an AlertDialog with Supplied Arguments

2. After your AlertDialog.Builder is instantiated in step 1, add the following line to set the AlertDialog so it is not cancelable. The user will not be able to press the back button to cancel the AlertDialog. The user must select an option on the AlertDialog, or close the app altogether.

myAlertDialogBuilder.setCancelable(false);

3. Compile and run!

Next Recommended Article: How to Set an AlertDialog.Builder Positive Button Listener

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCancelable(boolean)

How to Set the Title in an AlertDialog.Builder

public AlertDialog.Builder setTitle (CharSequence title) was added in API level 1
Set the title displayed in an AlertDialog.
Returns: This Builder object to allow for chaining of calls to set methods


1. Create an AlertDialog with Supplied Arguments

2. After your AlertDialog.Builder is instantiated in step 1, add the following line to set the title of the AlertDialog.

myAlertDialogBuilder.setTitle("The Title. Yep, that's me.");

3. Compile and run!

Next Recommended Article: How to Set the AlertDialog.Builder Non-Cancelable

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setTitle(java.lang.CharSequence)

How to Set a the Message in an AlertDialog.Builder

public AlertDialog.Builder setMessage (CharSequence message) was added in API level 1
Set the message to display in an AlertDialog.
Returns: This Builder object to allow for chaining of calls to set methods



2. After your AlertDialog.Builder is instantiated in step 1, add the following line to set the message.  

myAlertDialogBuilder.setMessage("This is my message to notify you.");

3. Compile and run!

Next Recommended Article: How to Set the Title in an AlertDialog.Builder

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setMessage(java.lang.CharSequence)


How to Create an AlertDialog with Supplied Arguments

public AlertDialog create () was added in API level 1
Creates an AlertDialog with the arguments supplied to this builder. It does not display the dialog. This allows the user to do any extra processing before displaying the dialog.

1. Instantiate a new AlertDialog.Builder

2. You may want to add a title, add a message, set it non-cancelable, add a positive button, add a negative button, or add a neutral button. However, none of these options are required. If no items are added, the dialog will still display and look like the whole screen dims and the user will not be able to press any items on the screen. Pressing the back button will remove the AlertDialog with no items set.

3. Declare an AlertDialog object named myDialog.

4. Add the below code to supply the arguments the builder and save it to the myDialog variable.

myDialog = myAlertDialogBuilder.create();

5. Make sure to return the myDialog.

return myDialog;

How to Instantiate a new AlertDialog.Builder

AlertDialog.Builder was added in API Level 1

1. Declare an AlertDialog.Builder object.

2. After you declare the AlertDialog.Builder object, add this line below to instantiate it.

myAlertDialogBuilder = new AlertDialog.Builder(this);

3. Compile and run!

Next Recommended Article: How to Set a the Message in an AlertDialog.Builder

Resources:
http://developer.android.com/reference/android/app/AlertDialog.Builder.html