Android Activity as a dialog

后端 未结 10 2390
轮回少年
轮回少年 2020-11-22 02:31

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 d

相关标签:
10条回答
  • 2020-11-22 03:08

    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">
    
    0 讨论(0)
  • 2020-11-22 03:08

    Create activity as dialog, Here is Full Example

    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>
      
    0 讨论(0)
  • 2020-11-22 03:10

    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.

    0 讨论(0)
  • 2020-11-22 03:15

    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.

    0 讨论(0)
  • 2020-11-22 03:17

    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>
    
    0 讨论(0)
  • 2020-11-22 03:26

    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

    0 讨论(0)
提交回复
热议问题