It’s best to create a View that fills the screen, but has a transparent background everywhere except for the part you want.
This can be done by specifying a custom theme in the res/values/styles.xml (initially shown on How to create transparent activity on Android? ):
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.Transparent" parent="android:Theme"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:backgroundDimEnabled">false</item> </style> </resources>
Then by adding this theme to your Activity manifest manifest:
android:theme="@style/Theme.Transparent"
You can then create a layout ( R.layout.lockscreen ) for this action by adding this XML file. Here you specify the height in pixels:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <FrameLayout android:layout_width="match_parent" android:layout_height="200px" android:centerInParent="true" > <include layout="@layout/test" /> </FrameLayout> </RelativeLayout>
Finally, just use the first method shown above to show the Activity:
super.onCreate(savedInstanceState); Window window = getWindow(); window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED); setContentView(R.layout.lockscreen);
source share