How to make a second color border with MaterialCardView?

391 Views Asked by At

I cannot find any questions online that have asked for this specifically

Screenshot of Square Border

How would I implement a red "second" border like in the image but on one side? I am using MaterialCardView for the main outside border. Would I just make a custom shape with red border on the left and use that as the background for the MaterialCardView?

2

There are 2 best solutions below

0
Rudra Rokaya On

First, create two drawable resource file like below:

bg_cardView_inner_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="@android:color/holo_red_dark"/>

<corners android:radius="7dp"/>

</shape>

bg_cardView_outline_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">


<solid android:color="@color/white" />
<stroke android:color="@android:color/holo_blue_light"
    android:width="5dp"/>
<corners android:radius="10dp"/>
</shape>

Then, in your XML file, Add those files as below:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
  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"
  tools:context=".DemoActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:background="@drawable/bg_cardview_outline_color"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardCornerRadius="5dp"
        app:cardElevation="2dp"
        app:cardUseCompatPadding="true"
        android:paddingBottom="5dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:background="@drawable/bg_cardview_inner_color">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:gravity="center_vertical|center"
                android:layout_marginStart="10dp">
                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:src="@drawable/your_image"
                    app:tint="@color/black"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="your text"
                    android:textSize="30sp"
                    android:gravity="center"
                    android:textStyle="bold"
                    />
            </LinearLayout>

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

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
0
MariosP On

This can be achieved easily with three nested MaterialCardViews by changing only the app:cardBackgroundColor, the app:cardCornerRadius and the android:layout_margin attributes for each card.

Below is the Xml Layout sample:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    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="200dp"
    android:layout_margin="10dp"
    app:cardBackgroundColor="#81afdc"
    app:cardCornerRadius="10dp">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        app:cardBackgroundColor="#a70e09"
        app:cardCornerRadius="10dp">

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="6dp"
            app:cardBackgroundColor="#ffffff"
            app:cardCornerRadius="6dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Sample Text"/>

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

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

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

Result:

material_cardview