Search code examples
androidandroid-linearlayoutandroid-cardview

Cardview is not visible when launching on my device


I'm attempting to use a cardview to show 2 images. Everything seems to work in android studio and i can see my layouts in the design tab perfectly. However, when i run on my tablet the cardview is not showing. The layout is working for a virtual device but not for my personal device.

<?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="#D3D3D3"
    android:weightSum="10">

<RelativeLayout
    android:layout_weight="2"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <TextView
        android:id="@+id/textGrid"
        android:text="GRID LAYOUT"
        android:textSize="34sp"
        android:textColor="#000"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

<GridLayout
    android:id="@+id/mainGrid"
    android:rowCount="3"
    android:columnCount="2"
    android:alignmentMode="alignMargins"
    android:columnOrderPreserved="false"
    android:layout_weight="8"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:padding="14dp">

    <androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_rowWeight="1"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:cardElevation="8dp"
        app:cardCornerRadius="8dp">

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

        </LinearLayout>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/torso2green" />

    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_rowWeight="1"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:cardElevation="8dp"
        app:cardCornerRadius="8dp">

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

        </LinearLayout>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/torso2" />

    </androidx.cardview.widget.CardView>

</GridLayout>

Things i've tried:

  • adding the dependency: implementation 'com.android.support:cardview-v7:28.0.0'

  • adding: android:hardwareAccelerated="true" to the manifest file

  • dragging the layouts in rather than coding them

I know it seems a bit basic, but been stuck for a while and can't seem to find a fix. Any help is much appreciated. enter image description here


Solution

  • <GridLayout
    android:id="@+id/mainGrid"
    android:rowCount="3"
    android:columnCount="2"
    android:alignmentMode="alignMargins"
    android:columnOrderPreserved="false"
    android:layout_weight="8"
    android:layout_width="match_parent"
    **android:layout_height="0dp"**
    android:padding="14dp">
    

    your layout hight is 0. I guess that's the problem.