java - Floating button setOnClickListener not gets called in Android -
i want integrate movable floating button same "whatsapp" application when open, displays hike floating button motion. , working click event.
i used default ontouch
event it's working can't apply click event on floating button. how can apply both click , touch event ?
thanks in advance!
home.xml
<relativelayout android:id="@+id/rl_parent_floating" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="visible"> <relativelayout xmlns:fab="http://schemas.android.com/apk/res-auto" android:id="@+id/rl_floating_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparentbottom="true" android:layout_margin="10dp"> <textview android:id="@+id/txtcartcount" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignright="@+id/fab" android:layout_aligntop="@+id/fab" android:background="@drawable/cart_gold_circle" android:gravity="center" android:singleline="true" android:text="2" android:textcolor="@android:color/white" android:textsize="8sp" /> <com.melnykov.fab.floatingactionbutton android:id="@+id/fab" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margintop="3dp" android:src="@drawable/ic_floating_cart" fab:fab_colornormal="@color/header_color_red" fab:fab_colorpressed="@color/colorprimarydark" fab:fab_colorripple="@color/gold_color" /> </relativelayout> </relativelayout>
home.java
mfloatingactionbutton = (floatingactionbutton) findviewbyid(r.id.fab); mrrootlayout = (viewgroup) findviewbyid(r.id.rl_floating_button); rl_floating_button = (relativelayout) findviewbyid(r.id.rl_floating_button); rl_parent_floating = (relativelayout) findviewbyid(r.id.rl_parent_floating); mfloatingactionbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent mintent = new intent(mactivity, cart.class); startactivity(mintent); } }); mfloatingactionbutton.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view view, motionevent event) { final int x = (int) event.getrawx(); final int y = (int) event.getrawy(); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: relativelayout.layoutparams lparams = (relativelayout.layoutparams) view.getlayoutparams(); view.getlayoutparams(); _xdelta = x - lparams.leftmargin; _ydelta = y - lparams.topmargin; break; case motionevent.action_up: break; case motionevent.action_pointer_down: break; case motionevent.action_pointer_up: break; case motionevent.action_move: relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) view .getlayoutparams(); layoutparams.leftmargin = x - _xdelta; layoutparams.topmargin = y - _ydelta; layoutparams.rightmargin = 0; layoutparams.bottommargin = 0; view.setlayoutparams(layoutparams); break; } mrrootlayout.invalidate(); return true; } });
use simpleongesturelistener & gesturedetector
class single tap on screen instead of using interface setonclicklistner
may not work ontouchlistner
private class singletapdetector extends simpleongesturelistener{ @override public boolean onsingletapup(motionevent e) { return true; } }
and in oncreate
method initialize gesturedetector
gesturedetector=new gesturedetector(this,new singletapdetector());
now, check in ontouchlistener
of floating bubble , if single tap detected on floating button
code
mfloatingactionbutton.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (gesturedetector.ontouchevent(event)) { // code single tap or onclick // pass intent here } else { switch(event.getaction()){ // code move , drag // handle motionevents here } return true; } });
hope you.
Comments
Post a Comment