I need to create some round shaped buttons, and I ended up creating a drawable to support the material design's ripple effect.
My problem is, that in the drawable file below, I have to set a default color for the button, and I can't change it later, at least I didn't find the way to do that.
I tried it with backgroundTint, but it didn't really changed anything. Then tried to created it from java code, and change the color with colorfilter, but the result was pretty weird. The ripple effect became pink-ish.
What is the proper way to create round buttons with ripple effects?
drawable/button_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FFFFFF"/>
</shape>
</item>
</ripple>
Trying it with backgroundTint:
<Button
android:id="@+id/button"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/button_circle"
android:backgroundTint="@color/colorAccent"/>
Doing it from code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_colors, container, false);
RelativeLayout main = (RelativeLayout) v.findViewById(R.id.main_layout);
Button b = new Button(getContext());
b.setLayoutParams(new RelativeLayout.LayoutParams(dpToPixels(150), dpToPixels(150)));
Drawable background = getResources().getDrawable(R.drawable.button_circle);
background.setColorFilter(Color.parseColor("#4CAF50"), PorterDuff.Mode.DARKEN);
b.setBackground(background);
main.addView(b);
return v;
}
private int dpToPixels(int dp) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}