androidgeometrydrawshapesandroid-shapedrawable

How to make a semicircular Hole in a rectangle side?


I need to make a rectangle which has a circle hole in middle something like this

enter image description here


Solution

  • There are different ways to obtain it.
    You can use the Material Components Library and the MaterialShapeDrawable to create custom shape path (you need the version 1.1.0)

    For example you can do something like:

    LinearLayout linearLayout= findViewById(R.id.linear_rounded);
    //Use the BottomAppBarTopEdgeTreatment to apply the bottom edge shape, or just create a custom class to obtain a similar shape
    BottomAppBarTopEdgeTreatment bottomAppBarTopEdgeTreatment = new BottomAppBarTopEdgeTreatment(
        getResources().getDimension(R.dimen.margin),
        getResources().getDimension(R.dimen.rounded_corner),
        getResources().getDimension(R.dimen.vertical_offset)
    );
    bottomAppBarTopEdgeTreatment.setFabDiameter(getResources().getDimension(R.dimen.diameter));
    
    ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED,radius)
        .setBottomLeftCorner(CornerFamily.ROUNDED,0)  
        .setBottomRightCorner(CornerFamily.ROUNDED,0)
        .setBottomEdge(bottomAppBarTopEdgeTreatment)
        .build();
    MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
    ViewCompat.setBackground(linearLayout,shapeDrawable);
    

    enter image description here