Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set percentage width for a dialog-themed Activity

I have an Activity with a dialog theme (Theme.Holo.DialogWhenLarge). It appears too narrow, and I'd like it to fill up a larger percentage of the screen. I am trying to accomplish this by overriding the windowMinWidthMinor and windowMinWidthMajor attributes.

The theme used by my Activity looks like this...

<style name="MyTheme" parent="@android:style/Theme.Holo.DialogWhenLarge">
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>

However, it seems like the windowMinWidthMajor and windowMinWidthMinor have no effect. Can anybody explain what I'm doing wrong?

like image 257
Gus Avatar asked Oct 21 '22 15:10

Gus


1 Answers

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Some code here
        setWindowHeight(90);
    }
    /**
     * Set percentage width height
     * @param percent percent from current size. From 0 to 100.
     */
    private void setWindowHeight(int percent){
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenHeight = metrics.heightPixels;
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = (int)(screenHeight*percent/100);
        this.getWindow().setAttributes(params);
    }
like image 95
Michael Kazarian Avatar answered Oct 24 '22 08:10

Michael Kazarian