[android] Android Activity as a dialog

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

This question is related to android android-activity android-dialog

The answer is


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>
    

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


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

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>

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.


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)


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


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.


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

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-activity

Kotlin Android start new Activity The activity must be exported or contain an intent-filter How to define dimens.xml for every different screen size in android? Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which? Not an enclosing class error Android Studio java.lang.IllegalStateException: Fragment not attached to Activity Soft keyboard open and close listener in an activity in Android android.app.Application cannot be cast to android.app.Activity Android Shared preferences for creating one time activity (example) Android ListView with onClick items

Examples related to android-dialog

How to make custom dialog with rounded corners in android How to create a number picker dialog? How to create a Custom Dialog box in android? How to set DialogFragment's width and height? Dialog with transparent background in Android How to dismiss the dialog with click on outside of the dialog? Android DialogFragment vs Dialog Android 'Unable to add window -- token null is not for an application' exception Show dialog from fragment? Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"