Buttons with equal heights - android

Linear layout below. This layout is aligned parent bottom in a Relative Layout. Problem is I want all buttons to have the same height. I have tried layout_gravity="fill" but that doesn't seem to work.
<LinearLayout android:id="#+id/button_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:layout_alignParentBottom="true">
<Button android:text="Send" android:id="#+id/send_button"
android:layout_weight="1" android:layout_gravity="fill"
android:layout_width="0dip" android:layout_height="wrap_content">
</Button>
<Button android:text="Report Missing Image" android:id="#+id/report_button"
android:layout_weight="1"
android:layout_width="0dip" android:layout_height="wrap_content">
</Button>
<Button android:text="Close" android:id="#+id/close_button"
android:layout_weight="1" android:layout_gravity="fill"
android:layout_width="0dip" android:layout_height="wrap_content">
</Button>
</LinearLayout>

For the Buttons in the same layout set the following:
android:layout_weight="1"
android:layout_height="fill_parent"
that way, the Button will have equal right to fill the parent layout's height so their heights will be the same size.

Try setting the layout_height on the buttons to fill_parent. This will cause them to all take up the amount of space in the parent.

You should specify the android:layout_weightsum parameter for LinearLayout with value 3. And for Buttons, the layout_weight as 1.
You don't need to specify the gravity.

Related

Android LinearLayout - wrap_content + Split remaining space

Consider the following layout: a horizontal LinearLayout with 2 buttons with the text "Push me", and an ImageButton with a small envelope icon (for example, to send an email).
I want the ImageButton's width to be set to "wrap_content" so it's only as large as the envelope's size, and then I want the other two buttons to evenly split the remainder of the screen evenly. How can it be done?
layout_weight specifies how much of the extra space in the layout to be allocated to the View.
So, for your envelope ImageButton set the android:layout_width="wrap_content". And for your remaining 2 buttons set android:layout_width="0dp" and add android:layout_weight="0.5" to both buttons.
Try this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Push me"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Push me"/>
</LinearLayout>
</LinearLayout>

How to make a view fill the extra space in a row?

In android app development, with Linear Layout, I have two buttons side-by-side in a row. I made such an arrangement with TableRow and center aligned it. But the buttons dont fill completely the extra space. How do I make the buttons fill the extra space to the left and to the right?
Here the xml code:
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_margin="7dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:textAlignment="center"
android:id="#+id/save"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAlignment="center"
android:id="#+id/cancel"
/>
</TableRow>
The LinearLayout has the following attributes:
<LinearLayout 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context="com.appname.classname">
Here is the sample screen shot:
Instead of using the TableRow, use an horizontal LinearLayout inside the vertical LinearLayout. Note that the default orientation of LinearLayout is horizontal. So you can do soemthing like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/save"
android:text="#string/save"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
<Button
android:id="#+id/cancel"
android:text="#string/cancel"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
</LinearLayout>
The layout_weight attribute will make each item occupy the available space in that ration. In this case, the buttons will share the screen space in a 1:1 ratio.
Set the android:layout_marginRight of the first button to "-8dip" or even more. The sspace should be smaller or gone.
You can use RelativeLayout, there are no margins there.

Can only LinearLayout specify a fixed-size element with an adjacent element that uses the remaining space?

I'd like to have two adjacent views, a first that is a fixed size and a second that adjacent to the first that uses the remaining space.
I could easily do this with LinearLayout and weights, but I would like to avoid the "nested weights are bad for performance" problem.
Is there another layout type that can accomplish the equivalent? Please provide an example if so.
A RelativeLayout could do what you want, for example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Button"
android:background="#99cc00" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/button1"
android:text="Button"
android:background="#0077cc"/>
</RelativeLayout>
The first Button will be 200dp in width and the second will stretch to fill the rest of the parent's remaining width.
You could also use a RelativeLayout to split two views in equal sizes to avoid having double weights on some layouts.
I believe this could be done with a RelativeLayout. Example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/button">
...
</LinearLayout>
</RelativeLayout>
You can try
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="view with fixed size " />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="view with remaining space" />
</LinearLayout>
this is how weight works in LinearLayout:
At first, it will deduct the fixed dimension, then according to the weight, divide the available space, assign to the views which specify the weight attribute

what is similar to layout_weight in relativelyout

In LinearLayout we can populate elements horizontally by using Layout_Weight which makes them occupy all the width and the width each take depends on their Layout_Weight value(or ratio). My question is how can I do the same in case of RelativeLayout. There is no attribute like Layout_Weight.
I am using a RelativeLayout in my project and it contains 4 buttons which need to be at the bottom of the screen and must fill the whole width. If I use hardcode Layout_Width="20dp" or something like that. It created a problem when I change the orientation from Portrait to Landscape or vice versa.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/feed_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/feed"
android:textColor="#FF000000" />
<Button
android:id="#+id/iwant_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/feed_button"
android:text="#string/iwant"
android:textColor="#FF000000" />
<Button
android:id="#+id/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/iwant_button"
android:text="#string/share"
android:textColor="#FF000000" />
<Button
android:id="#+id/profile_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/share_button"
android:text="#string/profile"
android:textColor="#FF000000" />
</RelativeLayout>
for making relative layout to take full width
android:layout_width="fill_parent"
to add it at the bottom
android:layout_alignParentBottom="true"
I think that there is no attribute like layout_weight in Relative layout .
But android has given functionality with Linear Layout . In that case, why would you want to go with Relative layout?
I think you know how to use layout_weight in Linear Layout.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/feed_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="feed"
android:textColor="#FF000000" />
<Button
android:id="#+id/iwant_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="iwant"
android:textColor="#FF000000" />
</LinearLayout>
I know you have compulsory use Relative Layout But if you put above code in your activity it show all buttons at bottom of display and If want to show those button layout at below of any other layout then use android:layout_below attributte in <LinearLayout > tag and use outer most layout as Relative Layout
Thanks
The only way to ensure the buttons are all equal with would be to put them in a LinearLayout that fills the area you want the buttons to cover like so:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<Button
android:id="#+id/feed_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/feed"
android:textColor="#FF000000" />
<Button
android:id="#+id/iwant_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_toRightOf="#id/feed_button"
android:text="#string/iwant"
android:textColor="#FF000000" />
<Button
android:id="#+id/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/share"
android:textColor="#FF000000" />
<Button
android:id="#+id/profile_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/profile"
android:textColor="#FF000000" />
</LinearLayout>
</RelativeLayout>
The LinearLayout will fill the entire width of the parent layout and will divide the buttons evenly based on their weight.
There is a very nifty trick if you have two layouts that you want to divide evenly in RelativeLayout. Create an "invisible" layout of 0x0 pixels, align it to the center of the parent view, then align the two layouts to the invisible layout relative to where you want them.
in relative layout we can achieve this dynamically.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Button1.setWidth(width / 2);
Button2.setWidth(width / 2);

Using layout_weight

Why the button on the middle dissapears? Instead of stretch to fill all the width of their parent? And whereas when I set layout_height = 0dp it doest with height and fills their parent?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
Since the linear layout orientation is vertical, only remaining height will be occupied if the layout_weight is "1", orientation is key here. if you want width to be shared, you might to change the orientation to "horizontal". keep in mind that your children then will be placed horizontally.

Categories

Resources