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.
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" />
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With