Overlay an activity on another activity OR overlay a view over another

62,384

Solution 1

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.

Solution 2

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.

Solution 3

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>
Share:
62,384
newbie
Author by

newbie

Newbie in Android development. Please guide me along!

Updated on July 31, 2020

Comments

  • newbie
    newbie almost 4 years

    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.