El_o.di
El_o.di

Reputation: 829

Android AlertDialog button positioning

I have a requirement to create an AlertDialog, which is split vertically into two zones:

  1. One of these zones (on the right side) should have dialog buttons (OK, Cancel and Set) at the bottom and another layout on top of these buttons.
  2. Left side of this dialog should have another layout, say, image.

The question is, is it possible to have anything like that or all alertDialogs have to have all their buttons at the bottom? I don't even know where to start. I've tried looking at AlertDialog class, but could see anything suitable.

Upvotes: 2

Views: 15576

Answers (4)

Simple
Simple

Reputation: 865

I'm afraid that I'm too late but a very simple solution would be:

Assuming you know either ways to set a button to an AlertDialog, I'm gonna omit this part:

…
yourDialog.show();

Button btn = yourDialog.getButton(DialogInterface.BUTTON_NEUTRAL);//Whatever button it is.

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) btn.getLayoutParams();
params.topMargin = yourValue;//Enter your desired margin value here.
params.bottomMargin = yourValue;
params.leftMargin = yourValue;
params.rightMargin = yourValue;

btn.setLayoutParams(params);

Upvotes: 1

abissa
abissa

Reputation: 173

enter image description here

 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity,
            R.style.MyAlertDialogStyle);
    builder.setTitle(mActivity.getResources().getString(R.string.app_name));
    builder.setCancelable(false);
    builder.setMessage(str_message);
    builder.setPositiveButton(str_btn1,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (listener != null) {
                        listener.onClick(null);
                    }
                }
            });
    if (str_btn2 != null) {
        builder.setNegativeButton(str_btn2,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface,
                                        int i) {
                        if (listener2 != null) {
                            listener2.onClick(null);
                        }
                    }
                });
    }
    builder.show();

Edit : Please see the image above. You will get the output like this. Simply create a dialog style

Dialog Style in styles.xml

    <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <!-- Used for the buttons -->
    <item name="android:textStyle">bold</item>
    <item name="colorAccent">@android:color/holo_blue_bright</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">@android:color/black</item>
    <!-- Used for the background -->
    <item name="android:background">@android:color/white</item>
</style>

Upvotes: 3

lelloman
lelloman

Reputation: 14173

it looks like you don't need an AlertDialog for this, but yes you can do it with AlertDialog. create your view and then use AlertDialog.Builder without calling setPositiveButton(), setNegativeButton() etc..

AlertDialog alertDialog = new AlertDialog.Builder(context)
        .setView(yourContentView)
        .create();

Upvotes: 2

Nir Duan
Nir Duan

Reputation: 6392

You can create your own custom dialog layout - with this 3 steps:

  1. Create a custom dialog layout (XML file).
  2. Attach the layout to Dialog.
  3. Display the Dialog.

Look here.

Upvotes: 0

Related Questions