Felipe Porge Xavier Felipe Porge Xavier - 3 months ago 41
Android Question

How to remove dialog margins?

I'm trying to create a custom dialog to show a list of values at the bottom of screen.

How can I remove the dialog margins?

I want this...
enter image description here

I have this...
enter image description here

Dialog code:

Dialog d = new Dialog(this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.dialog_options);
Window window = d.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
wlp.gravity = Gravity.BOTTOM;
window.setAttributes(wlp);
d.show();


Solved! Better solution:

<style name="BottomOptionsDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:layout_margin">0dp</item>
<item name="android:padding">0dp</item>
<item name="android:layout_gravity">bottom</item>

<item name="colorPrimary">@color/orange_dark</item>
<item name="colorPrimaryDark">@color/orange_dark</item>
<item name="colorAccent">@color/gray_light</item>
</style>


And use new Dialog(context, R.theme.BottomOptionsDialogTheme);

Answer

Change your line:

Dialog d = new Dialog(this);

to:

Dialog d = new Dialog(this, R.style.DialogTheme);

and add in your styles.xml with parent corresponding to your theme version.

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <!-- Customize your theme here. -->
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

But in your situation it can be better to use Snackbar in my opinion. Here is tutorial.