Android layout_height attribute explained

Posted on Jun 06, 2022

Learn how the Android layout_height attribute works with example code

The Android layout_height attribute is used to determine the height of a View component in a specific unit.

The layout_height attribute is required by all view components you create in your layout file.

To see an example of the layout_height attribute, here’s an XML file with three components:

        android:text="Hello World!" />
        android:text="Button" />

The LinearLayout is the container, and setting layout_height to match_parent will set its height to match the screen size.

The TextView has a layout_height of 200dp and the Button has a height of 100dp.

The design view will be as follows:

As with the layout_width attribute, there are five dimension units you can use for the height: dp, in, mm, pt, sp, and px.

The dp unit is the most recommended one because it’s a density-independent pixel that will adjust the component size to your screen’s density accordingly.

There are two special constants you can use for the height: match_parent and wrap_content.

The match_parent constant will use the parent component’s height as the view’s height. The outermost component will use the screen’s height.

The wrap_content constant will adjust the height depending on the content size. The more vertically large your content is, the greater the height size.

And that’s all there is to the layout_height attribute. 👌

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.