279

I have an Activity named whereActity which has child dialogs as well. Now, I want to display this activity as a dialog for another activity.

How can I do that?

enter image description here

forivall
  • 9,504
  • 2
  • 33
  • 58
d-man
  • 57,473
  • 85
  • 212
  • 296
  • If you just want loading dialog, you can use [ProgressDialog](https://developer.android.com/reference/android/app/ProgressDialog.html) – Daksh Agrawal Nov 09 '17 at 13:26

10 Answers10

578

To start activity as dialog I defined it like this in AndroidManifest.xml:

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

Use this property inside your activity tag to avoid that your Dialog appears in the recently used apps list

android:excludeFromRecents="true"

If you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog:

After setContentView() in your Activity use:

this.setFinishOnTouchOutside(false);

Now when I call startActivity() it displays as a dialog, with the previous activity shown when the user presses the back button.

Note that if you are using ActionBarActivity (or AppCompat theme), you'll need to use @style/Theme.AppCompat.Dialog instead.

HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
d-man
  • 57,473
  • 85
  • 212
  • 296
  • 189
    Using the android:theme="@android:style/Theme.Dialog" is the way to go but don't forget to use excludeFromRecents=true or else your dialog will appear in the Recently Used Apps (hold the Home key). – Emmanuel Nov 25 '10 at 14:22
  • 3
    I've also had difficulty with the size of the dialog if using LinearLayout as the root layout. Using RelativeLayout helps if you're going to be setting requestWindowFeature(Window.FEATURE_NO_TITLE) or getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert) – dell116 Dec 30 '11 at 05:40
  • 7
    i have difficulty related to the activity as dialog.. i made the activity as dialog but the problem is when i click outside the the activity it automatically get closed..??? is there any solution related to this..?????? – Wolverine Apr 03 '12 at 04:42
  • 7
    @Emmanuel, it has to be android:excludeFromRecents="true" note the double quotes – Piyush-Ask Any Difference Mar 10 '13 at 08:37
  • 1
    I want to add additional codes for who want activity dialog more wider.. Add this after setContentView() , getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); – lynndragon May 15 '14 at 09:20
  • Can we also embed ActionBar using this way ? – Salman Khakwani Jun 02 '14 at 10:44
  • 45
    Note that if you are using ActionBarActivity (or AppCompat theme), you'll need to use `@style/Theme.AppCompat.Dialog` – Quentin Klein Feb 12 '15 at 14:08
  • 8
    @Wolverine, Add `this.setFinishOnTouchOutside(false);` after `setContentView()` method to prevent the activity from being destroyed when you click outside the dialog you created. – Anggrayudi H Mar 21 '15 at 08:27
  • 1
    error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.Dialog'). – Prasad Jun 02 '15 at 11:32
  • @QuentinKlein lets say we are an sdk, projects import us as a library, how we can know if they are using actionbaractivity or appcompat activity, thanks :) – Mike Jun 19 '16 at 08:34
  • @Mike I don't think you can know that, but I'll advise always using AppCompat because it is retroactive and can work for everyone, but the other choice won't – Quentin Klein Jun 27 '16 at 07:48
  • @QuentinKlein I can not find this theme you suggest: `@style/Theme.AppCompat.Dialog` – Sayed Abolfazl Fatemi Sep 29 '16 at 08:28
  • 1
    @SayedAbolfazlFatemi note that you have to import Support Libraries v7 `com.android.support:appcompat-v7:24.2.1` (https://developer.android.com/topic/libraries/support-library/features.html) – Quentin Klein Oct 03 '16 at 08:15
  • Screen rotation has no effect. However I've different layouts in layout and layout-land folders. – Brijesh Kumar Nov 22 '16 at 11:32
  • Is it possible to send the data from Dialog Activity to the Parent Activity when i close the Dialog Activity. – SH7 Oct 03 '18 at 19:12
  • Is this how logging in on to Google Games work for leaderboards and achievements? How secure is this for passing sensitive information between apps? – rraallvv Apr 15 '19 at 08:42
45

Use this code so that the dialog activity won't be closed when the user touches outside the dialog box:

this.setFinishOnTouchOutside(false);

requires API level 11

nickgrim
  • 5,387
  • 1
  • 22
  • 28
Maverick
  • 961
  • 9
  • 13
  • 1
    Is there any solution for API < 11 ? – PK Gupta Jan 03 '16 at 19:57
  • @pkgupta, if it is acceptable to you, you should not worry at least API <15 for me since there is not much people using those earlier versions anyway. In fact, as of 2017, I think 19 or 20 is the new minSDK. – Neon Warge Jun 27 '17 at 14:18
32

You can define this style in values/styles.xml to perform a more former Splash :

   <style name="Theme.UserDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@drawable/trans</item>
    </style>

And use it AndroidManifest.xml:

   <activity android:name=".SplashActivity"
          android:configChanges="orientation"
          android:screenOrientation="sensor"
          android:theme="@style/Theme.UserDialog">
herbertD
  • 10,657
  • 13
  • 50
  • 77
26

If you need Appcompat Version

style.xml

    <!-- Base application theme. -->
    <style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <!-- Customize your theme here. -->
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

yourmanifest.xml

    <activity
          android:name=".MyActivity"
          android:label="@string/title"
          android:theme="@style/AppDialogTheme">
    </activity>
neferpitou
  • 1,642
  • 2
  • 20
  • 26
  • This is great. I needed to remove the action bar. I had to modify the XML slightly though. I had to add `true` to make it work for me. I also added `false` for good measure. – Eric Mar 06 '16 at 23:50
  • 6
    Also for me `true`did not work (Android 5.0.1), and I had to change to `true` – noti Jul 10 '16 at 08:26
23

1 - You can use the same activity as both dialog and full screen, dynamically:

Call setTheme(android.R.style.Theme_Dialog) before calling setContentView(...) and super.oncreate() in your Activity.

2 - If you don't plan to change the activity theme style you can use

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

(as mentioned by @faisal khan)

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
  • Just a note: when I use the programmatic method (the first method), everything behind the dialog box is blacked out completely; this was not an issue in the other method though. Too bad...I really wanted to do it programmatically....Nexus 5, Android 6.0.1 – Eric Mar 06 '16 at 23:42
  • @Eric did you find solution for blacked out?? – anand Feb 13 '18 at 12:26
  • 1
    @anand yes, combining both solutions together fixes the blackout issue: set theme of activity to a dialog theme in android manifest (``) and programmatically set theme of activity to a dialog or an activity in onCreate https://stackoverflow.com/a/35915764/2898715 – Eric Feb 13 '18 at 18:47
17

If your activity is being rendered as a dialog, simply add a button to your activity's xml,

<Button
    android:id="@+id/close_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Dismiss" />

Then attach a click listener in your Activity's Java code. In the listener, simply call finish()

Button close_button = (Button) findViewById(R.id.close_button);
close_button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});

That should dismiss your dialog, returning you to the calling activity.

aaronsnoswell
  • 6,051
  • 5
  • 47
  • 69
9

If you want to remove activity header & provide a custom view for the dialog add the following to the activity block of you manifest

android:theme="@style/Base.Theme.AppCompat.Dialog"

and design your activity_layout with your desired view

Ismail Iqbal
  • 2,774
  • 1
  • 25
  • 46
4

Create activity as dialog, Here is Full Example

enter image description here

  1. AndroidManife.xml

    <activity android:name=".appview.settings.view.DialogActivity" android:excludeFromRecents="true" android:theme="@style/Theme.AppCompat.Dialog"/>

  2. DialogActivity.kt

    class DialogActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dialog)
        this.setFinishOnTouchOutside(true)
    
        btnOk.setOnClickListener {
          finish()
        }
      }
    }
    
  3. activity_dialog.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#0072ff"
    android:gravity="center"
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="@dimen/_300sdp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/txtTitle"
            style="@style/normal16Style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:text="Download"
            android:textColorHint="#FFF" />
    
        <View
            android:id="@+id/viewDivider"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#fff"
            android:backgroundTint="@color/white_90"
            app:layout_constraintBottom_toBottomOf="@id/txtTitle" />
    
        <TextView
            style="@style/normal14Style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:text="Your file is download"
            android:textColorHint="#FFF" />
    
    
        <Button
            android:id="@+id/btnOk"
            style="@style/normal12Style"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/circle_corner_layout"
            android:text="Ok"
            android:textAllCaps="false" />
        </LinearLayout>
    
      </LinearLayout>
    
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Sanjayrajsinh
  • 15,014
  • 7
  • 73
  • 78
2

Set the theme in your android manifest file.

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

And set the dialog state on touch to finish.

this.setFinishOnTouchOutside(false);
2

Some times you can get the Exception which is given below

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

So for resolving you can use simple solution

add theme of you activity in manifest as dialog for appCompact.

android:theme="@style/Theme.AppCompat.Dialog"

It can be helpful for somebody.