Adil Hussain
Adil Hussain

Reputation: 32103

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 🙏

Upvotes: 2

Views: 5712

Answers (3)

Imre
Imre

Reputation: 504

Regarding your "Edit", actually, you can move around / resize a Dialog themed activity. For example, to make your Dialog themed activity appear at the top of the screen instead of the default center and be 80% width of the screen, just add the following after setContentView in your Dialog themed activity:

Window window = getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP;
window.setAttributes(wlp);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = (int) (metrics.widthPixels * 0.80);
window.setLayout(screenWidth, WindowManager.LayoutParams.WRAP_CONTENT);

Upvotes: 0

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

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>

Upvotes: 5

Sander van&#39;t Veer
Sander van&#39;t Veer

Reputation: 5980

What you need to use, are Fragments.

Fragments can be used to fill a part of the screen, while doing something else entirely in a different one. In your example you can create a main activity that contains two Fragments. One Fragment controls the "A activity" , the other one controls the "B activity" and the other ones.. By replacing the current Fragment in your "B activity" area with a different one on the press of a button, you can achieve the behavior you are looking for. At least, that's how I did it in an app of mine containing a main content area and a music player. The music player stays in place while the main content changes.

Sadly I can't provide any example code right now, but here is a tutorialthat should help you get started:

In case you are working with an Android version below 3.0, you can use the Compatibility package.

http://developer.android.com/sdk/compatibility-library.html

http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

Upvotes: 3

Related Questions