Skip to content Skip to sidebar Skip to footer

Dialogwhenlarge Screen Width Too Narrow

I need to make my dialog activity when the device is a tablet, wider and use more of the screen than it currently is. Please see screen shot: I need it to be about 70% of the scre

Solution 1:

You have two options

  1. Create custom dialog & add below line

    @OverridepublicvoidonAttachedToWindow() {
        DisplayMetricsmetrics= getContext().getResources().getDisplayMetrics();
        intwidth= metrics.widthPixels;
        intheight= metrics.heightPixels;
        getWindow().setLayout(width * 70 / 100, height * 30 / 100);
        super.onAttachedToWindow();
    }
    

Note: Change percentage as per your requirement

Or

  1. Do as per IntelliJ Amiya suggestion for current dialog

Solution 2:

Put this code in your OnCreate() method :-

DisplayMetricsdisplaymetrics=newDisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        intwidth= displaymetrics.widthPixels - 
common.convertDpToPixel(60, this);
        Log.i("widthval", "width" + width);
        getWindow().setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.CENTER);

This is a "convertDpToPixel" method Declare into your common file :-

publicstaticintconvertDpToPixel(int dp, Context context) {
        Resourcesresources= context.getResources();
        DisplayMetricsmetrics= resources.getDisplayMetrics();
        intpx= dp * ((int) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
        return px;
    }

I'm putted 60-dp but you can change it for your requirement.

Hope this helps you.

Solution 3:

For RESPONSIVE case,

Try this way,

DisplayMetricsmetrics= getResources().getDisplayMetrics();
    intwidth= metrics.widthPixels;
    intheight= metrics.heightPixels;
    finalDialogdialog=newDialog(YourActivity.this);
    dialog.getWindow().setLayout((int) (width*85 / 100), (int) (height*35 / 100));

Post a Comment for "Dialogwhenlarge Screen Width Too Narrow"