I
I
Igor2015-12-17 21:05:44
Android
Igor, 2015-12-17 21:05:44

How to make a rounded button in Android (but not rounded corners)?

How to make a rounded button in Android, not with rounded corners, but rounded, as in the screenshot below.
9961158aedee489f8c0eff8c4d8ac8f5.png
So that, regardless of the height of the content, the sides are rounded (not corners, but sides).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2015-12-17
@asdf999

I did it via GlobalLayoutListener:

private void addRoundedBackground(final TextView textView, final int color, final int borderColor) {
        textView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        } else {
                            textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }

                        final float height = textView.getHeight();

                        GradientDrawable shape =  new GradientDrawable();
                        shape.setCornerRadius(height/2);
                        shape.setColor(color);
                        shape.setStroke(1, borderColor);

                        textView.setBackgroundDrawable(shape);
                    }
                });
    }

L
lomikman, 2015-12-17
@lomikman

1) Alternatively, you can draw an oval with canvas ( stackoverflow.com/questions/5012685/draw-an-oval-s... and then overlap top and bottom with two background color views.
2) CardView play around with corner radius
3) change design and not to get tired (is it really necessary?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question