I have this overlay button that is being activated using a service. So it is a background button. You can see the button even if the other programs are running. However I have problem with adding a feature on the button where when I hold it, I can drag it anywhere I want to the screen. I have checked some drag and drop options here but I could not applied it properly in my program.
On my dragDrop()
method, it does not work because of the following cast error:
error(WindowManagerLayoutParams could not be cast on Freamelayout.LayoutParams)..
Here is my service code:
public class MainPowerAndVolumeService extends Service {
private ViewGroup mView;
private LayoutInflater inflater;
private ImageButton mainButton;
private Button testButton;
@Override
public IBinder
onBind(Intent intent) {
return null;
}
public void onCreate( ) {
super.onCreate();
/*This will make your window or layout to be overlay and of course you can click the buttons*/
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
/*This will load the main.xml view through inflate*/
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = (ViewGroup) inflater.inflate(R.layout.main, null);
mainButton =(ImageButton) mView.findViewById(R.id.MainButton);
wm.addView(mView,params);
buttonListener();
dragDrop( );
}
public void dragDrop(){
mainButton.setOnTouchListener(new View.OnTouchListener() {
int prevX,prevY;
/**this does not work since framelayout could not be cast to the Window Manager**/
@Override
public boolean onTouch(final View v, final MotionEvent event) {
final FrameLayout.LayoutParams par = (FrameLayout.LayoutParams) v.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
par.topMargin += (int) event.getRawY() - prevY;
prevY = (int) event.getRawY();
par.leftMargin += (int) event.getRawX() - prevX;
prevX = (int) event.getRawX();
v.setLayoutParams(par);
return true;
}
case MotionEvent.ACTION_UP: {
par.topMargin += (int) event.getRawY() - prevY;
par.leftMargin += (int) event.getRawX() - prevX;
v.setLayoutParams(par);
return true;
}
case MotionEvent.ACTION_DOWN: {
prevX = (int) event.getRawX();
prevY = (int) event.getRawY();
par.bottomMargin = -2 * v.getHeight();
par.rightMargin = -2 * v.getWidth();
v.setLayoutParams(par);
return true;
}
}
return false;
}
});
}
public void buttonListener(){
mainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent = Intent.
Toast.makeText(MainPowerAndVolumeService.this,
"ImageButton (selector) is clicked!",
Toast.LENGTH_SHORT).show();
}
});
}
public void onDestroy(){
super.onDestroy();
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
}
}
You have to set params.gravity
to Gravity.TOP | Gravity.LEFT
. Set that to your params
when you first initialize the View and when you update it in the OnTouchListener
. Here is the code:
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_DOWN)
{
/* You can play around with the offset to set where you want the users finger to be on the view. Currently it should be centered.*/
xOffset = v.getWidth()/2;
yOffset = v.getHeight()/2;
x = (int) event.getRawX() - xOffset;
y = (int) event.getRawY() - yOffset;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
x,y,
WindowManager.LayoutParams.TYPE_PHONE ,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity. LEFT;
wm.updateViewLayout(mView, params);
return true;
}
return false;
}
});
If you want to save it so your button shows up at the same spot when the user reloads your app, just set a SharedPreference for x and y in the OnTouchListener
's ACTION_UP
. Then recall them and insert them into your params
when you first initialize the View.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With