10

I want to have a CardView which contains an ImageView which overlaps the left border of the CardView. I want to do this by giving the ImageView a negative margin. This works fine with all other layouts (LinearLayout/RelativeLayout/FrameLayout) by setting clipChildren="false".

However I can't get it to work with a CardView. The ImageView will be clipped and does not overlap the CardView.

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:clipChildren="false">
    <ImageView
           android:layout_marginLeft="-10dp"
           android:background="@drawable/some_picture"
           android:layout_width="50dp"
           android:layout_height="50dp"/>
</android.support.v7.widget.CardView>
tymm
  • 543
  • 1
  • 6
  • 18
  • why don't you put imageview outside the cardView, and then put both of them in a RelativeLayout overlapping each other? – Rohit Jagtap Sep 02 '15 at 14:13
  • 2
    Would be an option. However having the ImageView inside of the CardView feels like the cleaner solution to me. – tymm Sep 02 '15 at 14:51
  • Check out his link, hope it helps... http://stackoverflow.com/questions/14845891/how-to-achieve-android-ui-like-this-image-layout-about-androidclipchildren – Rohit Jagtap Sep 02 '15 at 15:30
  • Thank you. But as I wrote: I know that it works fine with everything else than CardViews. I would like to use CardViews though. – tymm Sep 02 '15 at 15:43
  • Were you able to find a solution or a workaround? – Krishnaraj Oct 27 '15 at 06:50
  • Does this answer your question? [clipChildren is not working even though set to false?](https://stackoverflow.com/questions/22930626/clipchildren-is-not-working-even-though-set-to-false) – Fattie Apr 14 '21 at 12:22

4 Answers4

11

Ok, this seems to be a problem only with Android 5+, the solution is to set

cardView.setClipToOutline(false);

Source - https://www.reddit.com/r/androiddev/comments/2tccge/cant_draw_outside_of_cardview_parent/

Krishnaraj
  • 2,360
  • 1
  • 32
  • 55
2

Add this to your MaterialCardView xml file

android:outlineProvider="none"
Grey Lưu
  • 51
  • 2
1

wrap it in LinearLayout with param LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false">

<android.support.v7.widget.CardView/>

</LinearLayout>
Dmitriy
  • 133
  • 4
-1

Add:

android:clipChildren="false" 

to CardView's grandfather View.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
icepeak
  • 9
  • 4