Search code examples
androidandroid-drawableshapes

How can i make this shape in android using shape drawable


I want to make this shape in my android project. how can I make this shape using shape?

enter image description here


Solution

  • Create a Custom Drawable Class like this

    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.ColorFilter;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.PixelFormat;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    
    public class ArrowDrawable extends Drawable {
        private Rect mBounds;
        private Paint mDrawPaint;
    
        public ArrowDrawable() {
            mDrawPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
            mDrawPaint.setColor(Color.BLUE);
        }
    
        @Override
        public void draw(@NonNull Canvas canvas) {
            mBounds=getBounds();
            Path path=new Path();
            path.moveTo(mBounds.left,mBounds.bottom/2);
            path.lineTo(mBounds.right/8,mBounds.top);
            path.lineTo(mBounds.right,mBounds.top);
            path.lineTo(mBounds.right,mBounds.bottom);
            path.lineTo(mBounds.right/8,mBounds.bottom);
            path.lineTo(mBounds.left,mBounds.bottom/2);
            canvas.drawPath(path,mDrawPaint);
        }
    
        @Override
        public void setAlpha(int alpha) {
          mDrawPaint.setAlpha(alpha);
        }
    
        @Override
        public void setColorFilter(@Nullable ColorFilter colorFilter) {
          mDrawPaint.setColorFilter(colorFilter);
        }
    
        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    }
    

    Set the Drawable to the TextView

    ArrowDrawable arrowDrawable=new ArrowDrawable();
    textView.setBackground(arrowDrawable);