android - How to set corner radius programmatically?
Solution 1
Here example for how to create GradientDrawable
shape programmatically.
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.RED);
shape.setStroke(3, Color.YELLOW);
For change the radius for all corners of the gradient.
shape.setCornerRadius(15);
For Change the radius for specific corners of the gradient.
shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
You can use this drawable as a background as below :
view.setBackgroundDrawable(shape);
Solution 2
The answer of @Rohit Suthar in Kotlin, with dp converted to pixel, and color resources:
private val STROKE_WIDTH_PX = 2.dpToPx
private val CORNER_RADIUS_PX = 5.dpToPx.toFloat()
val shape = GradientDrawable()
shape.shape = GradientDrawable.RECTANGLE
shape.setColor(ContextCompat.getColor(context, R.color.pink))
shape.setStroke(STROKE_WIDTH_PX, ContextCompat.getColor(context, R.color.black))
// choose either cornerRadius or cornerRadii
shape.cornerRadius = CORNER_RADIUS_PX
shape.cornerRadii = floatArrayOf(
// top left
0f,
0f,
// top right
CORNER_RADIUS_PX,
CORNER_RADIUS_PX,
// bottom right
CORNER_RADIUS_PX,
CORNER_RADIUS_PX,
// bottom left
0f,
0f
)
view.background = shape
To convert dp to px:
val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
P.Lorand
Updated on June 05, 2022Comments
-
P.Lorand almost 2 years
I have two
ListViews
(leftList, rightList). I also have oneTextView
which I use as row view in both of them. I have a rectangle drawable shape and set it as background to theTextView
.I would like to change this shape and have rounded corners on the left.
What I tried :
GradientDrawable gradientDrawable = new GradientDrawable(); // gradientDrawable.setCornerRadius(30); ((GradientDrawable)gradientDrawable.mutate()).setCornerRadius(30); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { viewHolderPattern.digits.setBackground(gradientDrawable); }
I have created a new layout in drawable with the right corner radius set and set that to the textView with
setBackgroundRescource
but still didn't work.The TextView that I use as items in both listViews
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/digitsTextView" android:textSize="15dp" android:paddingTop="7dp" android:paddingBottom="8dp" android:fontFamily="monospace" android:textColor="@color/selected_items" android:background="@drawable/digital_text_shape"> </TextView>
Shape layout digital_text_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="@color/orange" /> <solid android:color="@color/orange" /> <corners android:bottomLeftRadius="20dp" android:bottomRightRadius="0dp" android:topLeftRadius="20dp" android:topRightRadius="0dp" /> <padding android:bottom="0dp" android:left="20dp" android:right="0dp" android:top="0dp" /> </shape>
Left list and Right list
<!-- Left ListView --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" > <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:id="@+id/left_listView" android:divider="@android:color/transparent" android:dividerHeight="0.1sp" android:choiceMode="singleChoice" > </ListView> </LinearLayout> <!-- Right ListView --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" > <ListView android:id="@+id/right_listView" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@android:color/transparent" android:dividerHeight="0.1sp" android:choiceMode="singleChoice" > </ListView> </LinearLayout>
-
barış çıracı over 4 years{mTopLeftRadius, mTopLeftRadius, mTopRightRadius, mTopRightRadius, mBottomRightRadius, mBottomRightRadius, mBottomLeftRadius, mBottomLeftRadius}
-
Michael P almost 3 yearsCall .mutate() on the drawable if it is used elsewhere to prevent those instances from changing as well