-1

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

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
amirhesni
  • 441
  • 1
  • 6
  • 22

1 Answers1

3

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • that sounds perfect but i couldn't find MaterialShapeDrawable dependency can you add it too ? – amirhesni Jan 15 '20 at 11:27
  • @amirhesni You can find all [info here](https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md). Use the version 1.1.0-rc01 – Gabriele Mariotti Jan 15 '20 at 13:28