i have created and android project in my xml file there is a floating bar like below
<android.support.design.widget.FloatingActionButton
android:id="#+id/btn_cart"
android:src="#drawable/ic_shopping_cart_black_24dp"
android:backgroundTint="#android:color/white"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:layout_anchor="#id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:useCompatPadding="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
in my activity i have use like below
FloatingActionButton btnCart;
and in side on create
btnCart = (FloatingActionButton) findViewById(R.id.btn_cart);
when i go to that page app will crash I dont know what did I do wrong
As per my comment remove android:backgroundTint. You can change background color in following ways:
In XML with attribute app:backgroundTint
<android.support.design.widget.FloatingActionButton
android:id="#+id/btn_cart"
android:src="#drawable/ic_shopping_cart_black_24dp"
app:backgroundTint="#android:color/white"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:layout_anchor="#id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:useCompatPadding="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Or in java file
btnCart.setBackgroundTintList(ColorStateList.valueOf(your color in int));
I think the error will be in this line:
android:src="#drawable/ic_shopping_cart_black_24dp"
Make ur changes in the third line of code as:
app:srcCompat="#drawable/ic_shopping_cart_black_24dp"
Related
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
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"
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
When I use background and src in android.support.design.FloatingActionbutton it is not set correctly. Instead it is displayed as
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pink"
android:src="#drawable/ic_action_barcode_2"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp" />
but when I use ImageView it appears correctly as
<ImageView
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pink"
android:src="#drawable/ic_action_barcode_2"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp" />
why is FloatingActionButton is not displayed correctly? What should I change in my code?
Floating action button's background does not need to be changed, you just apply a tint and then add your icon as usual
<android.support.design.widget.FloatingActionButton
...
app:backgroundTint="#color/ic_action_barcode_2"
android:src="#drawable/ic_add" />
This provides you with a round button still but in the colour you desire.
In this case the app namespace is used for the support library features:
xmlns:app="http://schemas.android.com/apk/res-auto"
i faced the similar problem. I tried to set src or background of FloatingActionButton, but couldn't fill the button by src. following code solved problem for me.
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.
.
.
android:background="#drawable/round_icon"
android:backgroundTintMode="src_over">
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