resize Dialog - Android User Interface

Android examples for User Interface:Dialog

Description

resize Dialog

Demo Code


//package com.java2s;
import android.app.Activity;
import android.app.Dialog;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

public class Main {
    public static final String TAG = "WindowUtil";

    public static void resizeDialog(Activity activity, Dialog dialog) {
        if (dialog != null) {
            int maxWidth = 1000;
            DisplayMetrics metrics = activity.getApplication()
                    .getResources().getDisplayMetrics();
            maxWidth = (int) (maxWidth / (metrics.densityDpi / 160f));

            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(window.getAttributes());
            //Check max width px with screen width px
            Log.v(TAG, "SCREEN = " + metrics.widthPixels + " - MAX = "
                    + maxWidth);/*from  w  w  w  .  j  a  va2 s.  c  o m*/
            lp.width = (maxWidth <= 0 || ((maxWidth > 0) && metrics.widthPixels <= maxWidth)) ? ViewGroup.LayoutParams.MATCH_PARENT
                    : maxWidth;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }
    }
}

Related Tutorials