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?
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>
The BoundedViews library allows to set boundedWidth/boundedHeight on views even layout_width="match_parent"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With