Dave Fite
Dave Fite

Reputation: 13

AlertDialog action button placement

I've created an AlertDialog using the AlertDialog.Builder and would like to adjust the placement of the action buttons (Cancel and OK) to improve its appearance.

I've searched and can't seem to find any easy way. Do I have to abandon the AlertDialog.Builder and add the action button functions to my view and setup all the listeners? If so, how do I determine the text size/color for the action buttons to stay consistent with other AlertDialog?

Screenshot of the popup

popup_tee_selection_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:id="@+id/popup_tee_selection_TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/enter_new_tee"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/popup_tee_selection_EditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

<Switch
    android:id="@+id/popup_tee_selection_Switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|start"
    android:showText="true"
    android:textOff="Men"
    android:textOn="Women"
    android:thumbTextPadding="10dp"
    android:thumb="@drawable/custom_switch_inner_holo_light"
    android:track="@drawable/custom_switch_track_holo_light"/>

</LinearLayout>

code fragment

private void popupTeeSelectionEditText() {
    View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_tee_selection_edittext, null);
    final EditText userInput = (EditText) popupView.findViewById(R.id.popup_tee_selection_EditText);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder
            .setView(popupView)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    String selectedItem = userInput.getText().toString();
                    if (DEBUG) Log.w(TAG, "Returned value: '" + selectedItem + "'");
                    if (listViewAlertDialog != null)
                        listViewAlertDialog.cancel();
                    processTeeSelectionCourse(selectedItem);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            })
            .show();
}

Upvotes: 0

Views: 625

Answers (3)

Mohit G
Mohit G

Reputation: 305

Have you searched through other similar questions on stack overflow?

Here are a few with solutions that maybe able to help you out. Links are below.

For the second link, scroll all the way down to the bottom where they briefly discuss about creating your own custom dialog boxes. With custom dialog boxes you should be able to get the format you want.

The getButton() method mentioned by Sound Conception may also be a viable method.

Upvotes: 0

Anudeep Bulla
Anudeep Bulla

Reputation: 8548

AFAIK, the view that one supplies to the dialog's builder is only for the central section of the dialog, without the Title section at the top and the button section at the bottom. To get a look that you are aiming for, you'll need to add your custom buttons to this view, setup the appropriate listeners, and then hide the original buttons.

To setup the color to match, check what the app's primary and accent colors are. If none are specified, you could set them to android defaults, or probably specify them in the app theme settings so that all dialogs in the app will carry the same look.

Have a look at this answer to checkout how to setup custom colors for the dialog buttons.

Upvotes: 1

Sound Conception
Sound Conception

Reputation: 5411

The AlertDialog method getButton can be used to get an action button after the dialog has been created. You can then change the textSize, textColor etc as you want. I don't think you'll be able to change it's position though.

To position the buttons precisely relative to your other content views you'll need to add them yourself instead, in your popupView.

Upvotes: 1

Related Questions