I have following layout. I have FrameLayout (fuchsia) filling entire screen and on the left I have ScrollView (silver). In ScrollView is LinearLayout (lime) with buttons. Above bottom button is TextView spacer. I want it to fill vertical space so that bottom button is at the bottom of screen. With scroll view scrolling works but spacer is not stretched:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<ScrollView
android:layout_width="134dp"
android:layout_height="match_parent"
android:background="#aaaaaa">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</ScrollView>
</FrameLayout>
If I remove ScollView then spacer is stretched but scrolling doesn't work in landscape mode:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<LinearLayout
android:layout_width="134dp"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</FrameLayout>
Solution:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<ScrollView
android:layout_width="134dp"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#aaaaaa">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Setting android:fillViewport="true" on the ScrollView fixed your issue and setting the height of ScrollView to match_parent.
I think constraint layout will help you.
<ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="134dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/btnBottom"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="134dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/btnBottom"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:text="Bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</ConstraintLayout>
Related
I want to evenly distribute the elements "checkbox, desayuno,elegir hora" to the full width of the screen.After the "elegir hora" button there is a small
Textview where the chosen time is shown, but very small. I have tried setting android:layout_weight="1" but it didn't work.this is how it is displayed:screenshot
Heres the layout 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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nombre de la pastilla"
android:gravity="center|center"
/>
<EditText
android:id="#+id/nombrePastilla"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:background="#color/color_blanco"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/checkBoxDesayuno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Desayuno"
android:layout_weight="1"
/>
<Button
android:id="#+id/buttonDesayuno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Elegir hora"
android:layout_weight="1"
/>
<TextView
android:id="#+id/horaDesayuno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=""
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/checkBoxComida"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Comida"
/>
<Button
android:id="#+id/buttonComida"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Elegir hora" />
<TextView
android:id="#+id/horaComida"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=""
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/checkBoxCena"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Cena" />
<Button
android:id="#+id/buttonCena"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Elegir hora" />
<TextView
android:id="#+id/horaCena"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=""
/>
</LinearLayout>
<Button
android:id="#+id/finalizar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Finalizar" />
</LinearLayout>
//You have to give weightsum to linear layout and layoutweight to your textview
and checkbox(Simply copy and paste this)
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|center"
android:text="Nombre de la pastilla" />
<EditText
android:id="#+id/nombrePastilla"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<CheckBox
android:id="#+id/checkBoxDesayuno"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Desayuno" />
<Button
android:id="#+id/buttonDesayuno"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="Elegir hora" />
<TextView
android:id="#+id/horaDesayuno"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="start|center_vertical"
android:text="dmeo"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<CheckBox
android:id="#+id/checkBoxComida"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Comida"
/>
<Button
android:id="#+id/buttonComida"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="Elegir hora" />
<TextView
android:id="#+id/horaComida"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="start|center_vertical"
android:text="dmeo"
android:textSize="18dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<CheckBox
android:id="#+id/checkBoxCena"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Cena" />
<Button
android:id="#+id/buttonCena"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="Elegir hora" />
<TextView
android:id="#+id/horaCena"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:gravity="start|center_vertical"
android:text="dmeo"
android:textSize="18dp"
/>
</LinearLayout>
<Button
android:id="#+id/finalizar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Finalizar" />
I have been trying to do a TicTacToe tutorial in Android Studio. I keep getting the multiple root tag error at the second LinearLayout. They guy in the video does not get the error but I do. I have been trying to find the solution but with no Luck.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text_view_p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player One: 0"
android:textSize="30sp" />
<TextView
android:id="#+id/text_view_p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_view_p1"
android:text="Player Two: 0"
android:textSize="30sp" />
<Button
android:id="#+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="54dp"
android:layout_marginRight="54dp"
android:layout_marginBottom="663dp"
android:text="reset" />
</RelativeLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
<Button
android:id="#+id/button_00"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_01"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_02"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
<Button
android:id="#+id/button_10"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_11"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_12"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
<Button
android:id="#+id/button_20"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_21"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_22"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
</LinearLayout>
</LinearLayout>
You started your layout as RelativeLayout - and it must end with the same tag. After closing it, you cannot add another LinearLayout or anything else. You closed Relative layout with /> too quickly and that's what cause your multiple root view problem.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout >
<RelativeLayout>
<TextView />
<TextView />
<Button />
</RelativeLayout>
<LinearLayout>
<Button />
<Button />
</LinearLayout>
<LinearLayout>
<Button />
<Button />
<Button />
</LinearLayout>
<LinearLayout>
<Button />
<Button />
<Button />
</LinearLayout>
</RelativeLayout>
You need to focus where to start and where to end the layouts. Use the following code in your activity.
Notice that <RelativeLayout /> is different than <RelativeLayout>....</RelativeLayout>.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/text_view_p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player One: 0"
android:textSize="30sp" />
<TextView
android:id="#+id/text_view_p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_view_p1"
android:text="Player Two: 0"
android:textSize="30sp" />
<Button
android:id="#+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="54dp"
android:layout_marginRight="54dp"
android:layout_marginBottom="663dp"
android:text="reset" />
</RelativeLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
<Button
android:id="#+id/button_00"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_01"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_02"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
<Button
android:id="#+id/button_10"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_11"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_12"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
<Button
android:id="#+id/button_20"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_21"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
<Button
android:id="#+id/button_22"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="60sp" />
</LinearLayout>
I'm new to Android and I want to create an activity with 3 set (line) of Buttons at the bottom of screen .For my example I wrote this code for 2 set (line) of buttons:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10" />
</LinearLayout>
</RelativeLayout>
I want to buttons 6-10 put on (above) the buttons 1-5 (like Stack).
How I can do this?
Thanks
You are using a RelativeLayout with two LinearLayouts.
You should give both your LinearLayout ids and after that you will position the other LinearLayout which you want to be on top by giving it the atrribute
android:layout_above="#id/the_id_of_the_one_you_want_to_be_below"
Try to wrap your two LinearLayout in a parent linearLayout.
That parent layout need to be aligned to the bottom.
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I tried to use a LinearLayout with 4 buttons but the last one was cut. How can I edit my code to get button's width corresponding to all smartphones'resolutions
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GREEN" />
</LinearLayout>
Thanks
make width="0dp" in all buttons and add
android:layout_weight="1"
to all buttons
First,set android:weightSum="4" in the LinearLayout.
And set android:layout_width="Odp" and android:layout_weight="1" in your Button .
<?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="match_parent"
android:weightSum="4"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
</LinearLayout>
Use weightSum to get this equally divided layout by adjusting layout_weight
Try this once:
<?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="match_parent"
android:weightSum="4"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="#+id/yellow"
android:layout_width=match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="#+id/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
</LinearLayout>
I am trying to place 4 buttons to fill all the relative layout.. I want each button have width and height till the center of the RelativeLayout (with about 2dp space between them).. I don't think that I can explain it very good, so I have made an image and here is the code I have made... Any ideas?? Thank you!! The view right now is:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAlignment="center"
android:textSize="30sp"
android:layout_marginTop="10dp"
android:id="#+id/textView" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/relativeLayout"
android:weightSum="1">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button4"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Try this code. I used two linear layouts instead of one relative layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="30sp" />
<LinearLayout
android:orientation="horizontal"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
</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">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="30sp" />
<LinearLayout
android:id="#+id/relativeLayout"
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="horizontal">
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="Button" />
</LinearLayout>
</LinearLayout>
</LinearLayout>