CardView: How do I add a gradient background while maintaining the radius - android

I want to re-create the image below with a CardView. To achieve this, I created a gradient file (btn_gradient.xml) and then proceeded to create the CardView.
CardView implementation:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_margin="25dp"
app:cardElevation="0dp"
app:cardCornerRadius="4dp"
app:cardPreventCornerOverlap="false">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/btn_gradient"
android:text="Create Account"
android:textColor="#000000"
android:textStyle="bold"
android:textAllCaps="false"/>
</android.support.v7.widget.CardView>
Everything works fine this way except that the radius disappears and this is not what I want. Is there a way I can set the gradient directly on the CardView? The cardBackgroundColor attribute accepts only colors, not drawables.
Any help would be appreciated.
Addendum: As requested, this is my btn_gradient.xml file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="0"
android:startColor="#ffc200"
android:endColor="#fca10b" />
</shape>

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_10sdp"
app:cardCornerRadius="#dimen/_10sdp"
app:cardElevation="#dimen/_1sdp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/gradient_16"
android:padding="#dimen/_6sdp">
</LinearLayout>
</androidx.cardview.widget.CardView>
and gradient be like
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#5B86E5"
android:endColor="#36D1DC"
android:angle="180" />
<corners
android:radius="10dp">
</corners>
</shape>
CardView card_radius and gradient radius should be same dimentions

If I may ask, are you per-chance testing/running on a pre-lollipop Android device? Your code seems to work as you desire (curved corners showing with the gradient) except on Android 4.
To achieve the desired result on pre-lollipop devices, you can add <corners android:radius="4dp" /> to your #drawable/btn_gradient file, (you would have to set the corner radius to match the CardView's cardCornerRadius.

Move this line
android:background="#drawable/btn_gradient"
To the CardView object
UPDATE
My bad:
Inside the place a layout to wrap the content of the .
In this case, I'd go with a <FrameLayout> like this:
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLyout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn_gradient">

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#004e92"
android:endColor="#614385"
android:angle="90" />
<corners
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp">
</corners>
</shape>
[![enter image description here][1]][1]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:background="#drawable/color"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="10dp"
android:layout_height="100dp">
<LinearLayout
android:layout_width="match_parent"
android:background="#drawable/cardcolor"
android:layout_height="100dp">
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>``
</LinearLayout>
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/iewIm.png
[1]: https://i.stack.imgur.com/pHIlW.png

I've tried integrating the gradient on card view dynamically & faced the issue that the corner radius got impacted that was set on the view property in xml file like app:cardCornerRadius="#dimen/margin_20".
Solution :
Implementing the gradient on card view. I'm getting 2 gradients from api that will be incorporated from left to right.
` try {
val startColor = sample?.gradient1
val endColor = sample?.gradient2
val colors =
intArrayOf(Color.parseColor(startColor),
Color.parseColor(endColor))
val gradientDrawable = GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT, colors
)
myCardView.background = gradientDrawable
} catch (e: Exception) {
// default color to white
myCardView.setBackgroundColor(
ContextCompat.getColor(
mContext,
R.color.white
)
)
}`
Now on running the code, the card view renders with the gradient value but the issue occurs with the corner radius of card view that was set statically at xml. The corner radius got removed. To solve the issue, we simply use to set the radius of the gradient drawable that holds the drawable color in gradient form. Just add below 3 lines before setting the background.
gradientDrawable.colors = colors
// setting the corner radius on gradient drawable
gradientDrawable.cornerRadius = 40f
myCardView.background = gradientDrawable
The final code with gradient configuration & setting the corner radius is :
try {
val startColor = sample?.gradient1
val endColor = sample?.gradient2
val colors =
intArrayOf(Color.parseColor(startColor),
Color.parseColor(endColor))
val gradientDrawable = GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT, colors
)
gradientDrawable.colors = colors
// setting the corner radius on gradient drawable
gradientDrawable.cornerRadius = 40f
myCardView.background = gradientDrawable
myCardView.background = gradientDrawable
} catch (e: Exception) {
// default color to white
myCardView.setBackgroundColor(
ContextCompat.getColor(
mContext,
R.color.white
)
)
}`
Screenshot for better view of code :
Hope this will help. Happy Coding :) Cheers!

Do not use android:background attribute in XML file.
use app:cardBackground instead.
To wrap it up, first create a gradient background XML file in drawables.
then assign it to the app:cardBackgroundColor like this:
app:cardBackgroundColor="#drawable/gradient_background"
if you don't know how to create the gradient_background.xml, write click on drawables directory, create new xml file and paste the code below.
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#color/secondaryColor"
android:endColor="#color/primaryColor"
android:angle="90"/>
</shape>

Related

Image view not clipped inside constraint layout with drawable shape background

This is my drawable shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:bottomRightRadius="60dp"/>
<solid android:color="#color/primary_blue"/>
</shape>
My layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="296dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clipChildren="true"
android:clipToPadding="false"
android:background="#drawable/bg_shape_bottom_right_curved"
app:cardPreventCornerOverlap="false"
android:outlineProvider="background"
android:layout_height="198dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/phase_banner"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
</LinearLayout>
The image keeps on being square and not getting the shape of drawable. Please help
When i add shape to the layout its showing the shape . But when i set image src its showing sqaure image , when setting background, it getting into right shape. But i need to load image from glide . so i need to make it work with src
Unfortunately, your background drawable does not qualify for clipping. From Clip Views:
Only rectangle, circle, and round rectangle outlines support clipping, as determined by the Outline.canClip() method.
Your background drawable does not qualify as a rounded rectangle but is implemented internally as a path (I believe). This is one reason clipping is failing for you.
Let's create a simple rounded rectangle drawable:
simple_rounded_rectangle.bg
<shape android:shape="rectangle" >
<corners android:radius="30dp"/>
<solid android:color="#color/primary_blue"/>
</shape>
Also, let's modify the layout a little:
activity_main.xml
<LinearLayout
android:id="#+id/layout"
android:layout_width="400dp"
android:layout_height="198dp"
android:background="#drawable/simple_rounded_rectangle"
android:outlineProvider="background">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/phase_banner" />
</LinearLayout>
We will also need to add some code to direct the view system to clip the view to the outline:
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val layout = findViewById<LinearLayout>(R.id.layout)
layout.clipToOutline = true
}
}
The main thing in this code is the line
layout.clipToOutline = true
which indicates that clipping should occur. There should be an XML attribute that does the same thing android:clipToOutline but there isn't below API 31. See the android:clipToOutline bug report.
Putting it altogether we see the following. (The image is one I supplied.)
If you use your drawable for the background, it will not be clipped for the reason I mentioned above.
You will need to fall back to a different method of clipping if you need asymmetric corners on your drawable. You may want to look at applications of clipPath. You will find information on how to do this if you do some online searching. I also suggest the Medium post Clipping and shadows on Android for other alternatives.
Take a look at the Material Components Library. MaterialCardView and ShapeableImageView may be useful.
This is your drawable shape.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
/>
<solid android:color="#color/primary_blue"/>
</shape>
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clipChildren="true"
android:clipToPadding="false"
app:cardPreventCornerOverlap="false"
android:outlineProvider="background"
android:layout_height="198dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/phase_banner"
android:src="#drawable/XYZ"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
</LinearLayout>
Try this you want like this?

Programatically change background of Drawable, maintaining the corner radius

interview_timeline_row.xml
<LinearLayout
android:id="#+id/interviewTimelineIconLayout"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_marginTop="20dp"
android:background="#drawable/timeline_row_icon_layout_bg"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/interviewTimelineRowIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:padding="6dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
timeline_row_icon_layout_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="#dimen/_50sdp" />
<stroke android:width="1dp" android:color="#color/white" />
<solid android:color="#color/ic_rescheduled"/> //need to add this programatically
</shape>
InterviewTimeline.java
iconBg = row.findViewById(R.id.interviewTimelineIconLayout);
iconBg.setBackgroundColor(getResources().getColor(R.color.ic_rescheduled)); //this is the wrong way to go about it
I want to use the timeline_row_icon_layout_bg.xml in various places in my app, and it should have a different background color each time. If I use the iconBg.setBackgroundColor() method, then it ignores the radius and I have a square background color.
Since you are just using the shape to create a layout with rounded corners and a border, the first option is to wrap your LinearLayout inside a CardView and then apply to the card the corner radius, the stroke and the background color.
Otherwise you could use the MaterialShapeDrawable included in the Material Components Library to draw custom shapes.
Just remove from the LinearLayout the android:background:
<LinearLayout
android:id="#+id/interviewTimelineIconLayout"
android:layout_width=".."
android:layout_height="..
..>
<!-- ..... -->
</LinearLayout>
Then in your code you can apply a ShapeAppearanceModel. Something like:
float radius = getResources().getDimension(R.dimen.corner_radius);
LinearLayout linearLayout= findViewById(R.id.interviewTimelineIconLayout);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
//Fill the LinearLayout with your color
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.yourColor));
//Stroke color and width
shapeDrawable.setStrokeWidth(2.0f);
shapeDrawable.setStrokeColor(...);
ViewCompat.setBackground(linearLayout,shapeDrawable);
In this way you easily change and set the color background and the stroke.
Copy your "timeline_row_icon_layout_bg.xml" file and rename it "timeline_row_icon_layout_bg_new.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="#dimen/_50sdp" />
<stroke android:width="1dp" android:color="#color/white" />
<solid android:color="#color/put_the_color_you_need" />
</shape>
And you can set background to layout:
iconBg.setBackgroundResource(R.drawable.timeline_row_icon_layout_bg_new);
set the background of LinearLayout programmatically.
val bgDrawable = resources.getDrawable(R.drawable.timeline_row_icon_layout_bg, null).apply{
colorFilter = PorterDuffColorFilter(
ResourcesCompat.getColor(resources, R.color.ic_rescheduled, null),
PorterDuff.Mode.SRC_IN
)
}
iconBg.background = bgDrawable
move the shape drawable timeline_row_icon_layout_bg to the drawable resource folder.

View setBackground with drawable remove the elevation's shadows

I try to implement the Material Design's elevation concept to my RecyclerView.
For each item of the RecyclerView I use this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="0dp"
android:elevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
.../>
<CheckBox
.../>
</RelativeLayout>
</android.support.v7.widget.CardView>
When I select item, I apply programmatically a drawable to the background for effect, and I set the elevation to 8dp as advised by Material Design specifications.
val draw = ResourcesCompat.getDrawable(resources, R.drawable.selected_task, null)
draw?.setColorFilter(task.color.aRGB.toInt(), PorterDuff.Mode.OVERLAY)
view.background = draw
view.elevation = 8F
(I code in Kotlin)
The drawable:
<?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="#color/select_border"/>
<solid
android:color="#color/select_fill"/>
</shape>
and the colors values
<color name="select_border">#78000000</color>
<color name="select_fill">#32000000</color>
The problem is when I select one item, the drawable is nicly applied but the shadows are tottaly removed and not amplified !
And if I not set the background with the drawable, it's works well. The shadows are amplified like expected.
What is the problem ?
It is trouble from 2014 https://issuetracker.google.com/issues/37008403
Decision 1
Do not use colors with transparency for background CardView
Decision 2
Set background for View or ViewGroup inside the CardView instead of this cardView, then everything works correctly

Ripple effect over the actual border

I have created a listview and added a border to it's items.
something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:background="#drawable/listviewborderbox"
android:padding="10dp" >
<LinearLayout
android:id="#+id/sharedbyyouNameLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".70"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/sharedbyyoutext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/sampletext1"
android:textColor="#color/blackText"
android:textSize="14sp" />
<TextView
android:id="#+id/sharedbyyouselected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/sampletext2"
android:textColor="#color/blackText"
android:textSize="16sp"
tools:ignore="RtlHardcoded" />
</LinearLayout>
<LinearLayout
android:id="#+id/sharedbyyouLayoutforarrow"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".10"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_next"
tools:ignore="RtlSymmetry,RtlHardcoded,ContentDescription" />
</LinearLayout>
</LinearLayout>
And I have ripple effect value in Drawable-v21 like this:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/white"> <item android:drawable="#color/footercolor"/> </ripple>
Border shape xml in drawable folder is this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/colorforbodybox" />
<corners android:radius="10dip"/>
<stroke android:width="2dip" android:color="#color/colorforborder" />
</shape>
Ripple effect works but ripple effect is shown outside the border line that I have drawn. Please check pic below:
How do I make the ripple effect not to cross the border in the list view?
To achieve rounded corner ripple effect change your ripple xml file to:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#android:color/white"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#color/footercolor"/>
</shape>
</item>
</ripple>
The problem I had was that the corner radius of my views was not a fixed value therefore using the xml suggested didn't work for me.
I needed a something that would adapt the ripple effect every time regardless of the shape used so...
I used a simple view extension:
fun View.addRippleEffect(rippleColorId: Int = R.color.rippleColor) { // Here you can pass the color you want for the ripple effect and assign a "default" value
val rippleColor = ColorStateList.valueOf(ContextCompat.getColor(App.context(), rippleColorId))
this.background = RippleDrawable(
rippleColor, // This is the color of the effect and needs to be a ColorStateList
this.background, // ( = content ) With this you use your view's background as the content of the ripple effect
this.background) // ( = mask ) With this the ripple will take the shape of the background and not "spill over". (Could be null IF you did set the previous variable "content = this.background")
}
OR, if you want to separate the two layers:
fun View.addRippleEffect(rippleColorId: Int = R.color.rippleColor) {
val rippleColor = ColorStateList.valueOf(ContextCompat.getColor(App.context(), rippleColorId))
this.foreground = RippleDrawable( //Using the foreground allows you to give the view whatever background you need
rippleColor,
null, //Whatever shape you put here will cover everything you've got underneath so you probably want to keep it "null"
this.background)
}
Basically you give a view a background (rounded rectangle with borders in your case) then you can simply call the extension in your Activity/Fragment:
whateverView.addRippleEffect()
//or
whateverView.addRippleEffect(R.color.red)
See: https://developer.android.com/reference/android/graphics/drawable/RippleDrawable
1. Create the Ripple Drawable Contains the Backgound Shape
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?colorControlHighlight"> //defaul ripple color
<item>
<shape //the background shape when it's not being click
android:shape="rectangle">
<solid
android:color="#color/colorPrimary" />
<corners
android:radius="32dp" />
</shape>
</item>
</ripple>
2. Applying the Drawable to the View and REMOVE THE SHADOW
<Button
style="?borderlessButtonStyle" //remove the default shadow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_button" //here
android:text="Sign up"
android:textAllCaps="false"
android:textColor="#android:color/white" />
Try setting clipToOutline of your View / ViewGroup to true, either in the code or through XML, it should limit the ripple's area accordingly (as long as you background shape matches the requirements, see the docs for more details).

how to create rounded corner videoview in android?

In my application, I want to display videoview as a rounded corners. I have tried placing videoview/surfaceview inside linearlayout with rounded corner set to linearlayout. but it does not work perfectly. I can not set rounded corner to videoview/surfaceview. I want to set view as below image:
Anyone have idea how to do this?
Not sure why there are so many bad answers when the solution is really simple (as long as you're min sdk > 21). Creating a shape and putting it overtop of the video won't work because you obviously want the background to be transparent to see the views behind it.
I found the answer here Android View Clipping. You just put the video view in a frame layout, add a rounded background to the frame layout, add an outline provider and clip the frame layout to the outline.
The background rounded_video_background:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000"/>
<corners android:radius="16dp" />
</shape>
The frame layout and video view inside of it:
<FrameLayout
android:id="#+id/video_view_container"
android:layout_width="90dp"
android:layout_height="120dp"
android:background="#drawable/rounded_video_background"
android:outlineProvider="background">
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
</FrameLayout>
And the final step is clipping to the outline (didn't see a way to do it in xml so I did it programatically):
video_view_container.clipToOutline = true
Its worked for me,
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_width="match_parent"
card_view:cardCornerRadius="25dp"
android:layout_height="wrap_content">
<VideoView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="215dp" />
</android.support.v7.widget.CardView>
You can make it using a FramLayout & an XML drawable
FramLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<VideoView
android:layout_width="match_parent"
android:layout_height="#dimen/dp_240"
android:layout_margin="#dimen/dp_24"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rounded_corner_video_bg" />
</FrameLayout>
XML Drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="#dimen/dp_24"
android:color="#color/md_white_1000" />
<corners android:radius="#dimen/dp_24" />
</shape>
You could try layering different views on top of each other to create the rounded corners you are looking for. Try placing four ImageViews over the VideoView in each corner, to achieve the desired rounded corners. I have had success using a RelativeLayout to accomplish this, but you could also try using a FrameLayout to hold the Views together.
It is directly not possible, but you can do this by draw a imageview on top of videoview and set an image having transparent from between and solid color in rounded shape on the corners.
check this: Click here
Its simple,
1. Create a drawable with rounded corners as mentioned by #sunil kumar
2. Set that drawable to your layout as a background
3. When using that layout set layout(your layout item name).clipToOutline = true
This is XML code of rounded VideoView.
<androidx.cardview.widget.CardView
android:id="#+id/videoCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="20dp"
card_view:cardBackgroundColor="#color/white">
<VideoView
android:id="#+id/relativeVideo"
android:layout_width="match_parent"
android:layout_height="225dp"
android:paddingTop="-10dp"
android:paddingBottom="-10dp" />
</androidx.cardview.widget.CardView>
Negative padding is important otherwise height of VideoView is smaller than cardview by half of cornerRadius in both top and bottom side. You can set height whatever you want but negative padding should be half of cardCornerRadius all the time. Purple in the image is a video preview, not related to xml.
Have a nice day!
put rounded.xml in your drawable folder and set on framelayout of video like android:background="#drawable/rounded.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFFFF" />
<corners android:radius="7dp" />
</shape>
Create an xml file in drawable folder called shape_video.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="6dp" />
</shape>
Change the radius according to your rqmnt and in the background attribute of your videoview, give
android:background="#drawable/shape_video"

Categories

Resources