24

I need the dialog to fill the screen except for some space at the top and the bottom. I've search for a solution but couldn't find one probably because I'm declaring it in an onClickListener. Can someone please give a solution?

Activity code:

sort.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder sort = new AlertDialog.Builder(HomeScreen.this);
                // Get the layout inflater
                LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
                View sortView = inflater.inflate(R.layout.sort_layout, null);
                sort.setView(sortView);
                sort.create().show();
            }
        });

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="50dp"
    android:layout_marginTop="50dp"
    android:background="@color/white"
    android:orientation="vertical" android:layout_gravity="center">
    + some more stuff here I dont think it's relevant
</LinearLayout>

enter image description here

4

7 Answers 7

43

here set your screen width and width to the dialog

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = width;
    lp.height = height;
    dialog.getWindow().setAttributes(lp);

or in your styles create a style like this then

 <style name="DialogTheme" parent="android:Theme.Dialog">

    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>


    <!-- No backgrounds, titles or window float -->
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

create a dialog object like this

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

Edit ::

get width and height like this for any device it will fills..

WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
    int width, height;
    LayoutParams params;

    if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
        width = manager.getDefaultDisplay().getWidth();
        height = manager.getDefaultDisplay().getHeight();
    } else {
        Point point = new Point();
        manager.getDefaultDisplay().getSize(point);
        width = point.x;
        height = point.y;
    }
3
  • Your method kind of works but how do I set the values to fit the any screen? Commented Aug 19, 2013 at 14:04
  • It is working but I just used manager.getDefaultDisplay().getWidth() because point needs a higher API level and I'm developing for 2.2 Commented Aug 19, 2013 at 14:33
  • 2
    Setting <item name="android:windowBackground">@null</item> won't five you an transparent background, but <item name="android:windowBackground">@android:color/transparent</item> will. Commented Feb 28, 2014 at 16:25
11

first Make custom style for dialog in style.xml file:

<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

Then after set this theme to your Alert Dialog:

 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this,  R.style.full_screen_dialog));

Or You can call new Activity and Give this Custom style to that activity as a theme in your manifest file...

1
  • windowIsFloating = true adds to the dialog a margin from start and from end, how to resolve it? Commented Jun 1, 2018 at 9:42
4

This may helpful for someone. I want a dialog to take full width of screen. searched a lot but nothing found useful. Finally this worked for me:

mDialog.setContentView(R.layout.my_custom_dialog);
mDialog.getWindow().setBackgroundDrawable(null);

after adding this, my dialog appears in full width of screen.

0
3

Make object like below theme

Dialog objD = new Dialog(this,     android.R.style.Theme_Translucent_NoTitleBar);
2
   Dialog dialog2 = new Dialog(context, R.style.DialogTheme);
    dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog2.getWindow().setBackgroundDrawable(null);
    dialog2.setContentView(R.layout.dialog_img);
    WindowManager.LayoutParams lp = dialog2.getWindow().getAttributes();
    Window window = dialog2.getWindow();
    lp.copyFrom(window.getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    window.setAttributes(lp);
    lp.gravity = Gravity.CENTER;
    final ImageView imgprofile=(ImageView)dialog2.findViewById(R.id.img_centre);
    Picasso.with(context)
            .load(arrayImages.get(position).get("image"))
            .resize(800,1000)
            .centerInside()
            .into(imgprofile, new Callback() {

                @Override
                public void onSuccess() {

                }

                @Override
                public void onError() {
                    imgprofile.setImageResource(R.drawable.user);
                }
            });
    dialog2.show();

And in your dialog_img.xml file add an Imageview with scaleType(fitXY).

1

I think you shoud try this one:

Inside the dialogFragment:

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        }
1

The simplest way of creating a full screen dialog, create a style like this:

<style name="DialogFullScreenTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
    <item name="windowNoTitle">true</item>
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>

then:

dialog = new Dialog(this, R.style.DialogFullScreenTheme);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.