3

My dialog is using a linearlayout for its root, and its using the following code as its custom theme:

    <style name="HuskyBusDialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@drawable/panel_background</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:colorBackgroundCacheHint">@null</item> 
</style>

Is it possible to set a max width? Its fine on phones but im trying to optimize it for tablets and they are too big.

jfisk
  • 6,125
  • 20
  • 77
  • 113

3 Answers3

1

An old question, however no replies are suitable, but you can find some hint here: How to customize the width and height when show an Activity as a Dialog

This helps customize the width and height, but not set the max width and height! To achieve that on an activity using a dialog theme, I had to do a couple of things:

1) set a layout listener to know when the dialog layout is set.

2) Adjust the dialog layout if its size was beyond the desired limit, effectively setting max size

Here is the working snippet I'm currently using, called after setContentView():

    getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener()
    {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout()
        {
            adjustDialog();
        }
    });

Now the dialog size adjustment, as a percentage of actual screen size:

private void adjustDialog()
{
    Window w = getWindow();
    w.setGravity(Gravity.CENTER);

    int current_width = w.getDecorView().getWidth();
    int current_height = w.getDecorView().getHeight();

    WindowManager.LayoutParams lp = w.getAttributes();

    DisplayMetrics dm = getResources().getDisplayMetrics();
    int max_width = (int) (dm.widthPixels * 0.90);
    int max_height = (int) (dm.heightPixels * 0.90);

    if (current_width > max_width)
    {
        lp.width = max_width;
    }
    if (current_height > max_height)
    {
        lp.height = max_height;
    }

    w.setAttributes(lp);
}
Community
  • 1
  • 1
3c71
  • 4,313
  • 31
  • 43
1

The width will be dealt with in the XML for the linear layout, not in the style that you apply to it. Use the android:layout_width tag in XML to specify how wide it could possibly be.

Glenn.nz
  • 2,261
  • 2
  • 17
  • 20
-2

yeaa if u wanna do with Widht GO for ur xml and try to do

android:layout_width="Defined in pixels//20px"

or u can try this also

android:width="Defined in pixels// 30px"

i hope it will be hepful to u

Raghav Chopra
  • 527
  • 1
  • 10
  • 26