Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set max Width for a Layout

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.

like image 880
MaikoMobile Avatar asked Jan 15 '12 20:01

MaikoMobile


People also ask

How do you set maximum width in HTML?

The max-width property defines the maximum width of an element. If the content is larger than the maximum width, it will automatically change the height of the element. If the content is smaller than the maximum width, the max-width property has no effect.

How do you find the width of a layout?

If you wanted to get the actual width of your view you would have to call getWidth() on your view once the layout has been inflated and displayed. Calling getWidth() before your layout has been displayed will result in 0.

What does Max-Width 100% mean?

"width" is a standard width measurement for defining the exact width of an element. Defining "width:100%" means that the width is 100%. Defining "max-width:100%" means that the width can be anywhere between 0% and 100% but no more then 100%. For example if my display has a 1200px availible width.

Can I use width and max-width at the same time?

If you set a fixed width and a max-width , this means the following: If the width goes above max-width , keep it at max-width . If the width is below max-width , keep it on width . It will never go over the max-width , that doesn't mean it can't stay under, the max keyword obviously indicates a limit and not a rule.


1 Answers

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

like image 67
kouretinho Avatar answered Sep 20 '22 13:09

kouretinho