MaterialCardView clips its children when using rounded corners. If I use cardCornerRadius = true (which rounds all 4 corners of the card), the clipping behaves as expected -> children are not being drawn outside of the rounded area. If I try to round less corners (ie. only top corners, using ShapeAppearanceModel), then the clipping part is lost -> a top positioned child will be drawn over the rounded cornered area).
I realised that clip MaterialCardView calls
setClipToOutline(shapeAppearanceModel.isRoundRect(getBoundsAsRectF()));
inside of setShapeAppearanceModel, where isRoundRect returns true only if all four corners are rounded, so I tried applying clipToOutline = true on the MaterialCardView after setting the shapeAppearanceModel with top corners rounded, but with no similar result - children were still able to be drawn over the rounded part of the parent card.
What is actually triggering the clipping part and how can I force it on a top rounded shaped MaterialCardView?
LE: trial and error code:
// card is MaterialCardView
card.shapeAppearanceModel =
ShapeAppearanceModel()
.toBuilder()
.setTopRightCorner(CornerFamily.ROUNDED, cornerPx) // cornerPx = 24dp in pixels
.setTopLeftCorner(CornerFamily.ROUNDED, cornerPx)
.build()
card.apply {
preventCornerOverlap = true
clipChildren = true
clipToOutline = true
}
card.invalidateOutline()
Apply the following configurationsif you use shape appearance overlay in order to prevent the childs to overlapping from the corners:
Set cardPreventCornerOverlap to true
Then leave everything else that has to do with the corner settings as default (just delete the values from the textboxes in the design mode).
Then you should see something like this:
Related
In the second fragment I have used shapeable image view with shape appearance overlay to rounded. while applying material container transform, black corner are visible in shapeable image view. Help me how to remove the black corners.
I faced the same issue recently. I was setting background color to RED for example, but on lower android versions, corners got black just like in your case.
a workaround I came up with was to set background of ImageView using following lines of code:
imageView.background = ShapeDrawable(RectShape()).also {
it.paint.color = myColor
}
I'm trying to draw this shadow below list header, to give an illusion of LazyColumn items scrolling below (or behind) the header:
I tried:
using Modifier.shadow() but it does not yield desired result.
drawing a Box with gradient background, which does what I want, but get overdrawn with LazyColumn elements.
To counter this drawing over my gradient I tried the following:
putting Box after LazyColumn,
setting Modifier.zIndex(10f) to Box.
Of course, none of that worked. Immediately after LazyColumn elements were drawn, gradient disappeared.
What am I missing?
Have you tried a Surface with elevation?
From docs
https://developer.android.com/jetpack/compose/designsystems/material#elevation-overlays
Surface(
elevation = 2.dp,
color = MaterialTheme.colors.surface, // color will be adjusted for elevation
/*...*/
) {
// TODO - Put your list header here.
}
This is a gradient rectangle with rounded corners. The corner radious is 5dp and gray turns to completely transparent within 20dp.
How would I make this bitmap programmatically?
( It's important to have the corners do the same gradient color transition circularly. So I cannot use one rectangle gradient overlapping with an oval gradient coz this would create a darker area where the two gradients would overlap )
I want to create a hexagon shape button in android so that it's touch area does not overlaps (on the right) ie.I want heaxagon touch bound not a rectangle touch bound. I tried to use image view but it does not excludes tranparent(shown in blue, left). Is there any clickable object which is defined by xml shape/path. please can anyone show me xml shape/path part for just one button if its possible or any other method.
check out this
<com.github.siyamed.shapeimageview.{ClassName}
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:src="#drawable/neo"
app:siBorderWidth="8dp"
app:siBorderColor="#color/darkgray"/>
Attributes:
siBorderColor border color
siBorderWidth border width in dp
siBorderAlpha alpha value of the border between 0.0-1.0
siStrokeCap border stroke cap type butt|round|square
siStrokeJoin border stroke join type bevel|miter|round
siSquare set width and height to the minimum of the given values
true|false
siShape a reference to an SVG. This is used by ShapeImageView, not
the subclasses of it.
There's no system view that would do custom shapes for you. You need to create custom view: http://developer.android.com/training/custom-views/index.html and in its onTouchEvent() determine if user's tap is inside or outside clickable area and act accordingly.
Anyone got a clue how to handle following issue:
I have a Layout with background drawable like grey border, then I add padding of the border width to `Layout.
Child Layout has background drawable representing a selector, when pressed its darker gray (see image) and when not pressed its little bit lighter.
How to get rid of that sharp edge in the picture, I can't add margin to child Layout, because the pressed state must fill totally the child Layout.
It seems that the only solution here is to add the margin and suck it, or adjust the background drawables to have rounded corners as well.