Home » Android » Android: How to set the absolute size of an Activity's Window?

Android: How to set the absolute size of an Activity's Window?

Posted by: admin June 15, 2020 Leave a comment

Questions:

Is there a way to set a GUI screen (Activity) to be 200dip wide and 150dip high, either using XML or programmatically?

I found getWindow().setLayout(), but that only take predefined constants for width and height.

How to&Answers:

You can use absolute numbers in Window.setLayout(), just like you can everywhere else you specify layout width and height. Sorry the doc isn’t clear on this.

Answer:

WindowManager.LayoutParams params = getWindow().getAttributes();  
        params.x = 0;  
        params.height = 650;  
        params.width = totalWidth;  
        params.y = totalHeght/10;

        this.getWindow().setAttributes(params); 

may be it works…but make sure about the values…

Answer:

Just use a full screen layout, and inside define another layout that has the size you want. Kind of like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:layout_width="200dip" 
        android:layout_height="150dip">
        <Button android:text="Button"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

I put a button in there just to show where the content goes.