layout_height attribute is used to determine the height of a
View component in a specific unit.
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>
LinearLayout is the container, and setting
match_parent will set its height to match the screen size.
TextView has a
200dp and the
Button has a height of
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 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 constant will use the parent component’s height as the view’s height. The outermost component will use the screen’s height.
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. 👌
- How to change from one activity to another in Android
- Finding the libs folder in Android Studio
- Android putExtra() method with code examples in Java and Kotlin
- Android error: Your content must have a ListView whose id attribute is 'android.R.id.list'
- Android - How to change keyboard size (with screenshots)