0

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);
}
Máté Széll
  • 134
  • 2
  • 10
  • Just create another drawable file with desired colour in your `drawable` folder and then `setBackgroundDrawable` from your code – Reaz Murshed May 12 '16 at 19:34
  • I want to change the color from my program, so xml is not an option. – Máté Széll May 12 '16 at 19:35
  • set the color filter to `null` – pskink May 12 '16 at 19:37
  • Is there any other option to change the color except color filter? Because if i set it to null, it won't affect the button's color. – Máté Széll May 12 '16 at 19:41
  • Tried it right now, and setting the color filter to null makes the button invisible. – Máté Széll May 12 '16 at 19:43
  • it just clears the color filter previously set, it cannot make it invisible: `Parameters colorFilter The color filter to apply, or null to remove the existing color filter ` – pskink May 12 '16 at 19:45
  • OK. I found where I was wrong. The thing is, that even if i don't do anything to the drawable, that I set as background for the button, the ripple effect gets pink, as long as I create the button from java. – Máté Széll May 12 '16 at 19:57
  • 1
    Check this one: [How to use RippleDrawable programmatically in code (not xml) with Android 5.0 Lollipop?](http://stackoverflow.com/questions/27787870/how-to-use-rippledrawable-programmatically-in-code-not-xml-with-android-5-0-lo) – Seishin May 12 '16 at 21:00

0 Answers0