How to turn Android Activity to Dialog
A dialog in android is a small window which prompts the user to add additional information or make a decision. Android provides different dialog classes to implement this functionality.
Dialogs can also be displayed using an activity as a dialog. This is useful when apps are designed for small screens. Also, it is super easy to implement.
Follow the below steps to show an activity as a dialog screen.
Step1: Create custom theme as below. parent theme can be Appcompat dialog or MaterialComponents dialog (Theme.MaterialComponents.Dialog).
<style name="Theme.AddCategory" parent="Theme.AppCompat.Dialog">
<item name ="title">"Add Category" </item>
</style>
Step2: Activity which shows up as dialog should have the above custom theme set as below, in its onCreate block. Hide title bar for the dialog using Window.FEATURE_NO_TITLE.
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Theme_AddCategory);
super.onCreate(savedInstanceState)
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
val binding = ActivityAddCategoryBinding.inflate(layoutInflater)
setContentView(binding.root)
Result will look something like below.
PS: Google developer document asks to use "Theme.Holo.DialogWhenLarge" theme to the activity in manifest file. But, I was getting the error - "java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity." Hence I used above approach. Hope this helps.