Gus
Gus

Reputation: 2641

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?

Upvotes: 5

Views: 3607

Answers (2)

Michael Kazarian
Michael Kazarian

Reputation: 4462

@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);
    }

Upvotes: 4

Ben Roby G
Ben Roby G

Reputation: 118

It's impossible to set percents for android, BUT there is a way around. What I've done is get the screen width and multiply it by the percent I want my view or item to be (example: if I want something to fill 40% of the width if would be Screen-Width * 0.4)

Upvotes: -1

Related Questions