2

is there a way to quickly and easily have a view match one of its siblings height or width? let's say I have this button and it has a sibling view element, in this case I don't care about width, but I need this button's height to match his sibling's

    <Button
        android:layout_width="wrap_content"
        android:layout_height="???"
        android:text="@string/muh_button"
        android:onClick="do_stuff"/>
user3453281
  • 559
  • 5
  • 12

1 Answers1

6

You could use a RelativeLayout and the layout_alignLeft and layout_alignRight XML attributes to match width (the analogous properties layout_alignTop and layout_alignBottom would work for matching height). This only works if you want the views aligned though - otherwise, you'll have to use a dimension resource.

Here's an example with aligned Buttons and matching heights:

<RelativeLayout
    ...>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/muh_button"
        android:onClick="do_stuff"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_toRightOf="@id/button1"
        android:layout_alignTop="@id/button1"
        android:layout_alignBottom="@id/button1"
        android:text="@string/muh_other_button"
        android:onClick="do_other_stuff"/>

</RelativeLayout>

Here's an example with a dimension resource:

<SomeViewGroup
    ...>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="@dimen/button_height"
        android:text="@string/muh_button"
        android:onClick="do_stuff"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="@dimen/button_height"
        android:text="@string/muh_other_button"
        android:onClick="do_other_stuff"/>

</SomeViewGroup>

where the dimension button_height is defined in dimens.xml.

stkent
  • 19,772
  • 14
  • 85
  • 111