With the Material Components Library you can use the MaterialShapeDrawable
to draw custom shapes.
For example with a TextView
you can do:
<TextView
android:id="@+id/textview"
android:backgroundTint="@color/secondaryColor"
../>
Then create a MaterialShapeDrawable
:
float radius = getResources().getDimension(R.dimen.default_corner_radius);
TextView textView = findViewById(R.id.textview);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(textView,shapeDrawable);
With a simple View
:
<View
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="4dp"
android:backgroundTint="@color/..."/>
Then apply the same MaterialShapeDrawable
:
View line = findViewById(R.id.line);
ViewCompat.setBackground(line,shapeDrawable);
You can also create different corners:
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,0)
.setBottomRightCorner(CornerFamily.ROUNDED,radius)
.build();
Also most of the components provided by the Material Component Library have a MaterialShapeDrawable as background.
In these cases just use something like (in this example a MaterialCardView).
MaterialCardView cardView = findViewById(R.id.card);
cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel()
.toBuilder()
.setBottomLeftCornerSize(...)
.setBottomEdge(...)
.build());
It requires the version 1.1.0 of the library. Currently 1.1.0-beta02
.
CardView
? – Pernas