I have created a dialpad using LinearLayout. Here is the code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:clickable="true"
android:text="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tv_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:clickable="true"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tv_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="6" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="8" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="9" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:clickable="true"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:clickable="true"
android:text="0" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="64dp"
android:text="#" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which looks like this
Now i want the same in RelativeLayout, but i find layout_weight doesn't work in RelativeLayout. I also don't want to use LinearLayout inside RelativeLayout.
Or is there any alternative which can work same like layout_weight works in LinearLayout but for RelativeLayout
You cannot use percentages to define the dimensions of a View inside a RelativeLayout. The best ways to do it is to use LinearLayout and weights, or a custom Layout.
Fore more information you can look at this question
If you want to stay with relative layout you can try to change the locations through the code. get the height and width of the outer layout using:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative);
rl.getWidth();
rl.getHeight();
and then get the params of the layout you want to evenly distribute:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50); //here i created a new one with size 50,50
now you can set params.leftMargin and params.topMargin the way you want with any calculation.
params.leftMargin = rl.getWidth()/4 ;
for example will set the new layout at the 1/4 of the screen width
RelativeLayout is not a good choice because it doesn't provide a way you to evenly distribute children the way LinearLayout does. If you want something other than nested LinearLayouts, you can try using GridLayout (not GridView).
You cannot use layout_weight in RelativeLayout. Looking over your XML, your current layout is poorly structured which will lead to poor performance. I suggest that you use either GridLayout or Use your current LinearLayout but instead of assigning a LinearLayout to each number/character you can do something like this instead (Only for first row, but you get the idea):
`
<LinearLayout
android:id="#+id/LL_topRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_margin="2dp">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="64sp"
android:clickable="true"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="64sp"
android:clickable="true"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="64sp"
android:clickable="true"
android:gravity="center"
android:text="3" />
</LinearLayout>
<!-- Next LinearLayout Here for next row -->
`
Related
I have three buttons need to be located in one line at the bottom of the screen. Below is the code in activity xml. In order to make the three button takes even space, I wrap each of them inside a LinearLayout and set the layout android:layout_weight to 1 and android:layout_gravity to center.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90px"
android:background="#88104502"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/goBack"
android:layout_width="70px"
android:layout_height="80px"
android:gravity="center"
android:background="#drawable/back"
android:onClick="toHomePage" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/findPlant"
android:layout_width="70px"
android:layout_height="80px"
android:gravity="center"
android:background="#drawable/flowr"
android:onClick="toImagePage" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_weight="1">
<Button
android:id="#+id/plantList"
android:layout_width="70px"
android:layout_height="80px"
android:gravity="center"
android:background="#drawable/list" />
</LinearLayout>
</LinearLayout>
But it the button is not located in the center of the LinearLayout. Below is the screenshot:
You can see that each button is located on the left of the LinearLayout. How to make them center located?
If you want a button with an image you can use ImageButton instead of Button, and you can try like this, and instead using nested layout you can give directly weight to ImageButton.
If you want to style a button you can use AppCompatImageButton in that you can use app:backgroundTint to change the background.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90px"
android:background="#88104502"
android:weightSum="3"
android:orientation="horizontal">
<ImageButton
android:id="#+id/goBack"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="toHomePage"
android:src="#drawable/ic_action_name"/>
<ImageButton
android:id="#+id/findPlant"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="toImagePage"
android:src="#drawable/ic_action_name"/>
<ImageButton
android:id="#+id/plantList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="toImagePage"
android:src="#drawable/ic_action_name"/>
</LinearLayout>
Try this. I use your code only give gravity to linear layout.If you do not want to use this much of LinearLayout use only one main LinearLayout and give equal weight to 3 buttons.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="90px"
android:background="#88104502"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1">
<Button
android:id="#+id/goBack"
android:layout_width="70px"
android:layout_height="80px"
android:gravity="center"
android:background="#mipmap/ic_launcher"
android:onClick="toHomePage" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/findPlant"
android:layout_width="70px"
android:layout_height="80px"
android:gravity="center"
android:background="#mipmap/ic_launcher"
android:onClick="toImagePage" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1">
<Button
android:id="#+id/plantList"
android:layout_width="70px"
android:layout_height="80px"
android:background="#mipmap/ic_launcher" />
</LinearLayout>
you can create an empty view as the padding. this way the spacing will always be the same even if the icon widths are different.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90px"
android:background="#88104502"
android:orientation="horizontal">
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"/>
<Button
android:id="#+id/button1"
android:layout_width="70px"
android:layout_height="80px"
android:background="#drawable/list" />
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"/>
<Button
android:id="#+id/button2"
android:layout_width="70px"
android:layout_height="80px"
android:background="#drawable/list" />
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"/>
<Button
android:id="#+id/button3"
android:layout_width="70px"
android:layout_height="80px"
android:background="#drawable/list" />
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"/>
</LinearLayout>
Use gravity instead of layout_gravity.
I am trying to layout 4 views (2 buttons, 2 TextViews) at the top of my activity. I need button_2 to be centered horizontally, TextView_2 to take up the rest of the space to the right-hand side of the view, button_1 to wrap_content and be pinned to the left of the parent and textView_1 to fill the space between button_1 and button_2.
My code so far:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="#+id/textView_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_weight="1"/>
<TextView
android:id="#+id/textView_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="button_1"/>
<TextView
android:id="#+id/textView_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/button_1"
android:layout_toLeftOf="#+id/button_2"
android:layout_alignBottom="#+id/button_2"
android:text="textView_1"/>
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="button_2"/>
<TextView
android:id="#+id/textView_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textView_2"
android:gravity="center"
android:layout_alignBottom="#+id/button_2"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/button_2" />
</RelativeLayout>
Relative Layout will suit to your condition perfectly. Use android:layout_toRightOf, android:layout_toLeftOf, android:layout_alignParentRight and android:layout_alignParentLeft properties wisely.
<RelativeLayout
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/textView_1"
android:layout_toRightOf="#+id/button_1"
android:layout_toleftOf="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello there"/>
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/textView_1"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/textView_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/button_2"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Try this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
remove layout weight in child linear layout and height is change to wrap content for textview and button to fulfull your requirements
I have LinearLayout inside that relataive layout is there. I want to keep RelativeLayout right side of the LinearLayout. How to do this one. below is my code
<LinearLayout android:id="#+id/status_bar_contents"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="6dp"
android:paddingEnd="8dp"
android:orientation="horizontal">
<com.android.keyguard.AlphaOptimizedLinearLayout android:id="#+id/system_icon_area"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/network"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_marginRight="20dp"
android:background="#drawable/network_strength_0"/>
<com.android.systemui.statusbar.policy.Clock
android:id="#+id/clock"
android:textAppearance="#style/TextAppearance.StatusBar.Clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:gravity="center"
android:layout_toRightOf="#id/network"
/>
</RelativeLayout>
</com.android.keyguard.AlphaOptimizedLinearLayout>
</LinearLayout>
use this one
<LinearLayout android:id="#+id/status_bar_contents"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="6dp"
android:paddingEnd="8dp"
android:orientation="horizontal">
<com.android.keyguard.AlphaOptimizedLinearLayout android:id="#+id/system_icon_area"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal">
</com.android.keyguard.AlphaOptimizedLinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/network"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_marginRight="20dp"
android:background="#drawable/network_strength_0"/>
<com.android.systemui.statusbar.policy.Clock
android:id="#+id/clock"
android:textAppearance="#style/TextAppearance.StatusBar.Clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:gravity="center"
android:layout_toRightOf="#id/network"
/>
</RelativeLayout>
</LinearLayout>
Note:-Weight is divide you parent LinearLayout into 2 View with same space .IF you want space between 2 views that put weight in ratio....
# Shiv you can try this ..hope this can help you,,
<LinearLayout android:id="#+id/status_bar_contents"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="6dp"
android:paddingEnd="8dp"
android:weightSum="1"
android:orientation="horizontal">
<com.android.keyguard.AlphaOptimizedLinearLayout android:id="#+id/system_icon_area"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="match_parent">
</com.android.keyguard.AlphaOptimizedLinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/network"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:layout_marginRight="20dp"
android:background="#drawable/network_strength_0"/>
<com.android.systemui.statusbar.policy.Clock
android:id="#+id/clock"
android:textAppearance="#style/TextAppearance.StatusBar.Clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:gravity="center"
android:layout_toRightOf="#id/network"/>
</RelativeLayout>
</LinearLayout>
Here is my Code
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="wrap_content">
<RadioButton android:id="#+id/preference_question_optionTwo" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
Now here when i apply android:layout_weight="1" on LinearLayout the image does not appear.Without this property image is displayed.
What i want is to divide the layout into two parts. Both parts containing the same code above i.e a radio button and besides that an image and a text
Update This is how i want it
This is what i am getting as output
You didn't share the rest of your code or your intentions on how you want to divide the screen into two: vertically or horizontally.
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout android:layout_width="0dp"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_weight="1" >
<RadioButton android:id="#+id/preference_question_optionOne" android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="0dp"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_weight="1" >
<RadioButton android:id="#+id/preference_question_optionTwo" android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
It seems that you got confused by the weight concept. It will be more clear this way, I guess. You should divide the parent's width or height according to those weights. Also don't forget to use it with 0dp.
If you want to divide the parent vertically, use this combination:
android:layout_height="0dp"
android:layout_width="match_parent" <!--or wrap_content-->
android:layout_weight="x"
and for horizontally
android:layout_height="match_parent" <!--or wrap_content-->
android:layout_width="0dp"
android:layout_weight="x"
The layout weight property is for elements within a linear layout. You should give each half of the screen the same weight and put them both within a linear layout.
Here's what you need:
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent">
<!-- first half -->
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="wrap_content">
<RadioButton android:id="#+id/preference_question_optionTwo" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
<!-- first half, same weight as first half -->
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_height="wrap_content">
<RadioButton android:id="#id/preference_question_optionTwo" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
If You need to divide Your layout in two parts with the same width, you need to set childs width to fill_parent and set the same wieght (> 1) to children.
For example:
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<RadioButton android:id="#+id/preference_question_optionTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
Wanna to split a screen for my app with two LinearLayouts. What parameters should I use to make exact splitting in two equal parts - first LinearLayout on the top and the second one is just under it.
Use the layout_weight attribute. The layout will roughly look like this:
<LinearLayout android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"/>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"/>
</LinearLayout>
I am answering this question after 4-5 years but best practices to do this as below
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/secondView"
android:orientation="vertical"></LinearLayout>
<View
android:id="#+id/secondView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/secondView"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
This is right approach as use of layout_weight is always heavy for UI operations.
Splitting Layout equally using LinearLayout is not good practice
Just putting it out there:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:weightSum="4"
android:padding="5dp"> <!-- to show what the parent is -->
<LinearLayout
android:background="#0000FF"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2" />
<LinearLayout
android:background="#00FF00"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
In order to split the ui into two equal parts you can use weightSum of 2 in the parent LinearLayout and assign layout_weight of 1 to each as shown below
<?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="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
To Split a layout to equal parts
Use Layout Weights. Keep in mind that it is important to set layout_width as 0dp on children to make it work as intended.
on parent layout:
Set weightSum of parent Layout as 1 (android:weightSum="1")
on the child layout:
Set layout_width as 0dp (android:layout_width="0dp")
Set layout_weight as 0.5 [half of weight sum fr equal two] (android:layout_weight="0.5")
To split layout to three equal parts:
parent: weightSum 3
child: layout_weight: 1
To split layout to four equal parts:
parent: weightSum 1
child: layout_weight: 0.25
To split layout to n equal parts:
parent: weightSum n
child: layout_weight: 1
Below is an example layout for splitting layout to two equal parts.
<LinearLayout
android:id="#+id/layout_top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView .. />
<EditText .../>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView ../>
<EditText ../>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_marginTop="16dp"
android:textSize="18sp"
android:textStyle="bold"
android:padding="4dp"
android:textColor="#EA80FC"
android:fontFamily="sans-serif-medium"
android:text="#string/team_a"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/team_a_score"
android:text="#string/_0"
android:textSize="56sp"
android:padding="4dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/team_a_fouls"
android:text="#string/fouls"
android:padding="4dp"
android:textSize="26sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="#string/_1_points"
android:layout_width="match_parent"
android:onClick="addOnePointTeamA"
android:textColor="#fff"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
<Button
android:text="#string/_2_points"
android:textColor="#fff"
android:onClick="addTwoPointTeamA"
android:layout_width="match_parent"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
<Button
android:text="#string/_3_points"
android:textColor="#fff"
android:onClick="addThreePointTeamA"
android:layout_margin="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="#string/_1_point_foul"
android:textColor="#fff"
android:layout_width="match_parent"
android:onClick="addOnePointFoulTeamA"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="#string/team_b"
android:textColor="#EA80FC"
android:textStyle="bold"
android:padding="4dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-medium"
android:textSize="18sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/team_b_score"
android:text="0"
android:padding="4dp"
android:textSize="56sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/team_b_fouls"
android:text="Fouls"
android:padding="4dp"
android:textSize="26sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="#string/_1_points"
android:textColor="#fff"
android:fontFamily="sans-serif-medium"
android:layout_width="match_parent"
android:onClick="addOnePointTeamB"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
<Button
android:text="#string/_2_points"
android:layout_margin="6dp"
android:fontFamily="sans-serif-medium"
android:textColor="#fff"
android:onClick="addTwoPointTeamB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="#string/_3_points"
android:fontFamily="sans-serif-medium"
android:textColor="#fff"
android:onClick="addThreePointTeamB"
android:layout_margin="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="#string/_1_point_foul"
android:textColor="#fff"
android:onClick="addOnePointFoulTeamB"
android:layout_width="match_parent"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<Button
android:text="#string/reset"
android:layout_marginBottom="25dp"
android:onClick="resetScore"
android:textColor="#fff"
android:fontFamily="sans-serif-medium"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>