Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Floating Button Icon is not centred

I'm trying to create a floating button in my app. The button is there, but the image in the button is a bit upwards (see image).

enter image description here

I can't figure out what's wrong with it. Below is part of the XML for floating button.

<android.support.design.widget.FloatingActionButton         android:id="@+id/buttonUp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginRight="16dp"         android:layout_marginTop="480dp"         android:clickable="true"         android:scaleType="center"         android:src="@drawable/btn_back_to_top_3x"         app:layout_anchor="@id/layout"         app:layout_anchorGravity="bottom|right|end"         app:backgroundTint="@android:color/background_light"         app:fabSize="normal" /> 

The button is showing where I wanted it. The only problem seems to be the image inside isn't center. Why is this happening?

EDIT: After some more looking around I realized the image itself was having problem where the image isn't actually centered and there are space at the bottom side (which cause the image to being push upward).

like image 447
rabbit87 Avatar asked Jan 05 '17 03:01

rabbit87


People also ask

How do you adjust a floating button?

Add the floating action button to your layoutThe size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.

How do I change the shape of the floating action button on Android?

To change the shape of the Floating action button: You can use the shape property of FloatingActionButton() widget class. Implement BeveledRectangleBorder( ) on this property. The output will be a square floating action button, increase the border-radius value to make a circular type shape.

Where is the floating action button?

A floating action button (FAB) performs the primary, or most common, action on a screen. It appears in front of all screen content, typically as a circular shape with an icon in its center.


2 Answers

I have faced the same problem. the following solution have worked for me

app:fabCustomSize="40dp" 
like image 179
Mehatab Avatar answered Oct 16 '22 16:10

Mehatab


You should set fabCustomSize value like FAB exactly size:

<android.support.design.widget.FloatingActionButton         android:layout_width="60dp"         android:layout_height="60dp"         app:fabCustomSize="60dp"         android:src="@drawable/plus_icon"         app:rippleColor="@color/colorPrimaryDark" /> 
like image 35
Hossein Yousefpour Avatar answered Oct 16 '22 16:10

Hossein Yousefpour