Friday, February 22, 2019

Create transparent activity

We want an activity has transparent screen, user can touch icons behind it to run them.
Copy these lines in tag values, file styles.xml.
<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
Add to class declaration.

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

Now in class, copy to below setContentView();

WindowManager.LayoutParams wlp = getWindow().getAttributes();
          wlp.dimAmount = 0;
          wlp.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                     | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
          getWindow().setAttributes(wlp);

Activity now float in center screen, to make it on top, add this to above setContentView();
WindowManager.LayoutParams wmlp = getWindow().getAttributes();
          wmlp.width = LayoutParams.WRAP_CONTENT;

          wmlp.gravity = Gravity.TOP | Gravity.CENTER;


In some phones like Samsung, screen behind still can’t touch, we should use service if need to run always on screen.

No comments:

Post a Comment