Created
September 11, 2015 20:17
-
-
Save laithnurie/1d709404beacceafd417 to your computer and use it in GitHub Desktop.
Overlay button over device
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service; | |
import android.content.Intent; | |
import android.graphics.PixelFormat; | |
import android.os.IBinder; | |
import android.view.Gravity; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.WindowManager; | |
import android.widget.ImageView; | |
public class FloatingButtonService extends Service { | |
private WindowManager windowManager; | |
private ImageView appButton; | |
WindowManager.LayoutParams params; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); | |
appButton = new ImageView(this); | |
appButton.setImageResource(R.drawable.ic_play_arrow_white); | |
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; | |
//this code is for dragging the chat head | |
appButton.setOnTouchListener(new View.OnTouchListener() { | |
private int initialX; | |
private int initialY; | |
private float initialTouchX; | |
private float initialTouchY; | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
initialX = params.x; | |
initialY = params.y; | |
initialTouchX = event.getRawX(); | |
initialTouchY = event.getRawY(); | |
return true; | |
case MotionEvent.ACTION_UP: | |
return true; | |
case MotionEvent.ACTION_MOVE: | |
params.x = initialX | |
+ (int) (event.getRawX() - initialTouchX); | |
params.y = initialY | |
+ (int) (event.getRawY() - initialTouchY); | |
windowManager.updateViewLayout(appButton, params); | |
return true; | |
} | |
return false; | |
} | |
}); | |
windowManager.addView(appButton, params); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if (appButton != null) | |
windowManager.removeView(appButton); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
} | |
<---------------------> | |
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> | |
<service | |
android:name=".FloatingButtonService" | |
android:enabled="true" | |
android:exported="true"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great, for TargetSDK>=23 use : if (!Settings.canDrawOverlays(this)) startActivity( Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)) to ask for SYSTEM_ALERT_WINDOW permission