Custom dialog using dialog fragments

Example for dialog is to show logout confirmation dialog
class MessageDialog() : DialogFragment() {
}
override fun onStart() {
super.onStart()
dialog?.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
}
<style name="custom_dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
</style>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.custom_dialog);
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/transparent">

<androidx.cardview.widget.CardView
android:id="@+id/dialog_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
app:cardBackgroundColor="@color/white_black"
app:cardCornerRadius="8dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="8dp">
<!-- put your view here-->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
final override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE);
return inflate(R.layout.my_custom_layout,container,inflater,context)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//if you were setting arguments
//firstNonNull(savedInstanceState,arguments)?.let {
//initData(it)
//}
initViewModel()

//
initViews()
}

--

--

--

I’m Islam Assem, Android developer with 2+ years experience in android development building robust apps and writing clean code following design patterns

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cleaner Previews in Compose with PreviewParameter

Convert JSON to Data Classes Using a Nice Android Studio Plugin

Android 开发技术周报 Issue#293

Common Mistakes as a beginner Android Developer

Automated Accessibility Testing Using Accessibility Test Framework In Android

Jetpack Compose Weekly Issue#1

Get started with the Navigation component — Beginner In-Depth Guide

Android Widgets in 2020 📱🤖

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Islam Assem

Islam Assem

I’m Islam Assem, Android developer with 2+ years experience in android development building robust apps and writing clean code following design patterns

More from Medium

Setup Firebase for Android development [2022]

Top 7 Tips for How to Develop Android Apps Better.

How to Develope Android Apps Better

Audio mixing on Android

Kickstarting Ads Kit by integrating Interstitial Ads in Application (Kotlin)