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:
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="Hello World!" />
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Button" />
</LinearLayout>
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. 👌