Android Activity as a dialog

AndroidAndroid ActivityAndroid Dialog

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

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">

Solution 4 - Android

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>

Solution 5 - Android

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)

Solution 6 - Android

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.

Solution 7 - Android

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

Solution 8 - Android

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>
    

Solution 9 - Android

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);

Solution 10 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiond-manView Question on Stackoverflow
Solution 1 - Androidd-manView Answer on Stackoverflow
Solution 2 - AndroidMaverickView Answer on Stackoverflow
Solution 3 - AndroidherbertDView Answer on Stackoverflow
Solution 4 - AndroidneferpitouView Answer on Stackoverflow
Solution 5 - AndroidM. Usman KhanView Answer on Stackoverflow
Solution 6 - AndroidaaronsnoswellView Answer on Stackoverflow
Solution 7 - AndroidIsmail IqbalView Answer on Stackoverflow
Solution 8 - AndroidSanjayrajsinhView Answer on Stackoverflow
Solution 9 - AndroidGyan Swaroop AwasthiView Answer on Stackoverflow
Solution 10 - AndroidGyan Swaroop AwasthiView Answer on Stackoverflow