Search code examples
javaandroidlayoutandroid-cardviewscreen-orientation

how to make a cardview with a icon in the center with the text below it?


I want to make 2 rows and 3 columns card view with text below it (not inside the card view) and an image in the center of the card view.

enter image description here


Solution

  • try this

    Just Copy and Paste your xml layout

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="3"
            android:layout_margin="10dp"
            >
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical"
                >
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:cardCornerRadius="10dp"
                    android:layout_margin="10dp"
                    app:cardElevation="10dp"
                    >
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/ic_launcher_background"
                        />
                </androidx.cardview.widget.CardView>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="dummy"
                    android:gravity="center"
                    />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical"
                >
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:cardCornerRadius="10dp"
                    android:layout_margin="10dp"
                    app:cardElevation="10dp"
                    >
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/ic_launcher_background"
                        />
                </androidx.cardview.widget.CardView>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="dummy"
                    android:gravity="center"
                    />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical"
                >
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:cardCornerRadius="10dp"
                    android:layout_margin="10dp"
                    app:cardElevation="10dp"
                    >
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/ic_launcher_background"
                        />
                </androidx.cardview.widget.CardView>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="dummy"
                    android:gravity="center"
                    />
    
            </LinearLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="3"
            android:layout_margin="10dp"
            >
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical"
                >
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:cardCornerRadius="10dp"
                    android:layout_margin="10dp"
                    app:cardElevation="10dp"
                    >
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/ic_launcher_background"
                        />
                </androidx.cardview.widget.CardView>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="dummy"
                    android:gravity="center"
                    />
    
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical"
                >
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:cardCornerRadius="10dp"
                    android:layout_margin="10dp"
                    app:cardElevation="10dp"
                    >
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/ic_launcher_background"
                        />
                </androidx.cardview.widget.CardView>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="dummy"
                    android:gravity="center"
                    />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical"
                >
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:cardCornerRadius="10dp"
                    android:layout_margin="10dp"
                    app:cardElevation="10dp"
                    >
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/ic_launcher_background"
                        />
                </androidx.cardview.widget.CardView>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="dummy"
                    android:gravity="center"
                    />
    
            </LinearLayout>
    
        </LinearLayout>
        
    </LinearLayout>