9

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

Milo
  • 1,017
  • 3
  • 16
  • 22

3 Answers3

10

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); 
    }
Community
  • 1
  • 1
yyunikov
  • 5,719
  • 2
  • 43
  • 78
1

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">
Oli
  • 3,496
  • 23
  • 32
  • I have tried this and it has no effect on my Nexus 10. My Manifest: Styles.xml – Milo May 26 '13 at 11:38
  • Also you should check this [link](http://developer.android.com/guide/topics/ui/themes.html) – Oli May 26 '13 at 11:50
  • Thanks, android:Theme.Holo.Dialog did the trick. Not sure why, perhaps I was overriding other styles somewhere else. However, I dont seem to be able to use the ActionBar using that Theme. I get a nullpointer calling: actionBar.setDisplayHomeAsUpEnabled(true); – Milo May 26 '13 at 11:50
  • I edited my previous comment, I just realised I cannot use ActionBar when using the android:Theme.Holo.Dialog theme, any ideas? – Milo May 26 '13 at 11:57
  • Check my answer please, about the action bar. – yyunikov May 26 '13 at 11:57
  • Please try this solution and add the parameters to your layout [Link](http://stackoverflow.com/questions/11425020/actionbar-in-a-dialogfragment) – Oli May 26 '13 at 11:59
  • Still the issue with null pointer being thrown when accessing the ActionBar. I may have to look into yyunikov suggestion of a custom Dialog? – Milo May 26 '13 at 12:02
1

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>
Max
  • 5,733
  • 4
  • 30
  • 44