How to get a button's height to match another element's height?
AndroidLayoutHeightMatchAndroid Problem Overview
I want to put a button next to a EditText and I want their heights to match.
For example, from the built in Android browser:
The Go button is the same height as the EditText field. I know I could wrap both these views in a parent layout view, and set both of their heights to fill_parent, and that would make them match. However, I would like to do this without having to give the layout a static size. I would rather have the EditText take whatever height it needs based on the font size and then have the button next to it match whatever height that might be.
Is this possible with an xml layout?
Android Solutions
Solution 1 - Android
Presumably the EditText
and the Button
are inside a RelativeLayout
. Set the button's layout attributes to alignTop
and alignBottom
of the EditText
.
Solution 2 - Android
You will need to make the EditText
wrap_content
on its height and have the Button
just fill_parent
. You will need to have them both wrapped in a Layout
parent. That way they are associated with the same Layout
parent.
Try that and see how that works, if it helps let me know. If it doesn't maybe i can give you some code that will help you out.
Solution 3 - Android
What you want is a relative layout. An example with some comments is as follows
We start with this RelativeLayout as a parent. That can wrap all content. In that parent we put 2 elements, the button and the editText from your example.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
We start by placing the Button element on the top right corner. That is what the the layout_alignParentRight and layout_alignParentTop are all about. Again this is the biggest element so we will let it wrap all content using wrap_content for both height and width.
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="some_text" />
Now the second element, the editText we want to align to the left side of our previous element, use the id reference with the layout_toLeftOf parameter to accomplish just that.
<EditText
android:id="@+id/EditText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/Button1"
android:hint="some_hint"
android:inputType="textCapWords" />
Close the RelativeLayout and now render this to see what you probably got at yourself already.
</RelativeLayout>
Since the editText is smaller in height it won't match the Button it's placed next to. Solution for that is to add some more layout parameters. The magic ones you're looking for are layout_alignBottom and layout_alignParentTop.
android:layout_alignBottom="@+id/Button1"
android:layout_alignParentTop="true"
Add these 2 and you get your layout right.
Solution 4 - Android
With ConstraintLayout's introduction, this task has become far more simple using the match-constraint attribute