2

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.

ddb
  • 2,367
  • 7
  • 24
  • 33
sparkly_frog
  • 771
  • 2
  • 9
  • 22
  • 1
    have a look at this for start : http://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android – Zaki Jul 25 '16 at 14:25
  • Maybe [this SO question](http://stackoverflow.com/questions/18352324/how-can-can-i-add-custom-buttons-into-an-alertdialogs-layout) can be of some help – Laur Ivan Jul 25 '16 at 14:25
  • Thank you, @Zaki. Looks like something I could try.. – sparkly_frog Jul 25 '16 at 14:41
  • You can do this but you shouldn't. Take a look at the material design guidelines for dialogs. They help to create dialogs with a good usability: https://material.io/guidelines/components/dialogs.html#dialogs-specs – goemic Jul 04 '17 at 10:50

4 Answers4

3

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>
abissa
  • 153
  • 1
  • 10
  • 1
    Thanks, but I need buttons to be at the bottom right, and they should not take full width of the dialog. I currently have this version implemented, but it requires customisation. – sparkly_frog Jul 25 '16 at 15:06
  • Hi, sure, that will probably bring buttons to the right side. However there is also a requirement to have image covering the whole area on the left half of the dialog. Thus, I think for this specific scenario chosen answer is more suitable. I'll keep it I'm mind for the future though. Thank you, @abissa. – sparkly_frog Aug 03 '16 at 08:54
1

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();
lelloman
  • 12,742
  • 5
  • 55
  • 75
1

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);
Simple
  • 730
  • 6
  • 16
0

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.

Nir Duan
  • 5,310
  • 4
  • 20
  • 38