I am new to Android and this is my first question here.
I am trying to add a colored vertical border at the beginning of the cardview. How can I achieve it on xml ? I tried adding it with empty textview but it is messing up the whole cardview itself. Please check the picture link posted below for example.
activity_main.xml
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:contentPadding="16dp"
card_view:cardElevation="2dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="@style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
style="@style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Content here" />
</LinearLayout>
</android.support.v7.widget.CardView>
Many thanks
I solved this by putting two CardViews in a RelativeLayout. One with background of the border color and the other one with the image. (or whatever you wish to use)
Note the margin added to top and start for the second CardView. In my case I decided to use a 2dp thick border.
try doing:
this removes the padding from the cardview and adds a FrameLayout with a color. You then need to fix the padding in the LinearLayout then for the other fields
Update
If you want to preserve the card corner radius create card_edge.xml in drawable folder:
and in the frame layout use
android:background="@drawable/card_edge"
I would like to improve the solution proposed by Amit. I'm utilizing the given resources without adding additional
shapes
orViews
. I'm givingCardView
a background color and then nested layout, white color to overprint yet with someleftMargin
...Start From the material design update, it support
app:strokeColor
and alsoapp:strokeWidth
. see moreto use material design update. add following code to
build.gradle
(:app)I think this solution may not be efficient but it serves the purpose and adds flexibility with the border width.