Draw Over Screen App Example Android
We always interact with some app which are drawn over screen. Facebook Messenger is one of the good example of that. When you receive a new message it pops up over screen even if Messenger App is closed.
So how to achieve this.
This required very basic setup in your application.
We are taking targetSdkVersion 21 in build.gradle to avoid Runtime Permission implementation of SDK 23.
Step 1 :- Give permission in your manifest file i.e.
android.permission.SYSTEM_ALERT_WINDOW
So how to achieve this.
This required very basic setup in your application.
We are taking targetSdkVersion 21 in build.gradle to avoid Runtime Permission implementation of SDK 23.
Step 1 :- Give permission in your manifest file i.e.
android.permission.SYSTEM_ALERT_WINDOW
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Step 2:- Then you have create your layout for overdraw window
We are showing a button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Step 3 :- Here comes you java code.
WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); final View myView = inflater.inflate(R.layout.item_dialog, null); myView.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ScreenShotService.this, "Clicked", Toast.LENGTH_SHORT).show(); wm.removeViewImmediate(myView); } });wm.addView(myView,params);
You can put this code in Activity, Service where you want to show this Popup like window.
Its all done you can check this by simply putting in onCreate method of an activity.
You can verify that your app is taking overdrawn permission by going in Setting>Apps>Permission>Gear>Overdrawn and you will find your app is listed their and having overdrawn permission.
Hope you will got basic walk through for achieving App Over Drawn feature of Android.
Happy Coding!!!
Comments
Post a Comment