I'm looking to make a new app, using only API 21.
To follow the Material Design, I'm trying to use a FAB, but I cannot find any decent information on how to use the one now included in the Design Library.
So far, I have added the library the the build.gradle file, and added a 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 app, I get:
android.view.InflateException: Binary XML file line #15: Error
inflating class android.support.design.widget.FloatingActionButton
I found one answer on SO that says I need to use an AppCompat theme, but I'd rather just use pure Material/API21 stuff.
How can I use a FAB without AppCompat (support libraries)?
The FAB is built into the new Design Compat library - which will work on devices as early as API 7. You will need to include it as a dependency to use it (and shouldn't hesitate to do so).
If you want to avoid using a library (or any external code) you will need to draw the FAB yourself (using a Shape Drawable).
Bottom line, using the Design Compat lib is the preferred method for supporting a FAB, and you should use it. The Design Compat lib IS the "pure/Material API21 stuff".
There is NOT a FAB implementation implemented for any API other then the one in the Design support lib. You have to include the lib, or implement the code entirely yourself.
The Design lib has a dependency on AppCompat, so if you are planning on using the native fab, you will also need to include the dependency for AppCompat.
From the official blog post: "Note that as the Design library depends on the Support v4 and AppCompat Support Libraries, those will be included automatically when you add the Design library dependency."
The Material Design library has a dependency with AppCompat.
Check the #Booger answer. It reports the official blog.
How to use a FAB.
Add the dependency to your build.gradle:
compile 'com.android.support:design:22.2.0'
Just add the 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" />
Currently there are some bugs.
You have to define app:borderWidth="0dp" to display the shadow with API21+.
Also you have to define different margins for API 21+ and older devices.
res/values/dimens.xml
<dimen name="fab_margin_right">0dp</dimen>
<dimen name="fab_margin_bottom">0dp</dimen>
res/values-v21/dimens.xml
<dimen name="fab_margin_right">16dp</dimen>
<dimen name="fab_margin_bottom">16dp</dimen>
FAB uses the accent color and you could override with app:backgroundTint attribute.
Finally you can set a ClickListener with:
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
Actually I was having the same problem. The solution was quite simple:
you have to define app:rippleColor="#color/colorPrimary"
and you can use a FAB without using Appcompat Themes and Activities. Of course you have to include compile 'com.android.support:support-v4:25.3.1' and compile 'com.android.support:design:25.3.1'
but you DO NOT have 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" />
There is also several popular libraries on GitHub like:
FloatingActionButton which can expand/collapse as menu to reveal multiple actions.
I would say that although you intend to make your app available on API21 only but I encourage you to implement your application using Android Support Library AppCompat-v7 anyway. There are a lot of benefits you will get.
Anyway it is your choice. Just my suggestion. But if you are convinced. Or check out this full tutorial about how to use Android Design Support Library. http://inthecheesefactory.com/blog/android-design-support-library-codelab/en
Hope this help
Related
I'm trying to get run the Extended FAB for my activity, see Screenshot below. It just does not want to run. In my activity xml I use it as follows:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:layout_constraintBottom_toTopOf="#+id/create_ad_view"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintTop_toBottomOf="#+id/create_title_et"
app:icon="#drawable/ic_check"
style="#style/Widget.MaterialComponents.ExtendedFloatingActionButton.Icon"/>
By the way, I used different sources and tutorials, even the official web site of Material Design with some details of using Extended FAB. This is my dependency:
implementation 'com.google.android.material:material:1.0.0'
The ExtendedFloatingActionButton was released with the 1.1.0-alpha04.
Use
implementation 'com.google.android.material:material:1.1.0'
When following this Android tutorial using AppCompat V7 to get the new Tool Bar work, the android.support.v7.widget.Toolbar supports android:elevation on SDK's < Lollipop. This is because they use android.support.v7.widget.Toolbar and not Toolbar.
Is there equivalent for button? Something like android.support.v7.widget.Button? (This one not exist) Or any other workaround in the kind of overlaying the Button in some view supporting elevation? (I don't want implementation of creating custom shapes with gradients for look & feel of elevation).
Thanks,
You can use the Floating Action Button from support design library .
Use below code to the layout where you want this button.
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_fab"
android:layout_gravity="bottom|end"
app:backgroundTint="#0091b4"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
android:layout_margin="10dp"
android:scaleType="centerCrop" />
Do add design dependency in your project
Add below in the dependency list of build.gradle file
compile 'com.android.support:design:23.0.1'
Unfortunately you will have to do what you do not want. Because there is no support button view yet. (As i know :) perhaps some omniscient person will correct my answer)
Is there any standard tools or existing libraries to create a circle button like on Gmail App?
In the android support design support library one of the widgets provided is the Floating Action Button, which is usually added in the bottom right corner(but need not be used only there)
A floating action button is a round button denoting a primary action
on your interface. The Design library’s FloatingActionButton gives you
a single consistent implementation, by default colored using the
colorAccent from your theme.
To get the android design support library in your build.gradle file, add this:
compile 'com.android.support:design:22.2.0'
In your layout file"
<RelativeLayout
...
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.FloatingActionButton
android:id="#+id/myFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_discuss"
android:layout_margin="10dp"
android:clickable="true"/>
</RelativeLayout>
Details of android design support library here
Also there are many other libraries providing the FAB like
https://github.com/makovkastar/FloatingActionButton
https://github.com/futuresimple/android-floating-action-button
1.First important thing use Android Studio
2.Add as a dependency to your build.gradle:
dependencies {
compile 'com.melnykov:floatingactionbutton:1.3.0'
}
3. Create a layout
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="#drawable/ic_action_content_new"
fab:fab_colorNormal="#color/primary"
fab:fab_colorPressed="#color/primary_pressed"
fab:fab_colorRipple="#color/ripple" />
4.Add the namespace
xmlns:fab="http://schemas.android.com/apk/res-auto"
to your layout file.
You can also Set the button type (normal or mini) via the fab_type xml attribute (default is normal):
fab:fab_type="mini"
or
fab.setType(FloatingActionButton.TYPE_MINI);
Yes, with the new Design Support Library a default implementation of the floating action button is provided here.
In order to have access to the new FAB class you will need to add this dependency to your gradle build file.
compile 'com.android.support:design:22.2.0'
The title is pretty self explaining.
The following code does not render shadow below the Floating Action Button. What can be done to render shadow? Is this feature really not supported even on API 21+?
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/ic_add"
android:clickable="true" />
Note: Adding android:elevation does not add shadow on API 21.
Screenshot taken from the example by dandar3:
https://github.com/dandar3/android-support-design
Simply setting app:borderWidth="0dp" resolve this issues for me.
Note: don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto" to your root layout.
This issue should be fixed in next release of android design library.
For API 21+ you need to set app:borderWidth="0dp" and app:elevation="[number]dp". Setting elevation you are giving the size of shadow that you want:
Here is an example of code for API 21+:
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/locate_user_FAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/location_off"
app:elevation="6dp"
app:borderWidth="0dp"
android:layout_above="#+id/take_taxi_FAB"
app:backgroundTint="#color/colorAccentGrey">
One important thing to remember for APIs below to 21 (Android 4), is for terms of compatibility FAB will put a margins around your button to draw the shadow. Then you should do something like that (I'm currently using this code and works):
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/locate_user_FAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/location_off"
app:elevation="6dp"
app:borderWidth="0dp"
android:layout_above="#+id/take_taxi_FAB"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/map_FAB_marginRight"
android:layout_marginBottom="#dimen/locate_user_FAB_marginBottom"
app:backgroundTint="#color/colorAccentGrey">
I prefer to put xmlns:app="http://schemas.android.com/apk/res-auto" at the beginning of the XML, but I put there just to remind you ;]
I was experiencing this same issue and I got it to work by deleting this tag from my AndroidManifest.xml.
android:hardwareAccelerated="false"
I initially added it, together with android:largeHeap="true", because I thought I needed it for a HeatMap in which a large number of points where shown, but then I realized it could work just with android:largeHeap="true".
If this still isn't working for some, there is a significant difference between:
app:elevation="6dp"
app:borderWidth="0dp"
and
app:borderWidth="0dp"
app:elevation="6dp"
Order seems to matter for some reason (The first order works, second doesn't) and this is from the support library 23.3.0
Check manifest in project or libraries in Application tag and delete them
android:hardwareAccelerated="false"
android:largeHeap="true"
But if you need these options then shadows and transformation animations will not work
In case it help, I was using
android:tint="#color/myColor"
instead of
android:backgroundTint="#color/myColor".
Replacing tint by backgroundTint brought back the shadow.
Is there a way I can Achieve the floating button to work on 4.0 and later android??
I've seen it on google plus but I haven't found any tutorial. Only for android l preview. What did google+ use to achieve it?
You can now use the FloatingActionButton from the support-design library. Add this dependency to your gradle build file:
compile 'com.android.support:design:22.2.0'
And then add the FloatingActionButton to your layout file:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_done" />
Example project from Chris Banes: https://github.com/chrisbanes/cheesesquare.
I created an open-source library called FloatingActionButton. It's a Google+ like floating action button which reacts on the list view scrolling events. Becomes visible when the list view is scrolled up and invisible when scrolled down. API level 14+.
Here is one aditional free Floating Action Button library for Android
It has many customizations and requires SDK version 9 and higher
Full Demo Video
dependencies {
compile 'com.scalified:fab:1.1.2'
}
Original post is here.
You can use this library, it works well from the Android 2.1, API level 7.
It's so easy to include it in your project:
build.gradle
dependencies {
compile 'com.shamanland:fab:0.0.6'
}
layout.xml
<com.shamanland.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image"
/>
You can use this demo FloatingView. This demo work from API 11.
Here put all the parts necessary to implement by code. FloatingView by steps
Try this library, https://github.com/navasmdc/MaterialDesignLibrary
It can be easily to adapt to your project using Android Studio