Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an image be used in FloatingActionButton?

I want to use an image as background of a FloatingActionButton.

I know that the default background color which is colorAccent and we can change it easily. But what about the image as its background ?

If I can then how?

Pictorial explanation will always be appreciated.

like image 548
Abhishek Kumar Avatar asked Jun 18 '16 18:06

Abhishek Kumar


2 Answers

Just use like this

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@android:color/transparent"
    android:src="@drawable/icon" />
like image 74
Ognev Zair Avatar answered Nov 16 '22 06:11

Ognev Zair


In xml you can set 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|right"
            android:layout_margin="16dp"
            android:src="@drawable/ic_add_black"
            app:borderWidth="0dp"
            app:fabSize="mini" />

and in java file you can set like this:

  ImageView iconFAB = new ImageView(this);
        iconFAB.setImageResource(R.drawable.ic_action_new);

        //set the appropriate background for the main floating action button along with its icon
        mFAB = new FloatingActionButton.Builder(this)
                .setContentView(iconFAB)
                .setBackgroundDrawable(R.drawable.selector_button_pink)
                .build();

here is selector_button_pink.xml file code

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_action_red_touch" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_action_red" android:state_pressed="false"></item>
</selector>
like image 24
Sagar Chavada Avatar answered Nov 16 '22 07:11

Sagar Chavada