Matthieu Nogueron
Matthieu Nogueron

Reputation: 54

CardView inside CardView: remove extra padding

I have implemented a layout composed of a CardView inside a CardView using AppCompat Support library for CardView. The first layout is the first layer and the second rests on it. Everything is fine on Lollipop, but as stated by Android dev doc, an extra padding is added in order to create shadows on pre-L versions.

Here is the L version:

L CardView version

and the pre-L version:

Pre-L CardView version

I've tried a lot of workarounds from other posts to remove these extra paddings, but nothing worked. I may have missed something but I don't know what.

Here is my layout code (I'm using appcompat-v7 r23):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:id="@+id/adapter_line_favorites"
    android:clickable="false">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        card_view:cardPreventCornerOverlap="false"
        card_view:contentPadding="0dp"
        card_view:cardCornerRadius="4dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="false"
            android:layout_alignParentRight="false"
            android:layout_alignParentEnd="false">

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                card_view:cardBackgroundColor="@color/CyanPrimaryDark"
                card_view:cardCornerRadius="4dp"
                android:id="@+id/line_layout">

                <FrameLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="70dp"
                        android:layout_gravity="center_horizontal">

                        <ImageView
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:id="@+id/line_icon"
                            android:layout_gravity="center"
                            android:layout_marginTop="15dp" />

                    </LinearLayout>

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:id="@+id/dropdown"
                        android:layout_gravity="center_vertical|right"
                        android:layout_marginRight="20dp"
                        android:focusableInTouchMode="false"
                        android:src="@drawable/ic_action_keyboard_arrow_down" />
                </FrameLayout>

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

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/inner_favorite"
                android:visibility="gone">

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

If anyone has a good workaround working on L and pre-L versions I'm more than interested!

Upvotes: 0

Views: 1642

Answers (1)

kd8bny
kd8bny

Reputation: 156

The problem is that you are using CardView from the support library. This Cardview uses its own padding/shadow mechanism on pre-L (api21.) The only way to fix this is to have your adapter use different card layouts for pre-L devices.

Similar question here. Here is an example, notice the method "determineLayout()" in createviewholder.

public class adapter_overview extends RecyclerView.Adapter<adapter_overview.AdapterViewHolder>{

public ArrayList<String> vehicleList = new ArrayList<>();
private View itemView;

public adapter_overview(ArrayList<String> vehicleList) {
    this.vehicleList.addAll(vehicleList.values());
}

@Override
public int getItemCount() {
    return vehicleList.size();
}

@Override
public AdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    itemView = LayoutInflater
            .from(viewGroup.getContext())
            .inflate(determineLayout(), viewGroup, 

    return new AdapterViewHolder(itemView);
}

@Override
public void onBindViewHolder(AdapterViewHolder adapterViewHolder, int i) {
    //do bind stuff
    }
}

public int determineLayout(){
        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ){
            return R.layout.card_overview_v19;

        }else{
            return R.layout.card_overview;

    }
}

public static class AdapterViewHolder extends RecyclerView.ViewHolder{


    public AdapterViewHolder(View view) {
        super(view);

        //set up text view/etc
    }
}

}

Upvotes: 1

Related Questions