'How to draw rounded rectangle in Android UI?
I need to draw a rounded rectangle in the Android UI. Having the same rounded rectangle for TextView
and EditText
would also be helpful.
Solution 1:[1]
In your layout xml do the following:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/holo_red_dark" />
<corners android:radius="32dp" />
</shape>
By changing the android:radius
you can change the amount of "radius" of the corners.
<solid>
is used to define the color of the drawable.
You can use replace android:radius
with android:bottomLeftRadius
, android:bottomRightRadius
, android:topLeftRadius
and android:topRightRadius
to define radius for each corner.
Solution 2:[2]
I think, this is you exactly needed.
Here drawable(xml) file that creates rounded rectangle. round_rect_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
Here layout file: my_layout.xml
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/round_rect_shape"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Something text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ff0000" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>
-> In the above code, LinearLayout having the background(That is the key role to place to create rounded rectangle). So you can place any view like TextView, EditText... in that LinearLayout to see background as round rectangle for all.
Solution 3:[3]
In monodroid
, you can do like this for rounded rectangle, and then keeping this as a parent class, editbox
and other layout features can be added.
class CustomeView : TextView
{
public CustomeView (Context context, IAttributeSet ) : base (context, attrs)
{
}
public CustomeView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
}
protected override void OnDraw(Android.Graphics.Canvas canvas)
{
base.OnDraw(canvas);
Paint p = new Paint();
p.Color = Color.White;
canvas.DrawColor(Color.DarkOrange);
Rect rect = new Rect(0,0,3,3);
RectF rectF = new RectF(rect);
canvas.DrawRoundRect( rectF, 1,1, p);
}
}
}
Solution 4:[4]
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorAccent" />
<corners
android:bottomLeftRadius="500dp"
android:bottomRightRadius="500dp"
android:topLeftRadius="500dp"
android:topRightRadius="500dp" />
</shape>
Now, in which element you want to use this shape just add:
android:background="@drawable/custom_round_ui_shape"
Create a new XML in drawable named "custom_round_ui_shape"
Solution 5:[5]
You could just define a new xml background in the drawables folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="enter_your_desired_color_here" />
<corners android:radius="enter_your_desired_radius_the_corners" />
</shape>
After this just include it in your TextView or EditText by defining it in the background.
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="80dp"
android:background="YOUR_FILE_HERE"
Android:layout_weight="1"
android:gravity="center"
android:text="TEXT_HERE"
android:textSize="40sp" />
Solution 6:[6]
Right_click on the drawable and create new layout xml file in the name of for example button_background.xml. then copy and paste the following code. You can change it according your need.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="14dp" />
<solid android:color="@color/colorButton" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:width="120dp"
android:height="40dp" />
</shape>
Now you can use it.
<Button
android:background="@drawable/button_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Solution 7:[7]
Use CardView for Round Rectangle. CardView give more functionality like cardCornerRadius, cardBackgroundColor, cardElevation & many more. CardView make UI more suitable then Custom Round Rectangle drawable.
Solution 8:[8]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="4dp" />
</shape>
Solution 9:[9]
I tried several methods and can say that you should also add View.setClipToOutline (API >= 21):
textView.clipToOutline = true
In this case you won't get a rectangular selection below TextView
(it occurs on some devices) like below:
Some people wrap a rectangle with CardView
. It works, but on some old devices it shows wrong corners, if radius is large enough:
In TextView
you can also add this attribute:
android:theme="@style/Theme.MaterialComponents.Light"
It will show darker gray selection when click light gray text labels.
Solution 10:[10]
paint.apply {
strokeWidth = lineWidth.toFloat()
style = Paint.Style.STROKE
color = lineColor
***strokeCap = Paint.Cap.ROUND***
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow