Milo
Milo

Reputation: 1017

Display Activity as overlay window on tablets

How do you present an Activity as an overlay window on tablets? An example of this is the new Google+ app as seen here:

enter image description here

Importantly I want the ActionBar to be part of the window and for the Activity beneath to be dimmed as seen in the screenshot.

Thanks

Upvotes: 9

Views: 4766

Answers (3)

Max
Max

Reputation: 5743

You need to extent your activity theme with

Theme.AppCompat.Light.DialogWhenLarge

or

Theme.Holo.DialogWhenLarge

Here is an example

<style name="AppTheme.DialogActivity" parent="Theme.AppCompat.Light.DialogWhenLarge">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <!-- Your theme here -->
</style>

Upvotes: 1

yyunikov
yyunikov

Reputation: 5907

You can just use dialog theme. To do this, just write in Manifest:

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

or

android:theme="@android:style/Theme.Holo.Dialog"

or just by creating your own theme in styles.xml:

<style name="MyDialogTheme" parent="Theme.Holo.Dialog">
...
</style>

You can set such theme for xlarge or large screen by creating styles.xml in values-xlarge or values-large folders.

If you want to set this theme only for tablets, then you can change theme dynamically by checking the screen size like this:

if (Configuration.SCREENLAYOUT_SIZE_XLARGE)
{
//setTheme(yourDialogTheme);
}

Please check this answer if you want dialog with action bar. You can do this by creating your custom dialog.

Dialog themed activity with action bar

Custom dialog

EDIT: An answer from google group post. Try this in your xml with styles:

<style name="PopupTheme" parent="android:Theme.Holo.Light.Dialog">
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
        <item name="android:windowActionModeOverlay">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

In Java code

public static void showAsPopup(Activity activity) {
        //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest
        activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
        activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
                WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        LayoutParams params = activity.getWindow().getAttributes(); 
        params.height = LayoutParams.FILL_PARENT;
        params.width = 850; //fixed width
        params.alpha = 1.0f;
        params.dimAmount = 0.5f;
        activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 
    }

Upvotes: 10

Oli
Oli

Reputation: 3536

You should use the Theme.Dialog in you Manifest.xml for the Activity

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

for future use you should use an CustomTheme in you values/values-11/values-14->styles.xml (EDIT)

EDIT:

         <activity 
             android:name="com.apps.ActivityP" 
             android:theme="@style/CustomTheme"/> 

in you values styles.xml folder

<style name="CustomTheme" parent="android:Theme.Black">

for example you values-11/14 styles.xml folder

<style name="CustomTheme" parent="android:Theme.Holo.Dialog">

Upvotes: 1

Related Questions