I have a table having 2 rows each row having 3 buttons. How can I make the buttons to fill the space equally. In HTML I would give them 33% width.
Also do you know any way I can create a view having 4 image buttons in a row as a grid layout, similar to the launcher.
Try adding android:stretchColumns="*"
to your <TableLayout>
tag.
I finally found the true answer here: http://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html
There is a more minimal example on stackoverflow also here: https://stackoverflow.com/a/2865558/265521
Example:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="Why is doing layouts on Android"
android:layout_weight="1"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="so"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="damn"
android:layout_weight="1"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="frustrating?"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
Set the TableRow layout_width to fill_parent and set a layout_weight of 1 on each button.
The layout_weight works sort of like a percentage. If all of your items get the same number, they take the same percent of space. If one button has a weight of 2, and another has a weight of 1, then the first will take up twice as much space.
If you haven't done so already, ready through the common layouts page of the dev guide for a good intro to layouts.
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