Search code examples
androidandroid-layoutandroid-activity

Display new Activity on top of previous Activity but only partially cover the previous Activity


Flow of desired behaviour

I have an Activity A which has a "down" button (see image above). When I press this button, I want to launch an Activity B on top of Activity A such that some of Activity A at the bottom is still visible. Activity B can in turn launch a number of other Activities (C, D etc) such that they only display within the same screen area as B. Does anybody know whether this is possible and, if so, how I can go about it?

Edit

I am aware that I can add android:theme="@android:style/Theme.Dialog" to my Activity's manifest declaration. However, as far as I am aware, this displays in the centre of the screen and not such that I can resize it how I require. Correct me if I am wrong 🙏


Solution

  • This is the XML for activities that should be on top of Activity A:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#00000000" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0"
            android:layout_weight="0.6"
            android:background="#000000" >
            //use this for your new activity contents
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0"
            android:layout_weight="0.4"
            android:background="#00000000" >
            //this is empty
        </LinearLayout>
    </LinearLayout>
    

    Now, in your manifest for Activities B, C etc:

    <activity
        android:label="@string/app_name"
        android:name=".ActivityB"
        android:theme="@style/Theme.Transparent"
        android:screenOrientation="portrait" >
    

    In values/styles.xml:

    <?xml version="1.0" encoding="utf-8"?>  
    <resources>
        <style name="Theme.Transparent" parent="android:Theme.NoTitleBar">  
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:windowBackground">@drawable/theme_transparent</item>
        </style>
    </resources>  
    

    Finally, in drawable/theme_transparent.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <solid android:color="#00000000" ></solid>
    </shape>