Android TableLayout make all buttons same size - android

I'm making a custom dialer and want to have all the buttons be the same size but because some of the text is longer than others (e.g. the "7" button has "PQRS" under it) it stretches out some columns. Here is what I'm doing currently, what am I doing wrong?
<TableLayout
android:id="#+id/tableView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*" >
<TableRow
android:layout_weight="1"
android:gravity="center" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_one" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_two" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_three" />
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_four" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_five" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_six" />
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_seven" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_eight" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_nine" />
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_star" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_zero" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="#string/dialer_pound" />
</TableRow>
</TableLayout>

Try this :
And if you don't want to fill your whole screen then remove the weightSum of the main TableLayout and respective layout_weight in TableRows
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tableView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:weightSum="4" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="3" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:text="dialer_one"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:text="dialer_two"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:text="dialer_three"
android:textSize="20sp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="3" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:gravity="center"
android:text="dialer_four"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:gravity="center"
android:text="dialer_five"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:gravity="center"
android:text="dialer_six"
android:textSize="20sp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="3" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:gravity="center_vertical"
android:text="dialer_seven"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:gravity="center_vertical"
android:text="dialer_eight"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:gravity="center_vertical"
android:text="dialer_nine"
android:textSize="20sp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="3" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:layout_gravity="center_vertical"
android:text="dialer_star"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:layout_gravity="center_vertical"
android:text="dialer_zero"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:soundEffectsEnabled="false"
android:layout_gravity="center_vertical"
android:text="dialer_pound"
android:textSize="20sp" />
</TableRow>
</TableLayout>
This should work

use linear layout instead of table layout and set weight=1 for all buttons. it will give you positive result what you want..

<TableLayout
android:id="#+id/tableView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_one" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_two" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_three" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_four" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_five" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_six" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_seven" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_eight" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_nine" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_star" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_zero" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:textSize="20sp"
android:text="dialer_pound" />
</TableRow>
I think this is what you want..

Use WeightSum and Weight in order to have all buttons of equal width like this,
<TableRow
android:layout_weight="1"
android:gravity="center"
android:weightSum="3" >
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:text="#string/dialer_one"
android:textSize="20sp" />
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:text="#string/dialer_two"
android:textSize="20sp" />
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:soundEffectsEnabled="false"
android:text="#string/dialer_three"
android:textSize="20sp" />
</TableRow>

It turns out that the buttons were all different sizes because the default padding around my button text was too high.

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>

TableRows change size when content changes

I'm trying to build a quiz app, and every time I change the text in a TextView or Button, the relative sizes of the TableRows change.
I have set all the TableRow heights to be 0dp, and all of the enclosed view heights to match_parent, but the heights still change.
The code below is my layout (quiz.xml), which I call from an activity using setContentView(R.layout.quiz).
All of the text is added programmatically.
I'm using Android IDE.
How can I force the TableRows/Buttons/TextViews to maintain their height (even if the text itself is too large)?
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TableRow
android:layout_weight="0.05"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:weightSum="1"
>
<Chronometer
android:id="#+id/chrono"
android:textSize="12sp"
android:gravity="top|left"
android:layout_weight="0.33"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/progress_text"
android:textSize="12sp"
android:gravity="top|center_horizontal"
android:layout_weight="0.34"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/score_text"
android:textSize="12sp"
android:gravity="top|right"
android:layout_weight="0.33"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</TableRow>
<TableRow
android:layout_weight="0.15"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
>
<TextView
android:id="#+id/question_text"
android:textSize="12sp"
android:gravity="top|left"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</TableRow>
<TableRow
android:layout_weight="0.1"
android:layout_width="match_parent"
android:layout_height="0dp"
>
<Button
android:id="#+id/choice1"
android:textSize="12sp"
android:gravity="top|left"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</TableRow>
<TableRow
android:layout_weight="0.1"
android:layout_width="match_parent"
android:layout_height="0dp"
>
<Button
android:id="#+id/choice2"
android:textSize="12sp"
android:gravity="top|left"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</TableRow>
<TableRow
android:layout_weight="0.1"
android:layout_width="match_parent"
android:layout_height="0dp"
>
<Button
android:id="#+id/choice3"
android:textSize="12sp"
android:gravity="top|left"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</TableRow>
<TableRow
android:layout_weight="0.1"
android:layout_width="match_parent"
android:layout_height="0dp"
>
<Button
android:id="#+id/choice4"
android:textSize="12sp"
android:gravity="top|left"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</TableRow>
<TableRow
android:layout_weight="0.1"
android:layout_width="match_parent"
android:layout_height="0dp"
>
<Button
android:id="#+id/choice5"
android:textSize="12sp"
android:gravity="top|left"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</TableRow>
<TableRow
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
>
<TextView
android:id="#+id/explanation_text"
android:textSize="10sp"
android:gravity="top|left"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</TableRow>
</TableLayout>

align three children left, right and center within LinearLayout

Maybe a duplicate, but I haven't found a solution yet. So, I have a vertical LinearLayout with a header which is a horizontal layout and a main content section (FrameLayout). Within the header I got three children of which one should be left align, the other one right aligned and the third on should fill the space in between.
I think I have to use a RelativeLayout instead of a LinearLayout, but if I do so, it fill's the parent completely and nothing else is visible.
Any suggestion? Basically the two button inside the red rectangle should be aligned to the right.
Here's the XML.
<LinearLayout
android:id="#+id/sort_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:text="#string/sort_by"
android:textColor="#color/white80"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/view_group"
style="#android:style/ButtonBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:orientation="horizontal"
android:padding="0dp" >
<ImageButton
android:id="#+id/btn_view_grid"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_grid" />
<ImageButton
android:id="#+id/btn_view_list"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_list" />
</RadioGroup>
<RadioGroup
android:id="#+id/sort_group"
style="#android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:gravity="center"
android:orientation="horizontal"
android:padding="0dp" >
<Button
android:id="#+id/btn_sort_popular"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_popular"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_new"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_newest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_hard"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_hardest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_easy"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_easiest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
</RadioGroup>
</LinearLayout>
<FrameLayout
android:id="#+id/preview_fragment"
android:background="#android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
something similar may help
<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/sort_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:text="sort_by"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/sort_group"
style="#android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:gravity="center"
android:orientation="horizontal"
android:padding="0dp" >
<Button
android:id="#+id/btn_sort_popular"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="sort_popular"
android:textColor="#FFFF"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_new"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:textColor="#FFFF"
android:text="sort_newest"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_hard"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:textColor="#FFFF"
android:text="sort_hardest"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_easy"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:textColor="#FFFF"
android:text="sort_easiest"
android:textSize="20sp" />
</RadioGroup>
<LinearLayout
android:id="#+id/sort_layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"
android:paddingBottom="10dp" >
<ImageButton
android:id="#+id/btn_view_grid"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:gravity="right"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_grid" />
<ImageButton
android:id="#+id/btn_view_list"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:gravity="right"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_list" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/preview_fragment"
android:background="#android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I finally figured out how to achieve this. I had to use the following values inside a RelativeLayout instead of a LinearLayout correctly:
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/view_group"
android:layout_toRightOf="#+id/label_sort"
Here's the XML:
<RelativeLayout
android:id="#+id/sort_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/label_sort"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-condensed"
android:text="#string/sort_by"
android:gravity="center"
android:textColor="#color/white80"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/view_group"
style="#android:style/ButtonBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#android:color/background_dark"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:padding="0dp" >
<ImageButton
android:id="#+id/btn_view_grid"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_grid" />
<ImageButton
android:id="#+id/btn_view_list"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:onClick="onShowList"
android:src="#drawable/ic_action_view_as_list" />
</RadioGroup>
<RadioGroup
android:id="#+id/sort_group"
style="#android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/view_group"
android:layout_toRightOf="#+id/label_sort"
android:background="#android:color/background_dark"
android:gravity="center"
android:orientation="horizontal"
android:padding="0dp" >
<Button
android:id="#+id/btn_sort_popular"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_popular"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_new"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_newest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_hard"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_hardest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_easy"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_easiest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
</RadioGroup>
</RelativeLayout>
<FrameLayout
android:id="#+id/preview_fragment"
android:background="#android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</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 Grid with LinearLayout

I'm trying to build a grid with some linear layouts.
Unfortunately the buttons inside the "grid" fill all the space and the "grid" isn't always the same size.
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/banner"
android:background="#80000000"
android:contentDescription="#string/about"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="#string/modi1"
tools:context=".MenuActivity" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="#string/modi2"
tools:context=".MenuActivity" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="10dp"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_diagramm" />
<Button
android:layout_marginTop="10dp"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_countdown" />
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_diagramm" />
<Button
android:layout_marginTop="10dp"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_countdown" />
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_diagramm" />
<Button
android:layout_marginTop="10dp"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/szen_countdown" />
</LinearLayout>
</LinearLayout>
</ScrollView>
It should look something like
this
with the buttons centered and at the size they need with the text inside.
Could anyone help me?
Try this.
<ScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#80000000"
android:contentDescription="ImageView"
android:layout_span="2"
android:src="#drawable/banner" />
</TableRow>
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="modi1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_weight="2"
android:gravity="center_horizontal"
android:text="modi2" />
</TableRow>
<TableRow>
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_diagramm" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_countdown" />
</TableRow>
<TableRow>
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_diagramm" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_countdown" />
</TableRow>
<TableRow>
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_diagramm" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="center"
android:text="szen_countdown" />
</TableRow>
</TableLayout>
this is how it shows to me
Remove the android:layout_weight="1" to avoid the button filling all the space.
You should try using a TableLayout and several TableRow to replace your LinearLayouts containing the buttons, thus creating a well formated grid
I had a similar problem, solved inserting a LinearLayout in TableRow. Have a look to the posted xml code and verify if it fits your requirements.
<TableRow
android:layout_gravity="fill_horizontal"
android:paddingTop="#dimen/table_row_padding_top" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageButton
android:contentDescription="#string/str_phone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/icon_phone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="#string/str_phone"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/icon_car"
android:contentDescription="#string/str_car" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="#string/str_car"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
</TableRow>

Categories

Resources