10

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 :-)

mark_w
  • 253
  • 1
  • 2
  • 9

2 Answers2

16

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
      }
    
Meenal
  • 2,879
  • 5
  • 19
  • 43
  • You can also use menu or action bar for making this kind of view instead of an activity – Meenal Aug 22 '13 at 06:17
  • Hi. Thanks for your answer :-) Unfortunately I can't get even the theme to work - it just says error no resource found... My min sdk is 8 and my target is 17 if that makes any difference. If I change the parent of the default AppBaseTheme there's no error but it doesn't work either. – mark_w Aug 22 '13 at 11:22
  • if you can provide your code or the error log,it will be much more helpful for me to get your problem. – Meenal Aug 22 '13 at 11:40
  • Hi. Unfortunately my dev computer is not connected to the Internet so I'm writing from my mobile. So I can't post any code. But it's okay - you've pointed me in the right direction and that's what matters... it's always the concepts I find hardest on android but once I've got the gist I get there eventually with the code. So I'll mark the question answered and thanks again for your help. – mark_w Aug 22 '13 at 12:32
  • 1
    In step 2 I would recommend you to use `android:theme="@android:style/Theme.Translucent.NoTitleBar"` because it keeps the system bar up. It won't change your screen size when showing/hiding the application. – Glogo Apr 06 '14 at 15:53
  • is this overlaying other apps on the screen, just like *always-on-top* inside windows pc? – gumuruh Apr 19 '22 at 14:31
4

You can use the below codes..it worked for me except it don't let apps to be installed from the internal storage..

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View oView = layoutInflater.inflate(R.layout.activity_transperant, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);        
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);
Xavi López
  • 27,550
  • 11
  • 97
  • 161
Slasher_bd
  • 61
  • 2
  • 14