0

I want to implement a second activity that is on top of the first one, with the first one still visible. But there's no real good documentation about it.

I already made the second activity and used

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

in the manifest but this shows only the name of the app without the content.

I made sketch of what I mean.

Sketch of layout

EDIT: the solution was to add a theme to the styles.xml

  <style name="CustomDialog" parent="Base.Theme.AppCompat.Light.Dialog.MinWidth">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

and to specify the width and height in dp in the second activity xml file.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Wouter Vandenputte
  • 1,948
  • 4
  • 26
  • 50

4 Answers4

3

You have to do a few things to be able to do this:

First make a custom style for your Activity.

   <style name="AppDialogTheme" parent="@style/AppTheme.NoActionBar">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

Then add the newly created style to the Android Manifest:

<activity
      android:name="Activity2"
      android:theme="@style/AppDialogTheme" />

And finally create your layout by making the parent ViewGroup for example a FrameLayout set the background also to @android:color/transparent. Now include your layout inside the FrameLayout and add the padding you want to get that Dialog look.

Hope that helps!

Alex
  • 962
  • 4
  • 15
2

Styles

<style name="DialogActivity" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Manifest

  <activity
        android:name="MyActivity"
        android:excludeFromRecents="true"
        android:theme="@style/DialogActivity" >

This will work exactly the way you want.

Taseer
  • 3,432
  • 3
  • 16
  • 35
0

You cannot have multiple activities running at the same time, so Why not use custom dialog?

For example, create dialogClass:

public class ProgressDialog extends Dialog {

public ProgressDialog(@NonNull Context context) {
    super(context);

    setContentView(R.layout.progress_dialog); //this is your layout for the dialog

    }
}

And all you need to do is to create dialog instant and call it like this:

ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.show(); // this line shows your dialog

And you will have your custom layout over your activity.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • Are you sure you can't run multiple activities at the same time? Because in one my app, I am running 2 activities together – Taseer Jul 23 '19 at 03:29
  • The code I shared above is how my second activity looks like. First activity calls some method and the method then opens the second activity on top of first activity – Taseer Jul 23 '19 at 10:25
  • You can have more than one activity visible at the same time. Only one activity will be however in RUNNING state the other visible but in background will be in PAUSE state. Check documentation https://developer.android.com/guide/components/activities/activity-lifecycle for more details – tuxdost Apr 08 '21 at 17:15
  • The dialog will get dismissed when the other activity launches – Oush Oct 06 '22 at 06:16
0

Open Another view on Activity We have used Dialogs , You can use dialog with custom layout Like this:

  final Dialog dialog=new Dialog(FirstActivity.this);
            dialog.setContentView(R.layout.dialog_custom);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
           LinearLayout 
  rebooking_back_button_ll=dialog.findViewById(R.id.rebooking_back_button_ll);

           rebooking_back_button_ll.setOnClickListener(
              new View.OnClickListener(){
                 @Override
                 public void onClick(View v){
                    dialog.dismiss();
                 }
              });
            dialog.show();

dialog_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@android:color/transparent"
android:orientation="vertical"
app:layout_constraintHeight_max="wrap">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/title"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="Custom Activty"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="14sp" />

    <LinearLayout
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/back_ll"
        android:layout_below="@+id/content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/rebooking_single_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:text="book a single appointment"
            android:textAlignment="center"
            android:textColor="#9C27B0"
            android:textSize="16sp"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:gravity="center">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Go next"/>
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/end_time_recycle"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#d9d8d8" />
        <TextView
            android:id="@+id/rebooking_recurring_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:text="Check Balance"
            android:textAlignment="center"
            android:textColor="#9C27B0"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/back_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/end_time_recycle"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#d9d8d8" />
        <LinearLayout
            android:id="@+id/rebooking_back_button_ll"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#FF5722"
            android:gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="Back"
                android:textColor="@android:color/white"
                android:textSize="17sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
   </LinearLayout>
 </RelativeLayout>

enter image description here

Android Geek
  • 8,956
  • 2
  • 21
  • 35