Android onTouchListener and onClick -
so working on simple app displays button on overlay , when click open apps if it's installed. , if want move can. working okay except when click drag , move icon around screen, have click precise point (the relative layout behind button) , if try click , drag button doesn't move, suggestions on how this?
this current code:
myimage = (imageview)mfloatingview.findviewbyid(r.id.floatimage); myimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { showtoast("clicked image"); } }); //drag , move floating view using user's touch action. mfloatingview.findviewbyid(r.id.root_container).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: //remember initial position. initialx = params.x; initialy = params.y; //get touch location initialtouchx = event.getrawx(); initialtouchy = event.getrawy(); return true; case motionevent.action_up: int xdiff = (int) (event.getrawx() - initialtouchx); int ydiff = (int) (event.getrawy() - initialtouchy); return true; case motionevent.action_move: //calculate x , y coordinates of view. params.x = initialx + (int) (event.getrawx() - initialtouchx); params.y = initialy + (int) (event.getrawy() - initialtouchy); //update layout new x & y coordinate mwindowmanager.updateviewlayout(mfloatingview, params); return true; } return false; } });
my xml file:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content"> <relativelayout android:id="@+id/root_container" android:layout_width="wrap_content" android:layout_height="wrap_content"> <imageview android:id="@+id/floatimage" android:layout_width="60dp" android:layout_height="60dp" android:layout_margintop="8dp" android:src="@drawable/float_icon"/> </relativelayout>
i tried this, click register slow react , if moves @ doesn't click.
myimage.findviewbyid(r.id.floatimage).setontouchlistener(new view.ontouchlistener() { private int initialx; private int initialy; private float initialtouchx; private float initialtouchy; //declare flag globally boolean goneflag = false; //put class final handler handler = new handler(); runnable mlongpressed = new runnable() { public void run() { goneflag = true; //code long click } }; @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: //remember initial position. initialx = params.x; initialy = params.y; //get touch location initialtouchx = event.getrawx(); initialtouchy = event.getrawy(); handler.postdelayed(mlongpressed, 200); return true; case motionevent.action_up: int xdiff = (int) (event.getrawx() - initialtouchx); int ydiff = (int) (event.getrawy() - initialtouchy); handler.removecallbacks(mlongpressed); if(math.abs(event.getrawx() - initialtouchx) <= 4 && !goneflag) { //code single click showtoast("single click"); return false; } return true; case motionevent.action_move: handler.removecallbacks(mlongpressed); //calculate x , y coordinates of view. params.x = initialx + (int) (event.getrawx() - initialtouchx); params.y = initialy + (int) (event.getrawy() - initialtouchy); //update layout new x & y coordinate mwindowmanager.updateviewlayout(mfloatingview, params); return true; } return false; } });
Comments
Post a Comment