NSouth
NSouth

Reputation: 5276

How can I set a max width on a LinearLayout so that it doesn't stretch in Landscape mode?

My app looks great in portrait mode, but it stretches out in landscape and looks bad. I know I could make a separate layout for landscape, but I'd rather just set my root element's maximum width so that landscape mode just results in some white space on the sides. Is there a way to do this?

Upvotes: 5

Views: 5423

Answers (3)

Bao Le
Bao Le

Reputation: 17497

The BoundedViews library allows to set boundedWidth/boundedHeight on views even layout_width="match_parent"

Upvotes: 2

kouretinho
kouretinho

Reputation: 2260

Keeping different layout files is a correct solution but it adds complexity to your project (you'd have to maintain multiple files in case the designer redesigns the view layout).

If all you need is to alter the width of a LinearLayout, you can do the following. Keep one xml file in the layout folder and give it a value of

<LinearLayout
    style="@style/width_match_parent_max_200"
    android:layout_height="wrap_content">

then, in your values-600dp/styles.xml write:

<resources>
<style name="width_match_parent_max_200">
    <item name="android:layout_width">200dp</item>
</style>
</resources>

and in your values/styles.xml:

<resources>
<style name="width_match_parent_max_200">
    <item name="android:layout_width">match_parent</item>
</style>

Upvotes: 4

Corneliu Dascălu
Corneliu Dascălu

Reputation: 3958

The quickest solution I can think of is to set right and left margins, saved in separate dimen.xml files for portrait and landscape.

Upvotes: 1

Related Questions