I am not able to control the height of the LinearLayout
. These won't align properly and won't fill up the width. I want the divider to be in the center and both the buttons on either side. Here is my code:
<LinearLayout
android:id="@+id/buttonFieldsLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/loginFieldsLayout" >
<Button
android:id="@+id/signUpButton"
style="@style/AuthButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_up_button_label" />
<View
android:id="@+id/buttonDivider"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@drawable/divider" />
<Button
android:id="@+id/cancelButton"
style="@style/AuthButton"
android:text="@string/cancel_button_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:startColor="@color/white"
android:centerColor="@color/blue"
android:endColor="@color/white" />
</shape>
Update 1:
View still sticks out after @Onik's suggestion
Update 2:
I removed the View
element and added this code in LinearLayout
instead and it worked!
android:divider="@drawable/divider"
android:showDividers="middle"
Note: android:divider
property is available only in Android
3.0 (API level 11) or higher.
Actually this link helped me and shows the proper way to put a divider: How to add (vertical) divider to a horizontal LinearLayout?
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”.
By default, orientation is horizontal. Horizontal orientation will only have one single row of elements on the screen and in the case of vertical orientation, it will only have one child per row. One more important attribute in a linear layout is the android:layout_weight attribute which assigns weights to elements.
You need to use layout_weight
attribute for Buttons
. To prevent divider
to fill the height of the layout (like entire height of the screen in the picture) use layout_height="match_parent"
instead of layout_height="wrap_content"
. This will fill the screen's (or parent's) width and set the button's
width to be equal, with the divider
between them of same height:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonFieldsLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/loginFieldsLayout" >
<Button
android:id="@+id/signUpButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="sign_up_button_label" />
<View
android:id="@+id/buttonDivider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@drawable/divider" />
<Button
android:id="@+id/cancelButton"
android:text="cancel_button_label"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With