0

I've tested this code on devices pre 5.0 and they all seem to work. When I try it on anything above 5.0 the images get clipped because their z position isn't at the front.

Here is the layout I'm trying to fix. The problem is the 5 images below the CardView. I want them to be somewhat "attatched" to the CardView" i.e the CardView has 5 images hanging off of it. They should be about 50% on the card and 50% off. I've achieved this with negative layout margin on android 4.4 and below but when I look at this on 5.0+ the images are underneath the CardView instead of on top of it.

I've tried to encase the CardView in a framelayout and do it that way but the views disappear entirely.

Any help would be greatly appreciated!

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clipChildren="false">

        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/card_rep_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/rep_dean" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp">

                    <ImageView
                        android:id="@+id/card_image_type"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true"
                        android:background="@drawable/reps_small_image" />

                    <TextView
                        android:id="@+id/card_type"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toRightOf="@id/card_image_type"
                        android:textColor="@color/reps_icon" />

                    <TextView
                        android:id="@+id/card_rep_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/card_type"
                        android:layout_toRightOf="@id/card_image_type"
                        android:textColor="#333333"
                        android:textSize="15dp"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:id="@+id/card_rep_bottom_container"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/card_rep_name"
                        android:layout_toRightOf="@id/card_image_type"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/card_rep_office_identifier"
                            android:layout_width="10dp"
                            android:layout_height="10dp"
                            android:visibility="gone" />

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

                        <Space
                            android:layout_width="5dp"
                            android:layout_height="wrap_content" />

                        <View
                            android:layout_width="2dp"
                            android:layout_height="10dp"
                            android:layout_marginTop="7dp"
                            android:background="@android:color/darker_gray" />

                        <Space
                            android:layout_width="5dp"
                            android:layout_height="wrap_content" />

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

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="30dp"
                            android:visibility="invisible" />

                    </LinearLayout>

                </RelativeLayout>

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


        <ImageView
            android:id="@+id/image_one"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_below="@id/card_view"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="-25dp"
            android:background="@drawable/social_bottom" />

        <ImageView
            android:id="@+id/image_two"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_below="@id/card_view"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="-25dp"
            android:layout_toLeftOf="@id/image_one"
            android:background="@drawable/bills_bottom" />

        <ImageView
            android:id="@+id/image_three"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_below="@id/card_view"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="-25dp"
            android:layout_toLeftOf="@id/image_two"
            android:background="@drawable/reps_gray_bottom" />

        <ImageView
            android:id="@+id/image_four"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_below="@id/card_view"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="-25dp"
            android:layout_toLeftOf="@id/image_three"
            android:background="@drawable/polls_bottom" />

        <ImageView
            android:id="@+id/image_five"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_below="@id/card_view"
            android:layout_marginTop="-25dp"
            android:layout_toLeftOf="@id/image_four"
            android:background="@drawable/news_bottom" />


    </RelativeLayout>
</LinearLayout>
cj1098
  • 1,560
  • 6
  • 32
  • 60

2 Answers2

1

Consider this

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_marginBottom="13dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

And the ImageViews :

<ImageView
        android:id="@+id/image_one"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_below="@id/card_view"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="-25dp"
        android:background="@drawable/social_bottom" />
Zain
  • 2,336
  • 1
  • 16
  • 25
  • I'm running into the same problem where the view is still "underneath" the CardView. I want it to be on top of it but 50% of the image "hangs off" the Card. – cj1098 May 29 '15 at 17:36
  • I've updated my answer above - But also consider using FrameLayout - http://stackoverflow.com/questions/4182486/placing-overlappingz-index-a-view-above-another-view-in-android – Zain May 29 '15 at 17:40
  • Thanks for your response, I figured it out ^_^ – cj1098 May 29 '15 at 17:55
0

I figured the solution for this out by translating my views on the Z axis so they were brought above the CardView.

This could've also bee achieved with FrameLayout or with bringToFront()

But this is also another solution.

cj1098
  • 1,560
  • 6
  • 32
  • 60