How to add a floating action button without AppCompat?

I want to create a new application using only API 21.

To monitor the design of materials, I try to use FAB, but I can not find decent information on how to use the one that is now included in the design library.

So far I have added the library the build.gradle file and added the FAB to my view (code below)

<android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:src="@android:drawable/ic_menu_add" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" app:elevation="5dp" app:borderWidth="0dp" app:fabSize="mini"/> 

(This is in a RelativeLayout)

When I launch the application, I get:

android.view.InflateException: binary line of XML file # 15: error inflating class android.support.design.widget.FloatingActionButton

I found one answer on SO that says I need to use the AppCompat theme, but I would prefer to use only Material / API21 material.

How can I use FAB without AppCompat (support libraries)?

+6
source share
5 answers

FAB is built into the new Design Design library, which will work on devices already in API 7. You will need to include it as a dependency for its use (and feel free to do it).

If you want to avoid using a library (or any external code), you will need to draw the FAB yourself (using the Drawable Shape).

The bottom line using the Design Compat lib is the preferred method of supporting FAB, and you should use it. The Design Compat lib is API21 Clean / Material.

The FAB implementation is NOT implemented for any other API, in addition, in the Design Support Library. You must enable lib or fully implement the code.

The lib construct has a dependency on AppCompat, so if you plan to use a native factory, you will also need to include the dependency on AppCompat.

From the official blog post : β€œPlease note that since the design library is dependent on the support libraries v4 and AppCompat, they will be included automatically when you add a dependency on the Design library.”

+6
source

The Material Design library is dependent on AppCompat. Check out @Booger answer. He reports on the official blog.

How to use FAB.

Add the dependency to your build.gradle :

 compile 'com.android.support:design:22.2.0' 

Just add FAB to your design:

 <android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:src="@drawable/ic_add" android:layout_marginBottom="@dimen/fab_margin_bottom" android:layout_marginRight="@dimen/fab_margin_right" app:elevation="6dp" app:borderWidth="0dp" app:fabSize="normal" /> 

There are currently some bugs.

You must define app:borderWidth="0dp" to display the shadow with API21 +.

You also need to define different fields for API 21+ and older devices.

RES / values ​​/ dens.xml

 <dimen name="fab_margin_right">0dp</dimen> <dimen name="fab_margin_bottom">0dp</dimen> 

RES / values-V21 / dens.xml

 <dimen name="fab_margin_right">16dp</dimen> <dimen name="fab_margin_bottom">16dp</dimen> 

FAB uses accent color, and you can override the app:backgroundTint attribute.

Finally, you can install ClickListener with:

 fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //do something } }); 
+1
source

GitHub also has several popular libraries:

FloatingActionButton , which can expand / collapse as a menu to reveal several actions.

-1
source

I would say that although you intend to make your application available only for API21, I urge you to implement the application using the Android Support Library AppCompat-v7 anyway. There are many benefits that you will get.

In any case, this is your choice. Just my suggestion. But if you are convinced. Or check out the complete guide to using the Android Design Support Library. http://inthecheesefactory.com/blog/android-design-support-library-codelab/en

Hope for this help

-1
source

Actually, I had the same problem. The solution was quite simple: you have to define app:rippleColor="@color/colorPrimary" and you can use FAB without using Appcompat applications and events. Of course you should enable compile 'com.android.support:support-v4:25.3.1' and compile 'com.android.support:design:25.3.1' but you DO NOT need to use AppCompatActivity and those themes.

  <android.support.design.widget.FloatingActionButton android:id="@+id/action_add_palette" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="16dp" android:src="@android:drawable/ic_input_add" app:rippleColor="@color/colorPrimary" /> 
-1
source

Source: https://habr.com/ru/post/989609/


All Articles