I have a textview and want its corner to be in round shape. I already know it can be done using android:background="#drawable/somefile". In my case, this tag is already included so cannot use again. e.g android:background="#drawable/mydialogbox" is already there to create image in background
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:background="#drawable/mydialogbox"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</RelativeLayout>
so when I want textview(textview_name) also with round corner, how this can be achieved.
Create rounded_corner.xml in the drawable folder and add the following content,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#color/common_border_color" />
<solid android:color="#ffffff" />
<padding
android:left="1dp"
android:right="1dp"
android:bottom="1dp"
android:top="1dp" />
<corners android:radius="5dp" />
</shape>
Set this drawable in the TextView background property like so:
android:background="#drawable/rounded_corner"
I hope this is useful for you.
Beside radius, there are some property to round corner like topRightRadius, topLeftRadius, bottomRightRadius, bottomLeftRadius
Example TextView with red border with corner and gray background
bg_rounded.xml (in the drawables folder)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="10dp"
android:color="#f00" />
<solid android:color="#aaa" />
<corners
android:radius="5dp"
android:topRightRadius="100dp" />
</shape>
TextView
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_rounded"
android:text="Text"
android:padding="20dp"
android:layout_margin="10dp"
/>
Result
With the Material Components Library you can use the MaterialShapeDrawable.
With a TextView:
<TextView
android:id="#+id/textview"
../>
You can programmatically apply a MaterialShapeDrawable:
float radius = getResources().getDimension(R.dimen.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);
If you want to change the background color and the border just apply:
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.....));
shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color....));
Since your top level view already has android:background property set, you can use a <layer-list> (link) to create a new XML drawable that combines both your old background and your new rounded corners background.
Each <item> element in the list is drawn over the next, so the last item in the list is the one that ends up on top.
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap android:src="#drawable/mydialogbox" />
</item>
<item>
<shape>
<stroke
android:width="1dp"
android:color="#color/common_border_color" />
<solid android:color="#ffffff" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
There is Two steps
1) Create this file in your drawable folder :- rounded_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp" /> // set radius of corner
<stroke android:width="2dp" android:color="#ff3478" /> // set color and width of border
<solid android:color="#FFFFFF" /> // inner bgcolor
</shape>
2) Set this file into your TextView as background property.
android:background="#drawable/rounded_corner"
You can use this drawable in Button or Edittext also
create an xml gradient.xml file under drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<corners android:radius="50dip" />
<stroke android:width="1dip" android:color="#667162" />
<gradient android:angle="-90" android:startColor="#ffffff" android:endColor="#ffffff" />
</shape>
</item>
</selector>
then add this to your TextView
android:background="#drawable/gradient"
You can use the provided rectangle shape (without a gradient, unless you want one) as follows:
In drawable/rounded_rectangle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<stroke android:width="1dp" android:color="#ff0000" />
<solid android:color="#00ff00" />
</shape>
Then in your text view:
android:background="#drawable/rounded_rectangle"
Of course, you will want to customize the dimensions and colors.
Right Click on Drawable Folder and Create new File
Name the file according to you and add the extension as .xml.
Add the following code in the file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<stroke android:width="1dp" />
<solid android:color="#1e90ff" />
</shape>
Add the line where you want the rounded edge android:background="#drawable/corner"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="5dp" />
<solid android:color="#ffffff"/>
</shape>
</item>
</layer-list>
Put your TextView inside MaterialCardView:
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="25dp"
>
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/emergency_sms_template"
android:padding="6dp"
android:textSize="11sp"
/>
</com.google.android.material.card.MaterialCardView>
You can use SVG for rounding corners and load into an ImageView and use ConstraintLayout to bring ImageView on TextView
I used it for rounded ImageView and rounded TextView
Simply using an rounded corner image as the background of that view
And don't forget to have your custom image in drawable folder
android:background="#drawable/my_custom_image"
Related
How to add a shadow border on 4 sides of an edit text as shown in the image? Applied below code but it creates a shadow on single side only.
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFBDBDBD"
android:centerColor="#65FFFFFF"
android:endColor="#00FFFFFF"
/>
<stroke
android:width="1dp"
android:color="#C3C3C3" />
<corners
android:radius="5dp" />
</shape>
You can use elevation like :-
android:elevation="5dp"
elevation worked as Z-index.
You can try adding width and color inside the shape tag.
It will look like this:
<stroke android:width="3dp" android:color="#bfafb2"/>
First create this background drawable with a rectangle, round corner and a stroke in grey. Called background_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<corners android:radius="5dp" />
<stroke android:width="1dp"
android:color="#C3C3C3"/>
</shape>
After that you set this as android:background="" in your LinearLayout and EditText, both widgets have android:elevation="2dp" for it's shadow on each sites matching the stroke of the rectangle. Here is your layout called activity_main.xml:
<LinearLayout
android:layout_width="200dp"
android:layout_height="100dp"
android:elevation="2dp"
android:id="#+id/linear_layout"
android:background="#drawable/background_drawable"
android:layout_centerInParent="true"
android:orientation="horizontal" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/edit_text"
android:elevation="2dp"
android:layout_marginTop="10dp"
android:background="#drawable/background_drawable"
android:layout_below="#+id/linear_layout"
android:layout_centerHorizontal="true"
android:padding="10dp"/>
The final result will look like this image.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CABBBBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
I want to add shadow to my text view . I found solutions for adding gradients and one other solution which suggested creating a duplicate text view with a lighter shade at the back of the original textView to give a shadow effect. But i want to have a real shadow effect.
This is what i tried .I created an xml file rounded_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#33000000"
android:dashWidth="3dp"
/>
<solid android:color="#000000" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="10dp"
/>
</shape>
I added elevation and translationz but its not showing shadow in the output
<TextView
android:background="#drawable/rounded_corners"
android:layout_width="260dp"
android:layout_height="70dp"
android:textSize="50sp"
android:text="Welcome User"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:textColor="#000000"
android:elevation="50dp"
android:translationZ="10dp"
android:id="#+id/welcome"/>
I assume that you want to put the shadow on the TextView and not on the text itself. The you can make a layer-list inside your rounded corner drawable file like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/black" />
<corners android:radius="3dp" />
</shape>
</item>
<item android:bottom="2dp" android:right="2dp">
<shape android:shape="rectangle">
<solid android:color="#282828" />
<stroke android:width="1dp" android:color="#302f2f" />
</shape>
</item>
</layer-list>
If you put the drawable as background of your TextView
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corner"
the output will link like this:
Just pu this on your TextView XML for shadow on the text:
android:shadowColor="#android:color/holo_purple"
android:shadowDx="10"
android:shadowDy="10"
android:shadowRadius="5"
you can edit with your preferences.
And create a Drawable file for shadow in all input, more or less like this one:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#d8d8d8" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="4px" android:left="0px" android:right="4px" android:top="0px">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
You can use the shadow XML attributes.
android:shadowColor="#000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="50"
For more info check this link, https://developer.android.com/reference/android/widget/TextView.html#attr_android:shadowColor
i have a listview and the row model/design should be something as shown in the image below.
My question is, how can I design a view to appear as shown in the image in the the third row?? I want to design something like the blue rectangular shape with round corners and then write the value on it as shown in the image
Is it a textview or imageView? Please advice how can i design such a view.
image:
You must create a TextView and give it a Shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#color/common_border_color" />
<solid android:color="replace with blue color" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="5dp" />
</shape>
Make this in an xml called rounded_corner.xml in the drawable folder.
android:background="#drawable/rounded_corner"
You can use textview and as a background of text view use below xml
rounded_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#abcdef" >
</solid>
<corners
android:radius="20dp" >
</corners>
</shape>
In your drawable create one xml file and add this code in that
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<padding
android:top="3dp"
android:left="8dp"
android:right="8dp"
android:bottom="3dp"/>
<solid android:color="#00796B" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
In your view(Button/TextView) add background, some ex code
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/delete"
android:text="Ur text"/>
I have one big layout, and one smaller layout inside of it.
How do I create a line border around the small layout?
Sure. You can add a border to any layout you want. Basically, you need to create a custom drawable and add it as a background to your layout. example:
Create a file called customborder.xml in your drawable folder:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<stroke android:width="1dp" android:color="#CCCCCC"/>
</shape>
Now apply it as a background to your smaller layout:
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/customborder">
That should do the trick.
Also see:
http://tekeye.biz/2012/add-a-border-to-an-android-layout
How to add border around linear layout except at the bottom?
Creat XML called border.xml in drawable folder as below :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
then add this to linear layout as backgound as this:
android:background="#drawable/border"
Try this:
For example, let's define res/drawable/my_custom_background.xml as:
(create this layout in your drawable folder) layout_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:width="2dp" android:height="2dp"
android:color="#FF0000" />
<solid android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
<corners android:radius="1dp" android:bottomRightRadius="5dp"
android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"
android:topRightRadius="0dp" />
</shape>
</item>
</layer-list>
main.xml
<LinearLayout
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/layout_border" />
</LinearLayout>
Create a one xml file in drawable folder
<stroke
android:width="2dp"
android:color="#B40404" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<corners android:radius="4dp" />
Now call this xml to your small layout background
android:background="#drawable/yourxml"
This solution will only add the border, the body of the LinearLayout will be transparent.
First, Create this border drawable in the drawable folder, border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:color="#ec0606"/>
<corners android:radius="10dp"/>
</shape>
Then, in your LinearLayout View, add the border.xml as the background like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/border">
you can do it Pragmatically also
GradientDrawable gradientDrawable=new GradientDrawable();
gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input));
Then set the background of layout as :
LinearLayout layout = (LinearLayout ) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);
I'll add Android docs link to other answers.
https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
It describes all attributes of the Shape Drawable and stroke among them to set the border.
Example:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#F00"/>
<solid android:color="#0000"/>
</shape>
Red border with transparent background.
Don't want to create a drawable resource?
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:minHeight="128dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="#android:color/white">
<TextView ... />
</FrameLayout>
</FrameLayout>
Try This in your res/drawable
<?xml version="1.0" encoding="UTF-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<padding android:left="15dp"
android:right="15dp"
android:top="15dp"
android:bottom="15dp"/>
<stroke android:width="10dp"
android:color="#color/colorPrimary"/>
</shape>
</item><item android:left="-5dp"
android:right="-5dp"
android:top="-5dp"
android:bottom="-5dp">
<shape android:shape="rectangle">
<solid android:color="#color/background" />
</shape></item></layer-list>
Nobody seems to be using the method we came up with so I thought I would share, as it involves much simpler code.
Simply nest one layout inside another with padding, and the outer one can be colored, making a frame.
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/llSignatureBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#android:color/black"
android:orientation="vertical"
android:padding="4dp">
<com.yourmom.mobiledriverapp.Controls.SignatureArea
android:id="#+id/signatureArea"
android:layout_width="#dimen/signaturearea_width"
android:layout_height="#dimen/signaturearea_height"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
I want to underline a TextView with a bent line, like this:
I thought a background drawable would be right for this but I can't figure out how to achieve that. Does anyone have an idea?
You can create that background either with a 9-patch image from an image created with your favorite image editor(and it should be pretty easy to do it) or creating a xml drawable. To create that type of drawable in xml you would use a ClipDrawable that will clip another Shape drawable. So you will have:
background.xml(this will be the background of the TextView):
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="#drawable/background_shape"
android:gravity="bottom" />
and the background_shape.xml file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp" />
<solid android:color="#ffffff" />
<padding
android:bottom="5dp"
android:left="7dp"
android:right="7dp" />
<stroke
android:width="2dp"
android:color="#000000" />
</shape>
Then in your activity, you would get the background drawable for that TextView and set its level:
ClipDrawable clip = (ClipDrawable) findViewById(R.id.textView1).getBackground();
clip.setLevel(5000);
You can teak the look of the drawable by playing with the corners radius, padding and the level of the ClipDrawable.
I don't know if you'll be able to obtain the exact image with the xml drawable.
shape.xml (Save this file in drawables)
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#000000"/> <!-- This is the Border Color For TextView-->
<!-- "#5D2E8C" -->
<solid android:color="#ffffff" /> <!-- background to the TextView -->
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
<corners android:radius="20dp" />
</shape>
Now, Set the background property for TextView like this::
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shape"
/>