Search code examples
androidandroid-layoutbackground-imagecover

Android Layout Help - rows with static height and covered background image


I'm new to Android development. I've searched and tried almost everything that I've found but I couldn't managed to create the layout below. Basiciliy it'll be rows with background image and text middle-left aligned. I want all rows to be static height and background image to be covered (like this: css).

layout that I want


Solution

  • It's not very clear but you could try a LinearLayout (vertical) with inside a RelativeLayout with fixed height for each items. In RelativeLayout aa ImageView with match_parent for background of item with scale type at center and a TextView for text with padding:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="100dp">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/btn_alert_1"
                android:scaleType="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="blabla"
                android:textColor="@android:color/holo_green_dark"
                android:paddingLeft="50dp"
                android:paddingTop="40dp"/>
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="100dp">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/btn_alert_2"
                android:scaleType="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="blabla"
                android:textColor="@android:color/holo_green_dark"
                android:paddingLeft="50dp"
                android:paddingTop="40dp"/>
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="100dp">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/btn_alert_1"
                android:scaleType="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="blabla"
                android:textColor="@android:color/holo_green_dark"
                android:paddingLeft="50dp"
                android:paddingTop="40dp"/>
        </RelativeLayout>
    </LinearLayout>
    

    Documentation about Android scale type: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html