Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting src and background for FloatingActionButton

When I use background and src in android.support.design.FloatingActionbutton it is not set correctly. Instead it is displayed as

enter image description here

<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

enter image description here

<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?

like image 591
Harish Avatar asked Aug 16 '15 15:08

Harish


People also ask

How do I change the background color on FloatingActionButton?

To change background color of Floating Action Button in Kotlin Android, set the backgroundTint attribute (in layout file) or backgroundTintList property (in Kotlin file) of FAB with the required color.

What is a floating action button?

A floating action button (FAB) is a circular button that triggers the primary action in your app's UI. This page shows you how to add the FAB to your layout, customize some of its appearance, and respond to button taps.

What is floating action button in flutter?

The Floating Action Button is the most unique type of button widget provided in flutter. It is a widget that floats on the screen over other widgets. It appears as a circular icon on the screen with an icon in its center as its child. It is by default placed at the bottom-right corner of the screen.


1 Answers

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"

like image 147
Nick Cardoso Avatar answered Oct 02 '22 19:10

Nick Cardoso