Skip to content Skip to sidebar Skip to footer

Recreate Shape As In Xml File By Using Code And Set Width Programmatically

I already have shape code and now I need to draw the same programmatically and set its width according to the length of texts in an array. I am not able to reproduce the same using

Solution 1:

Method 1: By Creating custom shape:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.shapes.Shape;

publicclassRectangleCutCornerextendsShape {
    privateintbackgroundColor= Color.BLACK;
    privatefloatstrokeWidth=1.0f;
    privatestaticfinalfloatCORNER=35.0f;

    privatefinalPaintborder=newPaint();
    privatefinal Path  path;

    publicRectangleCutCorner() {
        path = newPath();
        border.setColor(backgroundColor);
        border.setStyle(Paint.Style.FILL);
        border.setStrokeWidth(strokeWidth);
        border.setAntiAlias(true);
        border.setDither(true);
        border.setStrokeJoin(Paint.Join.ROUND);
        border.setStrokeCap(Paint.Cap.ROUND);
    }

    //for setting stroke width programmaticallypublicvoidsetStrokeWidthToLayout(float strokeWidth){
        this.strokeWidth = strokeWidth;
        border.setStrokeWidth(strokeWidth);
    }

    //for setting background/stroke color programmaticallypublicvoidsetBackgroundColor(int backgroundColor){
        this.backgroundColor = backgroundColor;
        border.setColor(backgroundColor);
    }

    //for setting background filled or not programmaticallypublicvoidsetBackgroundFill(boolean isFilled){
      if(isFilled){
          border.setStyle(Paint.Style.FILL);
      }else{
          border.setStyle(Paint.Style.STROKE);
      }
    }

    @OverrideprotectedvoidonResize(float width, float height) {
        super.onResize(width, height);

        floatdx= strokeWidth/2.0f;
        floatdy= strokeWidth/2.0f;
        floatx= dx;
        floaty= dy;
        floatw= width  - dx;
        floath= height - dy;
        path.reset();
        path.moveTo(x + CORNER,y);
        path.lineTo(w - CORNER,y);
        path.lineTo(w,y + CORNER);
        path.lineTo(w, h);
        path.lineTo(x + CORNER,h);
        path.lineTo(dx,h);
        path.lineTo(dx,y);
        path.close();
    }
    
    @Overridepublicvoiddraw(Canvas canvas, Paint paint) {
        // TODO Auto-generated method stub
        canvas.drawPath(path,border);
    }
}

How to use this programmatically:

    RectangleCutCorner rectangleCutCorner =new RectangleCutCorner();
    //set color
    rectangleCutCorner.setBackgroundColor(R.color.black);
    //set fill background (true/false)
    rectangleCutCorner.setBackgroundFill(false);
    //set stroke width
    rectangleCutCorner.setStrokeWidthToLayout(2.5f);
    //set background toview
    view.setBackground(new ShapeDrawable(rectangleCutCorner));

Output for above code is:

enter image description here

Method 2: By Using Material Shape Drawable:

Add this dependency into build.gradle(app) file:

implementation 'com.google.android.material:material:1.3.0'

How to use:

    ShapeAppearanceModel shapeAppearanceModel = newShapeAppearanceModel()
            .toBuilder()
            .setTopRightCorner(CornerFamily.CUT, 20)
            .build();
    MaterialShapeDrawable shapeDrawable = newMaterialShapeDrawable(shapeAppearanceModel);
    int[][] states = newint[][] {
            newint[] { android.R.attr.state_enabled},
    };
    int[] colors = newint[] {
            Color.WHITE,
    };
    ColorStateList myList = newColorStateList(states, colors);
    // set background to drawable
    shapeDrawable.setFillColor(myList);
    // set stroke and to view
    shapeDrawable.setStroke(1.5f, Color.BLACK);
    // set background to view
    ViewCompat.setBackground(tvUrl, shapeDrawable);

Output for above code is:

enter image description here

Post a Comment for "Recreate Shape As In Xml File By Using Code And Set Width Programmatically"