Skip to content
Advertisement

Cardview not displaying

I am trying to use com.google.android.material.card.MaterialCardView to display images and text. In the xml file it appears to be correct but when running the application, the card view is blank not even a text. Below are my xml file and grade file. The actual output I want also is shown in the picture (expected output) and the actual result is shown in the picture (actual output).

Xml code:

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/textView"
    android:layout_marginTop="20dp"
    android:layout_margin="20dp"
    android:columnCount="2"
    android:rowCount="3">


    <com.google.android.material.card.MaterialCardView
        android:id="@+id/cardAccount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_row="0"
        android:layout_column="0"
        app:cardCornerRadius="8dp"
        app:cardElevation="8dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_baseline_account"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="View Account"
                android:textSize="16sp"
                android:textColor="@color/black"/>



        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/cardLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_row="0"
        android:layout_column="1"
        app:cardCornerRadius="8dp"
        app:cardElevation="8dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_baseline_location"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Share Location"
                android:textSize="16sp"
                android:textColor="@color/black"/>



        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>


</GridLayout>

Gradle:

android {
compileSdk 32

defaultConfig {
    applicationId "com.example.quarantinemonitoringapp"
    minSdk 27
    targetSdk 32
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.cardview:cardview:1.0.0'

}

Expected Output:Expected output

Actual Output: enter image description here

Advertisement

Answer

Try this way

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#469F9F"
    tools:context=".MainActivity">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Welcome!"
                android:textColor="@color/black"
                android:textSize="20dp"
                android:textStyle="bold"
                android:padding="10dp"
                android:textAlignment="center"
                />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:columnCount="2"
        android:rowCount="3"
        >

        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="@color/white"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:padding="20dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher"
                     />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Text1"
                    android:paddingTop="10sp"
                    android:paddingBottom="10sp"
                    android:textAlignment="center"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
                


            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="@color/white"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:padding="20dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher"

                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Text2"
                    android:paddingTop="10sp"
                    android:paddingBottom="10sp"
                    android:textAlignment="center"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
                
            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="@color/white"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:padding="20dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher"

                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Text3"
                    android:paddingTop="10sp"
                    android:paddingBottom="10sp"
                    android:textAlignment="center"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
                
            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="@color/white"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:padding="20dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Text4"
                    android:paddingTop="10sp"
                    android:paddingBottom="10sp"
                    android:textAlignment="center"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
                
            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="2"
            android:layout_rowWeight="1"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="@color/white"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:padding="20dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Text5"
                    android:paddingTop="10sp"
                    android:paddingBottom="10sp"
                    android:textAlignment="center"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
                
            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="2"
            android:layout_rowWeight="1"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="@color/white"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:padding="20dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Text6"
                    android:paddingTop="10sp"
                    android:paddingBottom="10sp"
                    android:textAlignment="center"
                    android:textColor="@color/black"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>

    </GridLayout>


</LinearLayout>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement