0

I'm making a messenger application i made for every message a background with this shape:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
    android:bottomLeftRadius="80dp"
    android:bottomRightRadius="80dp"
    android:topRightRadius="80dp"
    android:topLeftRadius="80dp"
   />
<solid android:color="#fff"/>

But the problem is when the message is long the corners be big and this make my message layout is bad! i want to know if there's another method to make a rounded background!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ayech Hamza
  • 159
  • 2
  • 10
  • 1
    Can you add some graphic/ mock up image for expected layout and actual layout. – Sasuke Uchiha Jan 20 '18 at 17:49
  • Possible duplicate of [How to draw rounded rectangle in Android UI?](https://stackoverflow.com/questions/5618402/how-to-draw-rounded-rectangle-in-android-ui) – Gowthaman M Jan 20 '18 at 17:50

3 Answers3

0

You can use 9-patch drawables.

These are png files whose allow you to define areas that can't be stretched and areas that will stay same size regardless the view size.

You can read about it here: https://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

You can read about how to create it here: https://developer.android.com/studio/write/draw9patch.html

9-patch drawables are commonly used in Adnroid SDK Standard views.

0

You can create a custom button class which extend Button class and then override the onLayout() method may be like this... where basically you are setting the corner radius = height/2

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if(this.getBackground()!=null && this.getBackground() instanceof LayerDrawable) {
            LayerDrawable layerDrawable = (LayerDrawable)(this.getBackground().mutate());
            GradientDrawable gd1 = (GradientDrawable)layerDrawable.findDrawableByLayerId(R.id.layer_one);
            roundEdgeGradientDrawable(gd1, this.getHeight());
        }
    }

    public static GradientDrawable roundEdgeGradientDrawable(GradientDrawable bgShape, float height) {
        if (bgShape != null) {
            float radius = height / 2;
            float[] radii = {radius, radius, radius, radius, radius, radius, radius, radius};
            bgShape.setCornerRadii(radii);
        }
        return bgShape;
    }
Sasuke Uchiha
  • 890
  • 3
  • 15
  • 31
0
<?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android" >
      <solid android:color="@color/headerColor" />      
      <corners android:radius="5dp" />
</shape>
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35