How to show an Activity as a dialog box rather than a new screen?

Viewed 1273

I am pretty new to this. I made 2 Activities. From Activity A I want to be able to open Activity B as a dialog window instead of it taking over the screen. I figured out how to make a dialog, but it seems its functionality needs to be programmed into the same Activity Java file. I want to keep my code separate.

Is there a way to show a dialog window that contains another activity along with all of it's coded functionality?

4 Answers

In your AndroidManifest manifest file define your activity like this :

<activity android:theme="@android:style/Theme.Dialog"
          android:excludeFromRecents="true" />

and If you want your activity to not be cancelable when the user clicks outside it just add the following line in your Activity.Java file inside onCreate method :

this.setFinishOnTouchOutside(false);

Try the following for the activity : In the activity,set the Theme by calling

setTheme(R.style.AppTheme_DialogTheme)

and create a corresponding style :

<style name="AppTheme.DialogTheme" 
parent="Base.Theme.AppCompat.Light.Dialog.FixedSize">
    <!-- removing the dialog's action bar -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowFixedHeightMajor">400dp</item>
    <item name="windowFixedHeightMinor">400dp</item>
</style>

 

Set you activity B theme from manifest.

<activity android:theme="@android:style/Theme.Dialog" />

Yes, you have to make some changes in the Manifest.

you can refer to this because your question has already been asked:

android activity as a dialog

Related