Simple way to customize DialogFragment.
Add maven jitpack.io and dependencies in build.gradle (Project) :
// build.gradle projectallprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
// build.gradle app/moduledependencies {
...
implementation 'com.github.gzeinnumer:EasyDialogFragment:version'
implementation 'com.github.gzeinnumer:SimpleMaterialStyle:last-version'//check last version on https://github.com/gzeinnumer/SimpleMaterialStyle
}- DialogFragment (docs)
Add This Line to res/color.xml. Important
<?xml version="1.0" encoding="utf-8"?>
<resources>
<colorname="colorPrimary">#6200EE</color>
<colorname="colorPrimaryDark">#3700B3</color>
<colorname="colorAccent">#03DAC5</color>
</resources>- Dialog View
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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:id="@+id/dialog_canvas"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:gravity="center"android:orientation="vertical">
<TextViewandroid:text="You can use your widget here"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" />
</LinearLayout>- Create Class
CustomMyLibDialogExtendsMyLibDialogto your customDialogFragment. and inflate youlayoutononCreateView
CustomMyLibDialog.java
publicclassCustomMyLibDialogextendsMyLibDialog {
publicstaticCustomMyLibDialognewInstance() {
returnnewCustomMyLibDialog();
}
publicCustomMyLibDialog() {
}
@OverridepublicViewonCreateView(LayoutInflaterinflater, ViewGroupcontainer,
BundlesavedInstanceState) {
returninflater.inflate(R.layout.dialog, container, false);
}
@OverridepublicvoidonViewCreated(@NonNullViewview, @NullableBundlesavedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}- Custom Your Dialog
Here is some configuration that you can use. Use this configuration on onStart(). Optional you can use it or not.
publicclassCustomMyLibDialogextendsMyLibDialog {
...
publicCustomMyLibDialog() {
//super(R.style.CustomDialogStyle); // use this to change style . like Animation
}
@OverridepublicvoidonStart() {
super.onStart();
// disable cancelgetDialog().setCancelable(false);
// disable dismiss dialog out sidegetDialog().setCanceledOnTouchOutside(false);
// set dialog full screensetFullScreen(true);
// set canvas width, avaliable value 0.1 - 1.0setCanvasWidth(0.3);
// set BackButton to dismiss dialogenableBackButton(true);
//set gravity dialogsetGravity(Gravity.BOTTOM);
}
...
}- Custom your animation dialog show
Use super constructor in your constructor to change style
publicclassCustomMyLibDialogextendsMyLibDialog {
...
publicCustomMyLibDialog() {
super(R.style.CustomDialogStyle); // use this to change style . like Animation
}
...
}Here is the style
<resources>
<!-- res->styles.xml -->
<stylename="CustomDialogStyle"parent="Theme.MaterialComponents.Light.Dialog">
<itemname="android:windowMinWidthMajor">80%</item>
<itemname="android:windowMinWidthMinor">80%</item>
<itemname="android:windowEnterAnimation">@anim/anim_in</item>
<itemname="android:windowExitAnimation">@anim/anim_out</item>
</style>
</resources>Style that i prepare for you
- Change Corner
You can change corner on canvas. With this step.
publicclassCustomMyLibDialogextendsMyLibDialog {
...
@OverridepublicvoidonViewCreated(@NonNullViewview, @NullableBundlesavedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LinearLayout_dialogCanvas = view.findViewById(R.id.dialog_canvas);
_dialogCanvas.setBackground(requireActivity().getResources().getDrawable(R.drawable.rounded_corner));
_dialogCanvas.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(Viewv) {
getDialog().dismiss();
}
});
}
}To dismiss Dialog
getDialog().dismiss();- Example Corner
R.drawable.rounded_corner
<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" >
<cornersandroid:bottomLeftRadius="10dp"android:bottomRightRadius="10dp"android:topLeftRadius="10dp"android:topRightRadius="10dp" />
<solidandroid:color="@android:color/white" />
</shape>Other Example:
- Same Radius ->
R.drawable.rounded_cornerxmlPreview - Different Radius ->
R.drawable.rounded_corner_2xmlPreview - Dialog 3D ->
R.drawable.rounded_layerxmlPreview - Shadow Dialog ->
R.drawable.dialog_shadowxmlPreview
MainActivity.java
FragmentTransactiontransaction = getSupportFragmentManager().beginTransaction();
Fragmentprevious = getSupportFragmentManager().findFragmentByTag(CustomMyLibDialog.TAG);
if(previous != null){
transaction.remove(previous);
}
CustomMyLibDialogdialog = CustomMyLibDialog.newInstance();
dialog.show(transaction, CustomMyLibDialog.TAG);Preview :
![]() | ![]() |
|---|---|
Use setFullScreen(true) | Use setCanvasWidth(0.3) |
FullCode MainActivity & CustomMyLibDialog & XML
- 1.0.0
- First Release
- 1.0.1
- Add animation and set custom animation
- 2.0.0
- Support SDK 16
- 2.0.1
- Bug Fixing
- 2.1.0
- Gravity
You can sent your constibution to branchopen-pull.
Copyright 2020 M. Fadli Zein


