3

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?

Ali
  • 3,346
  • 4
  • 21
  • 56
Ben Mora
  • 279
  • 3
  • 16

4 Answers4

4

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);
Michael
  • 411
  • 2
  • 6
  • 15
  • Was I correct in assuming that I should add to the already existing activity tag? – Ben Mora Sep 24 '20 at 14:47
  • 1
    I got a crash, but turns out I guess I have to use "Theme.AppCompat.Dialog" instead of "Theme.Dialog." I don't like that it made it a dark theme though. I'll have to play around with it. – Ben Mora Sep 24 '20 at 14:53
  • yes sir using Theme.AppCompat.Dialog will work fine for you, if you don't like the dark theme you can try another one – Michael Sep 24 '20 at 16:21
3

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>

 
akash rao
  • 59
  • 1
  • 4
0

Set you activity B theme from manifest.

<activity android:theme="@android:style/Theme.Dialog" />
Asad khan
  • 156
  • 7
0

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

Twelve
  • 51
  • 3