How do you create a transparent activity that can overlay the home screen but is not dismissed when the home or back button is pressed?

mark_w picture mark_w · Aug 22, 2013 · Viewed 22.5k times · Source

In my app I'm looking to show an always-visible, semi-transparent status but am having a hard time figuring out how it is done.

Facebook Messenger and a few other apps I've seen do it so I know it's possible. They use the SYSTEM_ALERT_WINDOW permission to show a mostly transparent activity or dialog 'always-on-top'.

But what I don't understand is how they make it so that they are not closed when the back or home button is pressed? In other words they don't appear to behave like activities at all but I don't see what else they could be?

Any help here would be very much appreciated :-)

Answer

Meenal picture Meenal · Aug 22, 2013

You can create a transparent activity with the help of

  1. Make the background of layout in your xml file transparent by using

    android:background="@android:color/transparent

  2. And also, make the theme in your manifest file transparent for that particular activity

    <activity android:name="Your activity" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"> </activity>

  3. And for back press override the onBackPressed() method and remove super.onBackPressed()

    @Override
     public void onBackPressed()
      {
        // TODO Auto-generated method stub
      }