134

I made a simple AlertDialog in my Activity:

View view = layoutInflater.inflate(R.layout.my_dialog, null);
AlertDialog infoDialog = new AlertDialog.Builder(MyActivity.this)
                    .setView(view)  
                    .create();

infoDialog.show();

With above code, the dialog shows at the (about) the center of the screen.

I am wondering, how can I customize the dialog position to make it showing just under the top Action Bar ? (Is there anyway to change the gravity or something of the dialog?) and how to do it based on my code??

Sharath
  • 691
  • 8
  • 23
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

18 Answers18

287

I used this code to show the dialog at the bottom of the screen:

Dialog dlg = <code to create custom dialog>;

Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();

wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);

This code also prevents android from dimming the background of the dialog, if you need it. You should be able to change the gravity parameter to move the dialog about

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • 1
    Hi, thank you. I set its gravity to top, the dialog goes on the top of the screen, but it also covered my action bar, I would like the dialog on top but just under the action bar...how to adjust this? – Leem.fin Feb 27 '12 at 15:09
  • 7
    You might try using `wlp.x` and `wlp.y` fields to explicitly set the location of the dialog on screen. I haven't tried it myself, but it should probably work. – Aleks G Feb 27 '12 at 15:40
  • 5
    The clearing of the dimming didn't work for me (though the rest worked like a charm). I found a solution to the dimming in another spot, in the form of: window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); – Rubberduck Mar 21 '14 at 17:32
  • @AleksG is there a way to also change color of dim background keeping dailog at center? I tried setting windowIsFloating=false to dailog style. But it align dailog to top! – Kshitij Aug 25 '14 at 13:08
  • @Kshitij If you want to keep dialogue in the centre, you don't need any of this code - Android centres dialogues by default. As for changing the dim colour, this is controlled by the theme. You may be able to override it by including corresponding theme in your app. I never tried it though. – Aleks G Aug 25 '14 at 19:15
  • 1
    By WindowManager source, wlp.y only works if the gravity is setted to TOP or BOTTOM – Joao Polo Jan 27 '16 at 14:37
  • Remove `wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;` to get back your shadows. – CopsOnRoad Dec 04 '16 at 17:01
  • @KyleR If you look at the date of the answer, you'll realise that it was written way before API 22 (I believe, I was using API 11 at the time). When you have floating buttons (i.e. full screen app), all app UI will be behind the floating navigation buttons - always. – Aleks G Feb 20 '18 at 21:54
28
private void showPictureialog() {
    final Dialog dialog = new Dialog(this,
            android.R.style.Theme_Translucent_NoTitleBar);

    // Setting dialogview
    Window window = dialog.getWindow();
    window.setGravity(Gravity.CENTER);

    window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    dialog.setTitle(null);
    dialog.setContentView(R.layout.selectpic_dialog);
    dialog.setCancelable(true);

    dialog.show();
}

you can customize you dialog based on gravity and layout parameters change gravity and layout parameter on the basis of your requirenment

Ramesh Solanki
  • 2,961
  • 4
  • 28
  • 44
  • 1
    Hi, thank you. I set its gravity to top, the dialog goes on the top of the screen, but it also covered my action bar, I would like the dialog on top but just under the action bar...how to adjust this? – Leem.fin Feb 27 '12 at 15:06
  • 3
    you can set margin top of layout – Ramesh Solanki Feb 28 '12 at 05:46
  • `'FILL_PARENT'` is deprecated. I am on API 21. As by [Developer Reference](https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html) – fWd82 Sep 27 '16 at 11:28
  • 1
    @fWd82 so use 'MATCH_PARENT' instead – William Nov 05 '16 at 05:03
26

I found this code snippet from @gypsicoder code here

    private CharSequence[] items = {"Set as Ringtone", "Set as Alarm"};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if(item == 0) {

            } else if(item == 1) {

            } else if(item == 2) {

            }
        }
    });
    
    AlertDialog dialog = builder.create();
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
    
    wmlp.gravity = Gravity.TOP | Gravity.LEFT;
    wmlp.x = 100;   //x position
    wmlp.y = 100;   //y position
    
    dialog.show();

Here x position's value is pixels from left to right. For y position value is from bottom to top.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
16

For me, this worked out pretty well where I was trying to position my dialog somewhere exactly at the bottom of the textview where it gets selected.

public void setPosition(int yValue) {
    Window window = getWindow();
    WindowManager.LayoutParams param = window.getAttributes();
    param.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
    param.y = yValue;
    window.setAttributes(param);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41
15

New BottomSheetDialog:

BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);    
dialog.setContentView(YourView);

dialog.show();
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
9

just add this to your code:

dialog.getWindow().setGravity(Gravity.BOTTOM);
Ashish Sinha
  • 87
  • 2
  • 10
Ashok J
  • 91
  • 1
  • 2
3

use bottomSHeet :

BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();
abbasalim
  • 3,118
  • 2
  • 23
  • 45
3

You can use this snippet to place the AlertDialog at the bottom of the screen.

AlertDialog dialogPopup;

dialogPopup = mBuilder.create();
dialogPopup.getWindow().getAttributes().gravity = Gravity.BOTTOM;
3

kotlin:

val dialog = Dialog(context)
//set graviy bottom
dialog.window.setGravity(Gravity.BOTTOM)
3

in Java

AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);

AlertDialog alertDialog = builder.create();  
// set the gravity      
alertDialog.getWindow().setGravity(Gravity.TOP);
// set the margin 
alertDialog.getWindow().getAttributes().verticalMargin = 0.1F;
alertDialog.show();
Snow Star
  • 76
  • 1
  • 5
3

The easiest way to set Dialog gravity in kotlin:

dialog.window?.setGravity(Gravity.TOP) // Set the gravity here
private fun searchDialog() {
    val dialog = Dialog(this)
    dialog.window?.setGravity(Gravity.TOP) // Set the gravity here
    val binding = DialogSearchHomeBinding.inflate(layoutInflater)
    dialog.setContentView(binding.root)
    dialog.show()
}
AI Shakil
  • 1,015
  • 10
  • 20
2
dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM;
Hossein Mansouri
  • 752
  • 1
  • 13
  • 29
2
public class MyDialogFragment extends DialogFragment{
    protected void setDialogGravity(int gravity) {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = WindowManager.LayoutParams.MATCH_PARENT;
                params.height = WindowManager.LayoutParams.MATCH_PARENT;
                params.horizontalMargin = 0;
                params.gravity = gravity;
                params.dimAmount = 0;
                params.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                window.setAttributes(params);
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);

        return inflater.inflate(R.layout.my_dialog, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
                super.onViewCreated(view, savedInstanceState);
                setDialogGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    }
}
gotwo
  • 663
  • 8
  • 16
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
2

i use this method

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();
    wlp.gravity = Gravity.BOTTOM;
    dialog.setContentView(R.layout.dialog_droppoint);
    dialog.show();
    window.setAttributes(wlp);
    return dialog;

}
1

For my Dialog activity i used this one:

WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.gravity = Gravity.BOTTOM;
Andris
  • 3,895
  • 2
  • 24
  • 27
0

I have coded a custom Dialog, with a custom layout. It has a cancel and a save button and you can set up also gravity on the device's screen (bottom) and define the width and height of the dialog.

private void showDialog(final String scanContent, final String currentTime, final String currentDate) { LayoutInflater linf = LayoutInflater.from(this); final View inflator = linf.inflate(R.layout.dialog_barcode_result_dialog, null);

final Dialog dialog = new Dialog(this, android.R.style.Theme_DeviceDefault_Light_Dialog);

// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);

dialog.getWindow().setLayout(375, 350);

window.setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.dialog_barcode_result_dialog);

dialog.setTitle(getResources().getString(R.string.dialog_title));
dialog.setContentView(inflator);

final Button save = inflator.findViewById(R.id.saveBtn);
final Button cancel = inflator.findViewById(R.id.cancelBtn);
final TextView message = inflator.findViewById(R.id.dialog_message_text);
message.setText(scanContent);
save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        dialog.cancel();

    }
});
cancel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        dialog.cancel();

    }
});

dialog.show();

}

the dialog layout xml file is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:minWidth="350dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:textSize="16sp"
                android:layout_marginBottom="10dp"
                android:id="@+id/dialog_message_text"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="right"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/cancelBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/cancel" />

                <Button
                    android:id="@+id/saveBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/button_save" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>
</LinearLayout>
Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23
0

Java version of kotlin code suggested by A.I.Shakil :

 AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
            .setView(v)
            .setCancelable(false).create();
    dialog.getWindow().setGravity(Gravity.TOP);
    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    dialog.show();
PRANAV SINGH
  • 1,000
  • 11
  • 17
0

For showing dialog at a certain position just add this line at the onViewCreated method.

dialog?.window?.attributes?.gravity = Gravity.BOTTOM

for sure you can use any gravity value.

Marawan Mamdouh
  • 584
  • 1
  • 6
  • 15