how to set Linear layout child's height as tallest child height - android

I have four textviews in my Linear Layout and they have equal
width as follows,
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView86" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
My problem is, The textviews should be displayed with the same height.The content(text) for that textview will be set programmatically. I tried with Relative layout but the width of textview couldn't be same. I need both height and width should be same as tallest child.
Guide me please. Thanks!

Use below layout :
<LinearLayout
android:weightSum="1"
android:background="#color/colorPrimaryDark"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#color/black_30"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
/>
<TextView
android:background="#color/com_facebook_blue"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
/>
<TextView
android:background="#color/tw__composer_red"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
/>
<TextView
android:background="#color/colorAccent"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
/>
</LinearLayout>

Checkout below xml Code , You set the child height to wrap_content that should be match_parent :
<?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">
<TextView
android:background="#drawable/bluebackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView86" />
<LinearLayout
android:background="#color/colorPrimaryDark"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="#drawable/bluebackground"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<TextView
android:background="#drawable/bluebackground"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<TextView
android:background="#drawable/bluebackground"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<TextView
android:background="#drawable/bluebackground"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
</LinearLayout>
</LinearLayout>

Related

How to create layout_weight for vertical and horizontal at the same time in a RecyclerView item

I want a layout like below image for the item of a RecyclerView:
Below is my XML Item script:
<?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:orientation="horizontal" android:layout_width="match_parent"
android:weightSum="1"
android:layout_height="wrap_content">
<TextView
android:id="#+id/t"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp"
/>
<TextView
android:id="#+id/d"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp"
/>
</LinearLayout>
What changes to be needed so that vertical and horizontal weight will be applied on layout at same time using Linear Layout?
If you want to use LinearLayout You can use something like this:
[LinearLayout ] [LinearLayout ]
[child 1] [child 1](takes full screen height)
[child 2]
Here is the XML for it:
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button"/>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
</LinearLayout>
It will look like this:
Put it in Linear Layout and provide weight to your each layouts
<LinearLayout
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:id="#+id/myText"
android:text="Hello"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:id="#+id/newText"
android:text="NEW Text"/>
</LinearLayout>
<TextView
android:layout_weight="0.5"
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_height="wrap_content"
android:text="Hello"
android:id="#+id/Newtext"/>
</LinearLayout>
Thank you so much for your helps!
And this is my exact answer from your helps:
<?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:orientation="horizontal" android:layout_width="match_parent"
android:weightSum="1"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.4">
<TextView
android:id="#+id/t"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/d"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="0.6">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"/>
<Switch
android:id="#+id/switch1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.2"
android:checked="true" />
</LinearLayout>

Layout gravity bottom is not being applied

The below is my xml file in which i want a linearlayout to be in bottom i.e. layout gravity bottom. But it is not working.
Even after looking all solutions given for this type of problem, i am not able to solve it. Please Help
<?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">
<include layout="#layout/toolbar_layout" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewCart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" //----> not working
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/total"
android:textStyle="bold" />
<TextView
android:id="#+id/textViewTotalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="$23.00" />
</LinearLayout>
<Button
android:id="#+id/buttonCheckout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:drawableEnd="#drawable/ic_arrow_forward"
android:paddingEnd="15dp"
android:text="#string/checkout"
android:textColor="#color/white" />
</LinearLayout>
This is the way i want.
Try 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="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar_layout" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewCart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1" />
<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:layout_margin="20dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="total"
android:textStyle="bold" />
<TextView
android:id="#+id/textViewTotalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="$23.00" />
</LinearLayout>
<Button
android:id="#+id/buttonCheckout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:background="#color/colorPrimary"
android:drawableEnd="#drawable/ic_arrow_forward"
android:paddingEnd="15dp"
android:text="#string/checkout"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
Put Space layout between RecyclerView and LinearLayout
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
or use RelativeLayout instead of your main LinearLayout
Try this code.. and change 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="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar_layout" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewCart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="tees"
android:textStyle="bold" />
<TextView
android:id="#+id/textViewTotalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="$23.00" />
</LinearLayout>
<Button
android:id="#+id/buttonCheckout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:paddingEnd="15dp"
android:text="Hello" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>

(Android) Why the spacing exist in my gridview?

I am considering about the white blank spacing.
I don't know the reason why white spacing exist in my gridview.
I attached '.xml' and the 'result screen shot. '
I am trying use the gravity things.. but It dosen't work.
Could anyone help me??
...I got stackoverflow error 'It looks like your position is mostly 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="match_parent"
android:background="#fff"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="0.2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/set_border"
android:gravity="center"
android:text="#string/msg_diary_plan"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.53"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/btn_backmonth"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:background="#android:color/transparent"
android:text="" />
<TextView
android:id="#+id/tv_date"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="0.8"
android:background="#android:color/transparent"
android:gravity="center"
android:paddingLeft="10dp"
android:textColor="#000"
android:textSize="20sp" />
<Button
android:id="#+id/btn_nextmonth"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:background="#android:color/transparent"
android:text="" />
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.85"
android:listSelector="#ffff"
android:numColumns="7" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_alarm"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.17"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:orientation="vertical"
android:weightSum="1">
<Switch
android:id="#+id/sw_alarm"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Alarm" />
</LinearLayout>
<TimePicker
android:id="#+id/timePicker"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:timePickerMode="spinner" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<Button
android:id="#+id/btn_alarm_submit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:text="SUBMIT"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
and the result is
and this is the gridview item layout. Thanks
<?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"
android:weightSum="1">
<TextView
android:id="#+id/tv_item_gridview"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.8"
android:textColor="#000" />
<TextView
android:id="#+id/isDone"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.2"
android:visibility="visible" />
It is because you have yout item layout set to match_parent but TextViews to wrap_content. So by default it stays at the start position.
To fix this set your TextViews width to match_parent and gravity to center android:gravity="center"
So your item layout will look as 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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_item_gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:gravity="center"
tools:text="2"
android:textColor="#000" />
<TextView
android:id="#+id/isDone"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:gravity="center"
tools:text="13"
android:visibility="visible" />
</LinearLayout>

Android: Center two elements in a Linear Layout

I want that both elements are centered and without that stretching.
Also, it would be great if the weight was not that wide.
This is how it looks like:
Heres the code:
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/listColor"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/layout_item_value"
android:text="163,00"
android:textSize="18.2dp"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/txtListValue"
android:textColor="#color/back"
/>
<ImageView
android:id="#+id/image_order"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:scaleX="0.4"
android:scaleY="0.4"
android:gravity="center_vertical|center_horizontal"
/>
Thank You in advance!
Its very simple, you can maintain it with the parent layout itself
<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="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/txtListValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF1493"
android:text="163,00"
android:textColor="#000000"
android:textSize="30sp" />
<ImageView
android:id="#+id/image_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
try this layout:
<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="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/txtListValue"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FF1493"
android:gravity="center_vertical|center_horizontal"
android:text="163,00"
android:textColor="#000000"
android:textSize="30sp" />
<ImageView
android:id="#+id/image_order"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:scaleX="0.4"
android:scaleY="0.4"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
Output:
You can use below API without weightsum...
<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="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FC226E"
android:gravity="center"
android:text="1,00"
android:textColor="#FFFFFF"
android:textSize="28sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>

How can i create a grid of buttons with a textview below

Hi,
I want to create a screen like I've (tried to) drawn above.
A,b,c,d,e,f,g and h are buttons, and p is a EditText.
Now what I want to accomplish is that when deployed on a larger or smaller screen, or rotated, the following will happen:
element 'p' stays the same height but sizes horizontally (gets
wider/smaller)
elements a-h (the buttons) all size horizontally and
vertically. So when the screen gets twice as wide, the buttons are
two times wider, and when it's twice as high, the buttons are twice
as high.
I've been able to create one row of buttons to size horizontally (with a lineairlayout and a android:layout_weight, but I am not getting any further.
Try it out,hope it works:
<?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:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
Here's a solution that will work:
Set the EditText height as you wish.
<LinearLayout
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:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</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" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</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" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</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" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
One quick solution is to make use of nested layouts and nested weights. An example of this could be:
<LinearLayout
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:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</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" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</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" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</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" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
However, nested weights aren't recommended as they are computationally heavy. An alternative (superior) layout could be based on relative layouts to replace nested LinearLayouts and their weights. A hidden center component is used to split the relative layout in two. An example of this is posted below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- Hidden center view used to split relative layout in half -->
<View
android:id="#+id/hiddenView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/hiddenView1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/hiddenView1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- Hidden center view used to split relative layout in half -->
<View
android:id="#+id/hiddenView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/hiddenView2" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/hiddenView2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- Hidden center view used to split relative layout in half -->
<View
android:id="#+id/hiddenView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/hiddenView3" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/hiddenView3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- Hidden center view used to split relative layout in half -->
<View
android:id="#+id/hiddenView4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/hiddenView4" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/hiddenView4" />
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
The solution using RelativeLayout was inspired by the answer from DeeV in this SO question: Making two LinearLayouts have 50% of the screen each without using layout_weight
If you want to avoid the soft keyboard from showing when the layouts are inflated you can do the following to your parent LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true">
// try 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="match_parent"
android:padding="5dp"
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">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="a"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:text="b"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:layout_height="0dp">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="c"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:text="d"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:layout_height="0dp">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="e"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:text="f"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:layout_height="0dp">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="g"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:text="h"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="p"/>
</LinearLayout>

Categories

Resources