how to set max Width for a Layout

MaikoMobile picture MaikoMobile · Jan 15, 2012 · Viewed 48.9k times · Source

After a lot of googling I'm unable to find a solution to this problem. I have this layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/adlayout"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
<TableLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:stretchColumns="1"
   android:layout_weight="1"
  >

  <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent"   android:layout_height="wrap_content">
       <TextView android:padding="6dip" android:text="@string/barcode_format"  android:id="@+id/textView1" android:layout_width="wrap_content"   android:layout_height="wrap_content"></TextView>
       <EditText android:padding="6dip" android:layout_width="fill_parent" android:id="@+id/edit_barcode_format" android:layout_height="wrap_content"></EditText>
  </TableRow>
  <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content">
      <TextView android:padding="6dip" android:text="@string/barcode_type" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
      <EditText android:padding="6dip" android:layout_width="fill_parent" android:id="@+id/edit_barcode_type" android:layout_height="wrap_content"></EditText>
  </TableRow>        
 </TableLayout>


</LinearLayout>

The table layout defines a form. On a phone looks ok but on a tablet, the edittext views looks too large. I want to set a max Width for the layout, and paint the form centered.

Answer

kouretinho picture kouretinho · Feb 3, 2015

Keeping different layout files as @Devunwired suggested is correct, however, 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 button, I would suggest 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">

values-w600dp/styles.xml contains

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

values/styles.xml contains

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

Edit: Note the folder is values-w600dp and not values-600dp