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);
}
}