Evenly spaced out row of buttons required - android

Using 3 rows and 4 columns I have tried to evenly space out all the buttons horizontally but have failed. vertically is fine.
Below are 3 different attempts to space out the buttons, 1 for each of my 3 rows. The first row stretches the graphic (which is unwanted), the second row does not stretch but there is no spacing (spacing between the graphics is required), while the third stretches without any spacing. Please help me to evenly space these out. Other peoples solutions on this same site have not worked. Should I try using a table instead?
Visual summary= without stretching the graphic:
I want | x x x x |
and not | xxxx |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
<Button
android:id="#+id/a1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/clear64" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
<Button
android:id="#+id/a2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/clear64" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
<Button
android:id="#+id/a3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/clear64" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
<Button
android:id="#+id/a4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/clear64" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight=".1" >
</View>
</LinearLayout>
<LinearLayout
android:id="#+id/row2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/b1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center"
android:src="#drawable/clear64" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/b2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center"
android:src="#drawable/clear64" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/b3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center"
android:src="#drawable/clear64" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/b4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blanktilesml"
android:gravity="center"
android:src="#drawable/clear64" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/row3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/c1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#drawable/blanktilesml"
android:paddingLeft="10dp"
android:src="#drawable/clear64" />
<Button
android:id="#+id/c2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#drawable/blanktilesml"
android:paddingLeft="10dp"
android:src="#drawable/clear64" />
<Button
android:id="#+id/c3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#drawable/blanktilesml"
android:paddingLeft="10dp"
android:src="#drawable/clear64" />
<Button
android:id="#+id/c4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:background="#drawable/blanktilesml"
android:paddingLeft="10dp"
android:src="#drawable/clear64" />
</LinearLayout>
</LinearLayout>

Try this:
<LinearLayout
android:id="#+id/row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/arrow_up_float"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/arrow_up_float"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/a3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/arrow_up_float"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="#+id/a4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/arrow_up_float"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
try this for your first row. Similarly can be done for all the row,
So unless you don't want the images to be stretched you need to rap them in a layout. Doing so you will manage the spacing and the drawable will look just fine. If you stretch the buttons then the drawable will also be stretched accordingly.

Here is what I did with a calculator project I was working on. Requiring buttons in rows all the same size. You will need to add padding to your buttons to suit your needs.:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dp"
android:id="#+id/NumberPad">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/NumberRow123"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button1"
android:layout_weight="1"
android:background="#drawable/unpressed7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button2"
android:layout_weight="1"
android:background="#drawable/unpressed8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/button3"
android:layout_weight="1"
android:background="#drawable/unpressed9"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/NumerRow456"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button4"
android:layout_weight="1"
android:background="#drawable/unpressed4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button5"
android:layout_weight="1"
android:background="#drawable/unpressed5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button6"
android:layout_weight="1"
android:background="#drawable/unpressed6"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/NumberRow789">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button7"
android:layout_weight="1"
android:background="#drawable/unpressed1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button8"
android:layout_weight="1"
android:background="#drawable/unpressed2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/button9"
android:layout_weight="1"
android:background="#drawable/unpressed3"/>
</LinearLayout>

Related

Android even vertical spacing between widgets

I have a fragment layout that looks like this:
The top 3 rows are spaced nicely, but the age and sex rows are too short vertically. If I were to hard code a vertical size for the entire layout, is there a way to ensure every item is evenly spaced vertically? Currently, due to the different widget types being used, the bottom too dont look very nice
Here is the code for my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Research Assistant:" />
<EditText
android:id="#+id/input_ra"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ems="10"
android:inputType="textCapWords"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Subject Number:" />
<EditText
android:id="#+id/input_subnum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ems="10"
android:inputType="text"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Condition:" />
<EditText
android:id="#+id/input_condition"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ems="10"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Age:" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/input_age_spinner"
android:layout_weight="0.6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Sex:" />
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="#+id/input_button_male"
android:paddingRight="10dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/input_button_female"
android:paddingRight="10dp"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="50dp">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:orientation="horizontal" >
<Button
android:id="#+id/input_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:text="Save" />
<Button
android:id="#+id/input_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:text="Reset" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Try this. Each row view is equally apart from each other.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:weightSum="12"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_weight="2" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Research Assistant:" />
<EditText
android:id="#+id/input_ra"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:textAppearance="#android:style/TextAppearance.Medium"
android:ems="10"
android:inputType="textCapWords"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_weight="2"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Subject Number:" />
<EditText
android:id="#+id/input_subnum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ems="10"
android:inputType="text"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="2"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Condition:" />
<EditText
android:id="#+id/input_condition"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ems="10"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="2"
android:gravity="center"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Age:" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/input_age_spinner"
android:layout_weight="0.6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal"
android:layout_weight="2"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Sex:" />
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="#+id/input_button_male"
android:paddingRight="10dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/input_button_female"
android:paddingRight="10dp"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="2"
android:weightSum="1">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="0.75"
android:orientation="horizontal" >
<Button
android:id="#+id/input_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:text="Save" />
<Button
android:id="#+id/input_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:text="Reset" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Hope this works for you.
Perhaps you could consider using weightSum and weight attribute to evenly distribute it.
Below is the edited code based on that. Noted your height would be now 0dp, and I give more weight to the last buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:padding="20dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Research Assistant:" />
<EditText
android:id="#+id/input_ra"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ems="10"
android:inputType="textCapWords"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Subject Number:" />
<EditText
android:id="#+id/input_subnum"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ems="10"
android:inputType="text"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Condition:" />
<EditText
android:id="#+id/input_condition"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:ems="10"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Age:" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/input_age_spinner"
android:layout_weight="0.6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Sex:" />
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="#+id/input_button_male"
android:paddingRight="10dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/input_button_female"
android:paddingRight="10dp"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:paddingTop="50dp"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:orientation="horizontal" >
<Button
android:id="#+id/input_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:text="Save" />
<Button
android:id="#+id/input_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:text="Reset" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

how to set in textview same sizes

I need to implement textview with equal sizes for 3 character, but i have got some problem if i add:
This four buttons have got diferent sizes ...but i dont know why, and how can i change
MOD
_X_
_(_
_5_
_ is white spaces..
There is xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dp" >
<TextView
android:id="#+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" >
</TextView>
<TextView
android:id="#+id/item_text_Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" >
</TextView>
End there is picture of my problem:
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:gravity="center"
android:text="MC_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="MS_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="M+_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_(_"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:layout_marginLeft="5dp"
android:text="_)_"/>
</LinearLayout>
</LinearLayout>
Give your TextView a fix size width by changing layout_width from wrap_content to some fix dp size like 30dp
Example:
<TextView
android:id="#+id/item_text"
android:layout_width="32dp"
android:layout_height="wrap_content"
android:text="MOD"
android:textSize="12sp" >
</TextView>
look like you try to make button in table like in XML???
don`t know if this can help you, but when i try to make same precise width i use this...
here`s the explanation
create 1 linear layout with vertical orientation (so, everytime you add new item, it will placed to down)
inside it, insert linear layout with horizontal orientation... (insert as many row you want, in you case looks like you need 5 linear layout with horizontal orientation)
after that, insert you button / textview inside the linear layout (horizontal orientation), so your button / text will placed horizontaly.
if you want to change row, just insert the button / textview inside another linear layout (horizontal orientation).
after you insert all, set the linear layout (horizontal value) with this code
android:weightSum="5"
why 5? because in your case, u make 5 column.
and dont forget to set the width = fill parent.
after that set the button /textview inside the linearlayout (horizontal) with add this code
android:layout_weight="1"
and dont forget to change the width with fill parent.
with this, i can make precise width in every row. next step you can set the margin to make your button / textview looks good.
hope this can help you...
=====================================
here`s the code if you confuse with my explanation
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.tes.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<Button
android:id="#+id/Button22"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button21"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button24"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button23"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button20"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<Button
android:id="#+id/Button17"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button16"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button19"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button18"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button15"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<Button
android:id="#+id/Button12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button14"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button13"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<Button
android:id="#+id/Button07"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button06"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button09"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button08"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button05"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<Button
android:id="#+id/Button04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"/>
</LinearLayout>

Position of the elements in Android XML file

I have a question regarding how to position elements in a XML file for an android activity.
What i need to achieve is the following:
Basically i need:
a set of 9 squares (3x3 table) covering 50% of the screen height
on the second part of the screen, 3 equally distributed zones with a single textview / button for each.
i created the set of 9 squares using a tableview with this code:
<TableLayout
android:layout_width="match_parent"
android:layout_height="386dp"
android:stretchColumns="*" >
<TableRow
android:id="#+id/Row1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/square1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
<Button
android:id="#+id/square2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="3"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
<Button
android:id="#+id/square3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="3"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
</TableRow>
<TableRow
android:id="#+id/Row2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/square4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
<Button
android:id="#+id/square5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
<Button
android:id="#+id/square6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
</TableRow>
<TableRow
android:id="#+id/Row3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/square7"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
<Button
android:id="#+id/square8"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
<Button
android:id="#+id/square9"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:text="3"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
</TableRow>
</TableLayout>
This part is included in a relative layout.
Now i can't get in any way to set this part to be 50% of the screen, and add a second 50% with the 3 buttons.
Hope you can help me. I searched here for answers but didn't find any relevant topic
Here you go:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<Button
android:text="1"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
<Button
android:text="2"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
<Button
android:text="3"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<Button
android:text="4"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
<Button
android:text="5"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
<Button
android:text="6"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<Button
android:text="7"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
<Button
android:text="8"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
<Button
android:text="9"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<Button
android:text="10"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<Button
android:text="11"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<Button
android:text="12"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
</TableRow>
Use LinearLayout instead of RelativeLayout and use android:weight attribute to achieve 50% screen height as follows...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_wieght="1" >
..............
..............
</TableLayout>
<!-- Other other layout which will take android:layout_wieght="1" -->
</LinearLayout>
This will be a very simple answer to this question only using LinearLayout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="one part"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="one part"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="one part"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="one part"/>
</LinearLayout>
</LinearLayout>

Android layout of a menu to fill screen with buttons

I have a 3 by 3 stack of buttons which i want to make into a menu,
How can i lay them out so that they automatically fill the screen ? eg if there are 3 in a row each one will take up a third etc.
here is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
<ImageButton
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
android:contentDescription="Food"/>
<ImageButton
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageButton
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageButton
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageButton
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageButton
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Try this code.
In this code I set equal weight[1] for three linear layout rows. And in each linearlayout I set equal weight[1] to each buttons or imageviews.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/ic_launcher"/>
<ImageButton
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/ic_launcher"
android:contentDescription="Food"/>
<ImageButton
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/ic_launcher"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<ImageButton
android:id="#+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<ImageButton
android:id="#+id/button6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/button7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<ImageButton
android:id="#+id/button8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<ImageButton
android:id="#+id/button9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
You can use weighting. See the docs for LinearLayout params.
Basically if you have a LinearLayout that takes up the whole width, with three buttons in it, you can set weight on all the buttons to 1 and they will fill the layout taking up equal space.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3" >
<ImageButton
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/ic_launcher" />
<ImageButton
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/ic_launcher"
android:contentDescription="Food" />
<ImageButton
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3" >
<ImageButton
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<ImageButton
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<ImageButton
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3" >
<ImageButton
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<ImageButton
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<ImageButton
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>

How to fill remaining space in layout?

I have these button like shown on the picture below, and I want them to arrange themselve so they begin at the top of the layout and end at the bottom, and also auto arrange space in between. How to do that?
My xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="5dp" >
<Button
android:id="#+id/bA1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A1"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bA2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A2"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bA3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A3"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bA4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A4"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bA5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A5"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bA6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A6"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bA7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A7"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bA8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A8"
android:background="#drawable/buttons" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="top"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="10dp" >
<Button
android:id="#+id/bB1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/buttons"
android:text="B1" />
<Button
android:id="#+id/bB2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B2"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bB3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B3"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bB4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B4"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bB5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B5"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bB6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B6"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bB7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B7"
android:background="#drawable/buttons" />
<Button
android:id="#+id/bB8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B8"
android:background="#drawable/buttons" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Use weight. Some sample code follows.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:padding="48dp"
android:weightSum="4" >
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 3" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 4" />
</LinearLayout>
The code android:weightSum="4"will say to use a "weight" of 4, and each child here use 1 of that weight, a procentual value (25%). You could alter the weightSum and the weight of each child and thus giving each child a percentage width or height. Use 0dp to manipulate the dimension.
Try this.. its working for me.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_weight="8" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:weightSum="8" >
<Button
android:id="#+id/bA1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="A1" />
<Button
android:id="#+id/bA2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="A2" />
<Button
android:id="#+id/bA3"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="A3" />
<Button
android:id="#+id/bA4"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="A4" />
<Button
android:id="#+id/bA5"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="A5" />
<Button
android:id="#+id/bA6"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="A6" />
<Button
android:id="#+id/bA7"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="A7" />
<Button
android:id="#+id/bA8"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="A8" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:weightSum="8" >
<Button
android:id="#+id/bB1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="B1" />
<Button
android:id="#+id/bB2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="B2" />
<Button
android:id="#+id/bB3"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="B3" />
<Button
android:id="#+id/bB4"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="B4" />
<Button
android:id="#+id/bB5"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="B5" />
<Button
android:id="#+id/bB6"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="B6" />
<Button
android:id="#+id/bB7"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="B7" />
<Button
android:id="#+id/bB8"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/dialog_frame"
android:text="B8" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:background="#android:drawable/dark_header"
android:layout_weight="1" />
Now it will look like
As i added Header and Footer also.

Categories

Resources