I want to make a dialog fragment with corners. The problem I have
As you see, if a TextView in on top, there will be some parts of it that is over the transparent part of the dialog, which is bad. This also happens with that scrolling bar from the listview. I want to "cut" that parts..or mask them like in the second photo. Is this possible?
shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FFF" />
<corners android:topLeftRadius="20dp" android:topRightRadius="20dp"
android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp"/>
<stroke android:color="#7F7F7F" android:width="1dp" />
</shape>
I'm using a linear layout for the fragment, with the above background. I call
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
To get rid of the background color & title bar.
You can simply use MaterialAlertDialogBuilder to create custom dialog with rounded corners. then create a Alert Dialog object in Java class like this : AlertDialog alertDialog = new MaterialAlertDialogBuilder(this,R. style.
This method is deprecated. androidx.
To make the Bottom Sheet with rounded corners, simply add the shape parameter inside the showModalBottomSheet function and assign the RoundedRectangleBorder() with the borderRadius property set to BorderRadius. vertical(top: Radius. circular(25.0),), .
Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.
The simple (non-clipping) way
The easiest way would be to simply add padding to your layout. Then your text won't overlap with the rounded corners.
If you must have rounded corners without padding, there are a few other options that I know of:
Option 1: Clip Views (API 21+)
Support for clipping views was added to the View
class in API 21. If you don't need earlier API levels, it's really easy to use this in-built clip feature:
android:clipToOutline="true"
Unfortunately, there's a bug and this attribute doesn't seem to work. Luckily, we can set the clipping in Java:
View.setClipToOutline(true)
I've tested it, and it works:
Option 2: Nine-Patch frame mask
If you need to support rounded clipping on devices < API 21, you can use this general approach:
FrameLayout
or RelativeLayout
, create a rounded corner nine-patch to "frame" the rest of your content.If you use this approach, just make your nine-patch corners match the color of your background.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With