title: How To Display A Floating Icon In Home Screen
body: <p>I am building a lock screen app. I want to place a floating icon on Homescreen, which when clicked will lock the phone. I was able to implement the functionality of Locking the Phone using Device Manager API. Now I want to add a Floating Icon on Homescreen.</p>
  <p>I tried using the solution recommended here:
  <a href="https://stackoverflow.com/questions/15975988/what-apis-are-used-to-draw-over-other-apps-like-facebooks-chat-heads">What APIs are used to draw over other apps (like Facebook&#39;s Chat Heads)?</a></p>
  <p>But this does not seem to be working in my case. It is not diplaying an icon on the homescreen ever after gaining SYSTEM OVERLAY Permission.</p>
  <p><strong>LockButtonService.java</strong></p>
  <pre><code>public class LockButtonService extends Service {
  
      private WindowManager windowManager;
      private ImageView chatHead;
  
      @Override public IBinder onBind(Intent intent) {
          // Not used
          return null;
      }
  
      @Override public void onCreate() {
          super.onCreate();
  
          windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  
          chatHead = new ImageView(this);
          chatHead.setImageResource(R.drawable.lock);
  
          WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                  WindowManager.LayoutParams.WRAP_CONTENT,
                  WindowManager.LayoutParams.WRAP_CONTENT,
                  WindowManager.LayoutParams.TYPE_PHONE,
                  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                  PixelFormat.TRANSLUCENT);
  
          params.gravity = Gravity.TOP | Gravity.LEFT;
          params.x = 0;
          params.y = 100;
  
          windowManager.addView(chatHead, params);
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          if (chatHead != null) windowManager.removeView(chatHead);
      }
  }
  </code></pre>
  <p><strong>MainActivity.java</strong></p>
  <pre><code>add_homescreen.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  //Code For Adding Shortcut Icon On Home Screen
  //                if (ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext())) {
  //                    ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(getApplicationContext(), &quot;#1&quot;)
  //                            .setIntent(new Intent(getApplicationContext(), LockActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
  //                            .setShortLabel(&quot;Lock Screen&quot;)
  //                            .setIcon(IconCompat.createWithResource(getApplicationContext(), R.drawable.lock))
  //                            .build();
  //                    ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcutInfo, null);
  //                } else {
  //                    Toast.makeText(MainActivity.this,&quot;launcher does not support short cut icon&quot;,Toast.LENGTH_LONG).show();
  //                }
  
                      //Code For Requesting System Overlay
  
  
                  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                      if (!Settings.canDrawOverlays(getApplicationContext())) {
                          Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse(&quot;package:&quot; + getPackageName()));
                          startActivityForResult(intent, 12345);
                      }
                      else {
                          //Code To Execute If The Permission Is Granted
                          Toast.makeText(getApplicationContext(),&quot;Permission Granted&quot;,Toast.LENGTH_LONG);
                         startService(new Intent(getApplicationContext(), LockButtonService.class));
                      }
                  }
  
  
  
  
              }
  </code></pre>