Reputation: 7023
Android Floating Action button is working fine upto OS version 5.0.1. But it is not working properly rather its becoming transparent above OS version 5.0.1. Does any one have encountered with such issue. I have to change background Tint list dynamically so defining only in xml is not use full. So how to handle it with OS above 5.0.1. Thank you in advance for your co-operation.
Changing dynamically TintList color
mFloatingActionButtonBack.setBackgroundTintList(changeColor(getResources().getColor(R.color.color_gray)));
Xml for Floating Action Button
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/contact_floating_btn"
app:fabSize="normal"
android:src="@drawable/contact_directions"
android:layout_alignParentRight="true"
android:clickable="true"
android:layout_below="@+id/gmap_frag"
android:layout_marginTop="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin_right"
/>
Style Part
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primary_dark</item>
<item name="colorAccent">@color/color_primary</item>
<item name="colorControlNormal">#d7d7d7</item>
</style>
Upvotes: 1
Views: 876
Reputation: 7023
What I found a solution to the problem I was facing is following.
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Fab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.color_gray)));
} else {
Fab.setBackgroundTintList(changeColor(getActivity().getResources().getColor(R.color.color_gray)));
}
and the method for ColorStateList
is
public ColorStateList changeColor(int color){
ColorStateList myColorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_active},
new int[]{android.R.attr.state_window_focused},
new int[]{android.R.attr.state_pressed}, //1
new int[]{android.R.attr.state_focused}, //2
new int[]{android.R.attr.state_focused, android.R.attr.state_pressed} //3
},
new int[]{
color,
color,
color, //1
color, //2
color//3
}
);
return myColorStateList;
}
Hope this will help someone else having same issue as I had
Upvotes: 1