Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Layout: width half of parent

I have a very simple layout that I can't make it looks like I want. It's a LinearLayout with a button and a Switch. I want them to show one above the other, but I want their width to be the half of the parent layout.

|--LinearLayout---------| |                       | | -----------           | ||   Switch  |          | | -----------           | | -----------           | ||   button  |          | | -----------           |  ------------------------ 

I've been looking at other similar answer in SO but I couldn't find a solution that works for me. This is what I've tried so far:

            <LinearLayout                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:orientation="vertical"                 android:weightSum="2" >                  <Switch                     android:id="@+id/remember_me_switch"                     android:layout_width="match_parent"                     android:layout_height="wrap_content"                     android:layout_weight="1"                     android:hint="@string/remember" />                  <Button                     android:id="@+id/share_button"                     android:layout_width="match_parent"                     android:layout_height="match_parent"                     android:layout_weight="1"                     android:onClick="loginOnclick"                     android:text="@string/login_button_text" />              </LinearLayout> 

With this, the button and the switch take all the space of the parent instead of take only the half. I've tried with android:layout_width = 0dp in the children but it makes they disappear.

Any help, please?

like image 504
mario595 Avatar asked Nov 15 '13 11:11

mario595


People also ask

How to set width half of the parent in android studio?

Short answer: Use android:weightSum="2" in your horizontal parent layout. Use android:layout_weight="1" and android:layout_width="0dp" in your children layouts.

What is android layout width?

android:layout_width Specifies the basic width of the view. This is a required attribute for any view inside of a containing layout manager.

What is parent layout in android?

Android RelativeLayout enables you to specify how child views are positioned relative to each other. The position of each view can be specified as relative to sibling elements or relative to the parent.

What is parent and child in android layout?

on the hierarchy parents are toplevel and child is below.so when you come to android layout parent is container and child is the content.


1 Answers

One possible way is to have a master horizontal LinearLayout that splits the width to 2, and inside it have the vertical layout

    <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal" >         <LinearLayout             android:layout_height="wrap_content"             android:layout_width="0dp"             android:layout_weight="1"             android:orientation="vertical" >              <Switch                 android:id="@+id/remember_me_switch"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:hint="@string/remember" />              <Button                 android:id="@+id/share_button"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:onClick="loginOnclick"                 android:text="@string/login_button_text" />          </LinearLayout>         <!-- Right side spacer -->         <View            android:layout_width="0dp"            android:layout_height="1dp"            android:layout_weight="1" />      </LinearLayout> 
like image 187
yoah Avatar answered Sep 24 '22 05:09

yoah