Bottom navigation bar - rectangle action button - android

Using google.android.material:
Is it possible to make action button rectangle ?
Is it possible to move it a little higher / lower ?
I am talking about the middle circle button.

You can use the app:shapeAppearanceOverlay attribute to achieve a square button and the app:fabCradleVerticalOffset attribute to change the distance of the FAB to the BottomAppBar.
Something like:
<com.google.android.material.bottomappbar.BottomAppBar
app:fabCradleVerticalOffset="16dp"
app:fabCradleRoundedCornerRadius="0dp"
app:fabCradleMargin="0dp"
..>
<com.google.android.material.floatingactionbutton.FloatingActionButton
app:shapeAppearanceOverlay="#style/SquareFloatingShapeOVerlay"
../>
with:
<style name="SquareFloatingShapeOVerlay" parent="">
<item name="cornerSize">0dp</item>
</style>
If you want a rectangular shape you can use a ExtendedFloatingActionButton instead of the FloatingActionButton.
Something like:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
app:layout_anchor="#id/bottom_app_bar"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlayExtended"
../>
with:
<style name="ShapeAppearanceOverlayExtended" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">0dp</item>
</style>
Note: it requires the version 1.1.0 of the Material Components Library.

Try this: If you are using FloatingActionButton
app:borderWidth="0dp"
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add"
android:layout_marginRight="20dp"
app:fabSize="normal"
android:elevation="#dimen/fab_elevation"
android:background="#000000"
app:borderWidth="0dp"
android:stateListAnimator="#animator/fab_anim"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
Hope this will help !

To change the shape of the Floating Action Button, you can create a separate resource file in the drawable folder for eg: shape.xml as-
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorAccent" />
and then in Floating Action Button add the following property:
android:background="#drawable/shape"
Also, you can use margin to move it higher.

Use elevation attribute to FAB
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/nav_items" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="40dp"
android:clickable="true"
app:elevation="8dp"
app:srcCompat="#android:drawable/ic_input_add" />

Related

Theming PreferenceFragmentCompat

I'm having some troubles setting a theme to PreferenceFragmentCompat trying to achieve this:
I have these styles set:
<style name="AppThemeBase" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:statusBarColor">#color/status_bar</item>
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="preferenceTheme">#style/AppThemeBase.RecordingPreferencesTheme</item>
</style>
<style name="AppThemeBase.RecordingPreferencesTheme" parent="#style/PreferenceThemeOverlay">
<item name="android:textColorPrimary">#FD0000</item>
<item name="android:textColorSecondary">#1F6F2B</item>
</style>
With this options I can see what I want to achieve in Android Studio layout preview but when it does not have effect when I execute it
The result when I execute:
The only thing it worked so far is using <item name="android:textColor">#FD0000</item> that will change the tittle colour but I can't find a way to change the summary colour.
Another question I have is if there is a way to set the theme just for that preference fragment instead of using it in all preferences fragments. Using android:theme at the root of the fragment layout isn't working for me :(
Update:
As per #Susan question this is how I try to use android:theme at the root of the fragment layout I've created this theme based in the main one
<style name="RecordingPreferenceTheme" parent="AppThemeBase">
<item name="preferenceTheme">#style/AppThemeBase.RecordingPreferencesTheme</item>
</style>
And this is the preference layout where I have a fragment which will inflate the PreferenceFragmentCompat
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/RecordingPreferenceTheme">
<include
android:id="#+id/include"
layout="#layout/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/fragment_container_settings"
android:name="com.example.app.RecordSettingsFragment$RecordPreferences"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/include" />
</androidx.constraintlayout.widget.ConstraintLayout>
And at the root of the layout I'll set android:theme="#style/RecordingPreferenceTheme" but it doesn't work, just changes in the main theme set in Activity will have some effect. I've already tried customize PreferenceFragmentCompat layout with same results.
Cheers!!
See this sample project here created for your specific case the the only problem for this solution is you have to create your preference from code cant use xml file to inflate and
for those who just want the gist of this solution is that use setLayoutResource() method to set layout and use custom layout i just copied the original layout and hardcoded colors according to #Bhuntupana need
val messagesCategory = PreferenceCategory(context).apply {
layoutResource = R.layout.sample_preference
key = "messages_category"
title = getString(R.string.messages_header)
screen.addPreference(this)
}
Update
use layout tag to specify layout resource to use like this
<EditTextPreference
android:layout="#layout/sample_preference"
app:key="signature"
app:title="#string/signature_title"
app:useSimpleSummaryProvider="true" />
sample_preference.xml just copy of default layout preference_material.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:baselineAligned="false"
android:clipToPadding="false"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingRight="?android:attr/listPreferredItemPaddingRight">
<include layout="#layout/image_frame" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<TextView
android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="#FD0000" />
<TextView
android:id="#android:id/summary"
style="#style/PreferenceSummaryTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:layout_gravity="start"
android:maxLines="10"
android:textAlignment="viewStart"
android:textColor="#1F6F2B" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end|center_vertical"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:paddingEnd="0dp"
android:paddingRight="0dp" />
</LinearLayout>

ExtendedFloatingActionButton - how to achieve same height when button is extended as when it's shrank

I'm using latest material components - 1.1.0-beta01.
When ExtendedFloatingActionButton expands, its height also reduces.
Here is how xml layout looks:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Text"
android:textColor="#02220D"
android:backgroundTint="#F2C203"
style="#style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:textSize="15sp"
android:fontFamily="sans-serif-medium"
android:textStyle="normal"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:iconTint="#02220D"
app:icon="#drawable/ic_camera"
app:iconPadding="10dp"
android:insetLeft="0dp"
android:insetBottom="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
I tried to set min and max height, but it didn't work. Any idea how to achieve the same height when button is expanded and shrank?
Thanks.
The ExtendedFloatingActionButton is a MaterialButton.
You can use the android:minHeight attribute:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:minHeight="56dp"
../>
Otherwise you can define a custom style:
<style name="CustomExtendedFloating" parent="Widget.MaterialComponents.ExtendedFloatingActionButton.Icon">
<item name="android:minHeight">56dp</item>
</style>
Try setting app:collapsedSize property with your required size
app:collapsedSize="56dp"
Instead of hardcoded value use the one provided by the material library :
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:minHeight="#dimen/design_fab_size_normal"
It's worth noting, according to the spec for FAB and Extended FAB, in the extended state, it's not as tall. https://material.io/components/buttons-floating-action-button/#specs

How to set border and background color of Floating Action Button with transparency through xml?

I have tried this solution: https://stackoverflow.com/a/54074154/10299002
which works great as long as the FAB is over a dark background. However, it seems to break when it passes over a white background. What might be the issue?
Here is my FAB:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:theme = "?appAccentStyle"
android:id="#+id/button_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_margin="16dp"
app:backgroundTint="?colorFloatingActionButtonBorder"
android:src="#drawable/ic_arrow_downward_24dp"
android:tint="?colorAccent"
android:backgroundTint="?colorFloatingActionButtonBackground"
tools:visibility="visible"
android:layout_gravity="end|bottom" />
This is the style being applied:
<style name="LimeColorAccent">
<item name="colorAccent">#color/Lime</item>
<item name="appAccentStyle">#style/LimeColorAccent</item>
<item name="colorFloatingActionButtonBorder">#color/LimeTranslucentBorder</item>
<item name="colorFloatingActionButtonBackground">#color/LimeTranslucent</item>
</style>
And here are the colors used:
<color name="Lime">#FFCDDC39</color>
<color name="LimeTranslucentBorder">#4cCDDC39</color>
<color name="LimeTranslucent">#26CDDC39</color>
Output:
Edit:
Tried this solution: https://stackoverflow.com/a/57307389/10299002
My fab.xml:
(Same as in Solution)
My FloatingActionButton:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/button_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_arrow_downward_24dp"
android:background="#drawable/fab"
android:layout_margin="16dp"
android:layout_gravity="end|bottom"
app:fabSize="normal"
app:backgroundTint="#55990000"
app:borderWidth="0dp"
app:elevation="2dp"
tools:visibility="visible" />
Output: (Inner Circle Still Broken)
I used you code and directly used color. and found two result with black background and white background .
and the code:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/button_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_margin="16dp"
app:backgroundTint="#4cCDDC39"
android:src="#drawable/ic_action_profile"
android:tint="#color/colorAccent"
app:borderWidth="2dp"
android:backgroundTint="#26CDDC39"
tools:visibility="visible"
android:layout_gravity="end|bottom" />
You should create your own FAB style extending com.google.android.material.floatingactionbutton.FloatingActionButton as parent, to properly custom your button.
Check out this answer, it should help you. The idea is to create your own border.
FloatingActionButton only provides app:borderWidth to play with border thickness.

Background drawable does not fill floating action button

I have FAB and trying to fill a drawable(round_drawable.xml) to my entire fab but the drawable appears currently as in the top left corner as shown here,
I tried a solution from Setting src and background for FloatingActionButton but it did not work, added most attributes from other sources but neither scaleType, src, background did not work.
How do I get to fill the FAB with the round_drawable ?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="100dp"
android:layout_height="95dp"
android:scaleType="fitXY"
app:backgroundTint="#color/white"
android:src="#drawable/round_drawable"
android:backgroundTintMode="src_atop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
round_drawable.xml
#drawable/right is a png image of right arrow.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadius="50dp"
android:shape="oval">
<corners android:radius="10dp" />
<gradient
android:angle="45"
android:endColor="#D425B5"
android:startColor="#EBF928" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<bitmap
android:gravity="center"
android:src="#drawable/right" />
</item>
</layer-list>
Just add this line in you dimen.xml file
<dimen name="design_fab_image_size" tools:override="true">56dp</dimen>
NOTE : 56dp is default size of FAB buttom
Default - 56dp
mini - 40dp
custom - whatever you have specified in fabCustomSize
TIP : use app:fabCustomSize rather giving custom height width to FAB button.
Example
in your layout
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#color/white"
<!--inmprtent-->
app:fabCustomSize="100dp"
android:src="#drawable/round_drawable"
android:backgroundTintMode="src_atop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
now create dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="design_fab_image_size" tools:override="true">100dp</dimen>
</resources>
keep your round_drawable.xml as it is and you are good to go
Result
cheers..
You don't have to give specific size to FAB.And it won't require background drawable also for any specific color. It has attribute fabSize in which you can pass predefined sizes like mini(40dp), normal(56dp). And still if you want to add size remove sizes from drawable <item> tag. That tag actually making your drawable small.
check this sample code for creating Fab with image in center
<android.support.design.widget.FloatingActionButton
android:id="#+id/fbtn_addNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="#dimen/margin_16"
android:layout_marginRight="#dimen/margin_16"
app:srcCompat="#drawable/ic_add_white_28dp"
app:backgroundTint="#color/colorPrimary"
app:borderWidth="#dimen/margin_0"
app:elevation="#dimen/margin_8"
app:fabSize="normal"
android:visibility="visible"
app:pressedTranslationZ="#dimen/margin_5" />
It will look like this. Hope it will help...
Updated answer of 2019 with androidX:
Dependency : implementation 'com.getbase:floatingactionbutton:1.10.1'
XML:
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="#+id/multiple_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="#dimen/fab_margin"
android:layout_marginBottom="20dp"
app:fab_addButtonColorNormal="#color/colorPrimaryDark"
app:fab_addButtonColorPressed="#color/colorPrimaryDark"
app:fab_colorPressed="#color/colorPrimaryDark"
app:fab_labelStyle="#style/menu_labels_style">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_langauge_translation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_language_icon"
app:fab_size="normal"
android:paddingBottom="15dp"
app:fab_colorNormal="#color/colorPrimaryDark"
app:fab_colorPressed="#color/colorPrimaryDark"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_facebook_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_fb_icon"
app:fab_size="normal"
android:paddingBottom="15dp"
app:fab_colorNormal="#0184FF"
app:fab_colorPressed="#0184FF"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_whats_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_whatsapp_icon"
app:fab_colorNormal="#4EC248"
android:paddingBottom="15dp"
app:fab_size="normal"
app:fab_colorPressed="#4EC248"
app:fab_title="" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/btn_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_email_icon"
app:fab_colorNormal="#848484"
android:paddingBottom="15dp"
app:fab_colorPressed="#848484"
app:fab_size="normal"
app:fab_title="" />
</com.getbase.floatingactionbutton.FloatingActionsMenu>
Output:
I used this code and it worked:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_add"
android:scaleType="center"
android:backgroundTint="#android:color/transparent"
android:outlineAmbientShadowColor="#android:color/transparent"
android:outlineSpotShadowColor="#android:color/transparent"
/>
and in the dimens.xml file:
<dimen name="design_fab_image_size" tools:override="true">40dp</dimen>
Just add this line in you style.xml file
<style name="FullImageFloatingButton" parent="Widget.Design.FloatingActionButton">
<item name="fabSize">normal</item>
<item name="maxImageSize">56dp</item>
</style>
<style name="FullImageMiniFloatingButton" parent="Widget.Design.FloatingActionButton">
<item name="fabSize">mini</item>
<item name="maxImageSize">40dp</item>
</style>
Use FullImageFloatingButton for normal FloatingButton and FullImageMiniFloatingButton for mini FloatingButton.

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" />

Categories

Resources