FAB 2 coloured icon - android

I am using a FAB widget and I want to add a 2 coloured icon in it. Is that possible? This is my current code right now:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/notificationFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:tintMode="multiply"
android:backgroundTint="#color/colorWhite"
android:layout_marginTop="5dp"
app:tint="#color/colorPrimaryLight"
app:rippleColor="#color/colorPrimary"
android:layout_marginHorizontal="10dp"
android:src="#drawable/active_notif_final"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:borderWidth="0dp"
/>
I did a workaround: made the colors of the icon light and added tintMode as multiply to get the original color. But it still doesn't look good.
workaround
Original
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/notificationFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorWhite"
android:layout_marginTop="5dp"
app:rippleColor="#color/colorPrimary"
android:layout_marginHorizontal="10dp"
app:icon="#drawable/active_notif_final"
app:iconTint="#color/colorPrimaryLight"
app:iconTintMode="multiply"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:borderWidth="0dp"
/>
with extended FAB:

By default, the icon (app:srcCompat) is tinted with the app:tint color as per the FloatingActionButton documentation.
You can disable this behavior by adding app:tint="#null" to your fab:
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:srcCompat="#drawable/..."
app:tint="#null"
.../>
In the ExtendedFloatingActionButton the app:icon is tinted with the app:iconTint:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
app:icon="#drawable/...."
app:iconTint="#null"
.../>

Related

how to change the color of vector asset in FloatingActionButton?

this is my FloatingActionButton
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:focusable="true"
android:src="#drawable/ic_camera_24"
android:tint="#color/green"
app:fabSize="mini"
app:rippleColor="#color/green" />
i tried to change color in layout ic_camera_24.xml but it still not work, how can i fix it?
have a nice day, everyone!
try this with app:srcCompat and app:tint:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:focusable="true"
app:srcCompat="#drawable/ic_camera_24"
app:tint="#color/green"
app:fabSize="mini"
app:rippleColor="#color/green" />
src: https://material.io/components/buttons-floating-action-button/android#mini-fabs

Adding elevation (shadow) to ExtendedFloatingActionButton

I'm trying to add elevation (shadow) to a com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton and it just doesn't apply
I've tried so far adding:
app:elevation="8dp"
app:borderWidth="0dp"
Even on different order as someone mentioned it may make a difference
Removed the android:background and some other attributes just to see if it collides with the elevation but no luck.
The complete definition currently looks like this:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/save_reminder_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:text="#string/save"
android:textAllCaps="true"
android:textColor="#color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingRight="20dp"
android:paddingEnd="20dp"
app:elevation="8dp"
app:borderWidth="0dp"
app:icon="#drawable/ic_check_white_18dp"
app:backgroundTint="#color/colorPrimary"
app:layout_anchorGravity="bottom|end" />
The attribute to set the elevation is app:elevation and the default value is 6dp
Use:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
app:elevation="8dp"
Check your material components library and use the latest stable version (currently 1.2.0).
use:
android:stateListAnimator="#null"
and
android:elevation="2dp"

How to remove grey shadow color below FloatingActionButton

I am getting light grey color below the FloatingActionButton as shown in image. Any one know how to remove it. I tested it on Lollipop version.
My xml code
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_add_black"
app:borderWidth="0dp"
app:fabSize="mini"
app:rippleColor="#color/colorPrimaryDark" />
Try set elevation:
app:elevation="0dp"
It happens when you set an elevation and the fab element has not space enough to display the "shadow".
Then you can set the elevation as 0dp as suggested above, or set a margin like
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_build_white_36dp"
android:id="#+id/build"
app:elevation="4dp"
android:clickable="true"
android:layout_margin="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
In this way the "elevation-effect" will appear correctly

Set FAB icon color

current FAB
I would like to know how to change the icon color of the FAB (Floating Action Button) widget supplied by the 'com.android.support:design:22.2.0' library from green to white.
style.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/color_primary</item>
<item name="colorPrimaryDark">#color/color_primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
<color name="color_primary">#00B33C</color>
<color name="color_primary_dark">#006622</color>
<color name="accent">#FFB366</color>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include android:id="#+id/toolbar" layout="#layout/toolbar" />
<TextView android:id="#+id/text"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:paddingTop="16dp"
android:textSize="20sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="16dp" />
</LinearLayout>
<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:src="#android:drawable/ic_input_add"
android:layout_margin="24dp"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
app:borderWidth="0dp" />
UPDATE 2
If you are using com.google.android.material.floatingactionbutton.FloatingActionButton, use app:tint
app:tint="#android:color/white"
UPDATE
Refer to the answer of #Saleem Khan which is the standard way to set the app:tint using:
android:tint="#android:color/white"
via XML on FloatingActionButton.
OLD (June 2015)
This answer was written before October 2015, when android:tint on FloatingActionButton was supported only with API >= 21.
You can change it programmatically using ColorFilter.
//get the drawable
Drawable myFabSrc = getResources().getDrawable(android.R.drawable.ic_input_add);
//copy it in a new one
Drawable willBeWhite = myFabSrc.getConstantState().newDrawable();
//set the color filter, you can use also Mode.SRC_ATOP
willBeWhite.mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
//set it to your fab button initialized before
myFabName.setImageDrawable(willBeWhite);
Using android:tint property you can set the color like this
<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:tint="#android:color/white"
android:src="#android:drawable/ic_input_add"
/>
If you are using Material Components
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_gravity="bottom|end"
app:fabSize="normal"
app:tint="#color/colorAccent"
app:srcCompat="#drawable/ic_google"/>
If you want to use icon default color, change app:tint="#null"
You have to change app:tint for that to work. android:tint didn't do any change for me.
It's easier than get the drawables, you only need to access to the color filter and set it to the color that you want.
FloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.myfabid);
myFab.setColorFilter(Color.WHITE);
Since FloatingActionButton extends ImageView we can use ImageViewCompat to tint the icon:
ImageViewCompat.setImageTintList(
floatingActionButton,
ColorStateList.valueOf(Color.WHITE)
);
Use the white version of ic_add from the google design site.
android:tint looks like a clean solution but it is not supported below API level 21
Using a bitmap adds less complexity to your app than attempting to change the color of an existing icon programmatically. Less complexity means fewer things to test :)
Try this code
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:backgroundTint="#color/sm_blue"
app:tint="#color/white"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_input_add" />
Result
If you are using material FAB use app:tint to change the color of the icon instead of android:tint
If you want to change the color of the icon in CollapsingToolbarLayout use the following code
app:tint="#color/white"
Use the app instead of Android
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_loan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/medium"
android:layout_marginBottom="16dp"
android:backgroundTint="#color/ci_blue"
android:theme="#style/fabtheme"
app:srcCompat="#drawable/ic_baseline_add_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:rippleColor="#color/white" />
theme
<style name="fabtheme" parent="Widget.Design.FloatingActionButton">
<item name="colorOnSecondary">#color/white</item>
</style>
or
use the attribute
app:tint="#color/white"
For API >= 21
My Solution to change FloatingActionButton icon color programmatically
val fab = FloatingActionButton(requireContext())
fab.apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
imageTintList = ColorStateList.valueOf(Color.WHITE)
}
You can make your custom style:
<style name="FloatingButton" parent="Widget.MaterialComponents.FloatingActionButton">
<item name="colorSecondary">#color/red</item>
<item name="colorOnSecondary">#color/white</item>
</style>
Where colorSecondary is the background and colorOnSecondary is the color of the drawable of the button.
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_phone"
android:theme="#style/FloatingButton" />
If you are using com.google.android.material.floatingactionbutton.FloatingActionButton
Then
To change Background Color of Floating Action Button, use app:backgroundTint
To change Floating Action Button's Icon's color, use app:tint
To change Floating Action Button's Icon Drawable, use app:srcCompat
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#color/white"
app:srcCompat="#drawable/fb_icon"
app:tint="#android:color/black" />
If you are using material FAB, you can style it programmatically using the below code in Kotlin.
fab.supportImageTintList = ContextCompat.getColorStateList(context, R.color.fab_icon_tint)
If you use Extended, set app:iconTint you can do like this:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/fAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:icon="#drawable/d0"
app:iconTint="#color/white"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.Material3.FloatingActionButton"
tools:ignore="SpeakableTextPresentCheck" />
This will keep the original color of the icon.
app:tint="#null"
or
android:tint="#null"
In Java
private FloatingActionButton login;
login = findViewById(R.id.loginbtn);
login.setColorFilter(android.R.color.white);
In XML
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/loginbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimaryDark"
android:src="#drawable/loginicon"
app:rippleColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/pass_l" />

FloatingActionButton shadow not being drawn properly

I am using FloatingActionButton - http://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
Randomly I see that the shadow is not being drawn properly - it comes out in solid grey as opposed to a gradient.
Here is my markup:
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="24dp"
android:layout_marginRight="24dp"
app:borderWidth="0dp"
android:id="#+id/fabButton"
android:src="#drawable/search"
android:clickable="true" />
Has anybody else faced this?
I think, you forgot these attributes:
...
app:elevation="6dp"
app:pressedTranslationZ="12dp"
...
See this answer for more information about FloatingActionButton design guides

Categories

Resources