28

I have 2 classes, FirstActivity and SecondActivity.

First Activity

Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);

Is it possible for SecondActivity to overlay on FirstActivity? ie. FirstActivity gets dimmed, SecondActivity gets displayed on top of FirstActivity.

If it is not possible for 2 different activities, is it possible to do an overlay for 2 views in the same activity? I hope using dialog is not the only option.

newbie
  • 958
  • 4
  • 13
  • 25

4 Answers4

27

I suggest you set your second activity up as a dialog -- which will dim the background. Here is a tutorial that could be helpful:

http://developer.android.com/guide/topics/ui/dialogs.html

http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application

Or you can simply set the theme in the manifest as a dialog for your SecondActivity.

Alan Moore
  • 6,525
  • 6
  • 55
  • 68
  • Yup, I know dialog is one option but are there better ways? In terms of UI, it doesn't look as nice. – newbie Oct 24 '11 at 15:54
  • Of course you can create a view for the content and adjust the alpha if you like. I'm not really sure what you're going for here, usually the FirstActivity would not be viewable at all since the SecondActivity would cover the screen? – Alan Moore Oct 24 '11 at 15:57
  • my second activity is more of like a `EditText` box so it will only cover half the screen. That is also why I am thinking of merging SecondActivity into FirstActivity and see if I can do a `View` overlay. SecondActivity is like a sub-activity and I want the users to get back into FirstActivity quickly – newbie Oct 24 '11 at 16:09
  • You can always create a custom dialog... An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). – JPM Oct 24 '11 at 16:16
  • 1
    Also, if you don't like the theming of the dialog, you can always create your own theme. – Alan Moore Oct 24 '11 at 17:10
  • I guess this is the best answer. Seems like no better way than to create a custom dialog – newbie Oct 24 '11 at 18:46
  • Let me know if you find something better :) Thanks! – Alan Moore Oct 24 '11 at 19:55
25

If you don't want to do a dialog, you can overlay views using a relative layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="some content"
            android:textSize="70dp"/>
    </LinearLayout>

    <LinearLayout android:id="@+id/overlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#99000000"
            android:clickable="true"
        android:visibility="gone">
        <EditText android:id="@+id/edittext"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="50dp" />
    </LinearLayout>

</RelativeLayout>

The first LinearLayout (id/content) is your base layout where your normal content would go.

The second LinearLayout (id/overlay) is your overlay layout which you'd want to show over top of the base layout. The background color will give give you that faded out background, and you can add whatever you want to that layout to make your overlay. To show the overlay, just change its visibility from gone to visible.

ImR
  • 787
  • 6
  • 8
  • Does it mean that buttons etc in the first linear layout will still be fully functional after the overlay layout is visible? – newbie Oct 24 '11 at 17:31
  • 4
    If you don't want the background to be clickable, you can make the overlay LinearLayout clickable. This will catch all clicks and prevent them from going through to the background while the overlay is visible. When you set the overlay's visibility to 'gone', clickability of the content layer will be restored. I updated the code sample to show this. – ImR Oct 25 '11 at 16:09
  • Your solution worked like a charm to me. I only want to say that, if you want more background colors to test (the opacity codes), you can see this good answer: http://stackoverflow.com/a/16890937/4744263 – Filipe Brito Dec 18 '16 at 19:03
7

In manifest file declare the secondactivity activity like this. android:theme="@android:style/Theme.Dialog".then simply call the secondactivity from firstactivity from your code.

 <activity
                android:name=".FirstActivity"
                android:label="@string/title_activity_first" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".SecondActivity"
                android:label="@string/title_activity_second" 
                android:theme="@android:style/Theme.Dialog"
                >
                <intent-filter>
                    <action android:name="transparent.text.SECONDACTIVITY"/>
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>

Second Activity xml file.you can design as your wish but for reference i have posted this.the key concept is in manifestfile (ie) how to define your secondactivity in manifest

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="192dp"
            android:background="#aabbcc"
            android:text="Sybrant has provided Takoma with a great team which helped us from the beginning to the final stage of our product, to our fullest satisfaction. We have been able to deliver a high quality of eLearning products to our corporate customers like Nissan with Sybrant’s support”"
            tools:context=".FirstActivity" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="43dp"
            android:layout_marginLeft="80dp"
            android:text="Button" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/textView1"
            android:layout_marginRight="42dp"
            android:layout_marginTop="80dp"
            android:text="TextView" />
    
    </RelativeLayout>
Community
  • 1
  • 1
Ruban
  • 1,514
  • 2
  • 14
  • 21
-17

1-Take a screenshot of the first activity.

2-(Optional) Darken, tint or blur the screenshot.

3-Then call the second activity and use the first activity screenshot as background for the second activity.

  • 20
    That's harsh, but I laughed – Maxime Tremblay-Savard Jul 17 '16 at 17:23
  • 3
    This is actually a very good strategy in games when you want to open a full screen menu with background partially visible.. massive draw call savings! – 5argon Aug 31 '18 at 13:49
  • 1
    @5argon Well that really depends on what your game looks like. Still - doing it in mobile app? You still need to store that image somewhere, probably in memory? How do you handle orientation change, do you take two screenshots? Overall I think there are better ways of handling these cases than taking screenshots. – Jiri Kralovec Apr 01 '21 at 14:21