Pre-Lollipop CardView Compatibility Issues

I have some problems using the new CardView

What's my current situation: I want to use a CardView to create a floating Action button for all devices (also Pre-Lollipop)

my activity layout looks like this

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:cardview="http://schemas.android.com/apk/res-auto"
         android:id="@+id/container"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="#cdcdcd"
         android:focusable="false">

<include layout="@layout/toolbar"/>


<android.support.v7.widget.CardView
    android:layout_width="58dp"
    android:layout_height="58dp"
    cardview:cardPreventCornerOverlap="true"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="18dp"
    cardview:cardCornerRadius="29dp"
    cardview:cardBackgroundColor="?attr/colorPrimary">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center"
        android:layout_margin="12dp"
        android:src="@android:drawable/ic_menu_edit"/>
</android.support.v7.widget.CardView>

      

Running the application on Nexus 5 (4.4.4) looks like this: MainActivity with FAB - no custom cardView corner radius

now i want to set cardElevation by setting this in xml

cardview:cardElevation="8dp"

      

After launching the application, the button looks like this (it is no longer a circle): MainActivity with FAB - custom cardView corner radius - set to 8dp

It seems that setting the height of the map also affects the dimensions of the view ... If you look closer to image # 1, you can see that this button is not a perfect circle either.

Is there a way to figure this out? I also tried to install this

cardview:cardPreventCornerOverlap="false"

      

But it also has no effect

Thanks guys:)

+3


source to share


2 answers


Using CardView for FAB shadows is not a good idea. CardView is a layout, so it's pretty heavy. It is also very limited for pre-Lollipop versions. The shadow is not animated, the corners are overlaid on the content, and there is no ripple. There seems to be no good method to achieve 100% FAB using only AppCompat.

In general, I'm not happy with the AppCompat limitation, so I wrote my own Button classes based on the regular ones. I was able to achieve very good results as you can see in the screenshot. This is gingerbread and has animated shadows, ripples, vector graphics, etc. This is a pretty big repository, so I can't give you a short solution here, but if you'd like, take a look at the code on github .



enter image description here

+1


source


You can try using the FAB from this MaterialDesign library if you are desperate for a shadow effect on older devices.

You can find the library at https://github.com/navasmdc/MaterialDesignLibrary

<com.gc.materialdesign.views.ButtonFloat
            android:id="@+id/buttonFloat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="24dp"
            android:background="#1E88E5"
            materialdesign:animate="true"
            materialdesign:iconDrawable="@drawable/ic_action_new" />

      

Alternatively, you can create your own shadow resource in your drawables folder and add a shape under your button, something like this:



<shape android:shape="oval"
 xmlns:android="http://schemas.android.com/apk/res/android">

 <solid android:color="@color/black_alpha"/>
 <corners android:radius="20dip"/>

      

And create a layer list resource where you include your button and shadow

<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shadow"/>
<item
 android:drawable="@drawable/button"
 android:bottom="4px" />  
 </layer-list>

      

0


source







All Articles