Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

Draw Over Other Apps Permission in Android

Draw Over Other Apps Permission in Android





In this post, we will learn how to request runtime permission of Draw over Other Apps.

By turning on this permission, we will show our app icon or perform any task from our app to overlay on other apps. 

The best Example for this is, Facebook chat Heads, for showing facebook chat heads from messenger, we have to give permission of draw over other apps. 

Preview:

Draw Over Other Apps Runtime Permission Android

Draw Over Other Apps Runtime Permission Android


Runtime Permission request is used from Android Marshmallow(6.0), before Android M this permission given by default.

To request other permissions, you can see this post, but for Draw over other apps permission, we will change our request criteria to get this permission.

So, Let's Start,

Write permission in AndroidManifest.xml
 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  

After that, we will create a button in our XML file to check permission and also we created a TextView to set the text of permission granted or not.

XML activity
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   tools:context=".NextActivity">  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/textview_next"  
     android:text="Permission"  
     android:textSize="16dp"  
     android:padding="20dp">  
   </TextView>  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/checkbutton"  
     android:text="Check Pemission"  
     app:layout_constraintTop_toBottomOf="@id/textview_next">  
   </Button>  
 </LinearLayout>  

After that, in our java file we will request our permission and also we check if our app runs on greater than Android M devices or not.

Java Activity
 package studio.harpreet.sampleproject;  
 import android.content.Intent;  
 import android.content.SharedPreferences;  
 import androidx.appcompat.app.AppCompatActivity;  
 import android.net.Uri;  
 import android.os.Build;  
 import android.os.Bundle;  
 import android.provider.Settings;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 public class NextActivity extends AppCompatActivity {  
   TextView tv;  
   Button btn;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_next);  
     tv = findViewById(R.id.textview_next);  
     btn = findViewById(R.id.checkbutton);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
           if(Settings.canDrawOverlays(NextActivity.this))  
           {  
             tv.setText("Permission Granted");  
           }  
           else  
           {  
             Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"+getPackageName()));  
             Toast.makeText(NextActivity.this, "Please Give Permission to Draw over other apps", Toast.LENGTH_SHORT).show();  
             startActivity(intent);  
           }  
         }  
       }  
     });  
   }  
 }  

Where your app used to draw overlays, check if permission requested or not to avoid Exceptions.
 


Follow us for more posts like this, 
Subscribe to Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments