- how retrieve id of drop target (button choicedraglistener() class called)
- how can specify drag , drop limit ,i.e. how many times "drag , drop" can done
this code
design activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:background="@drawable/g" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".mainactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <tablelayout android:id="@+id/table" android:paddingtop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <tablerow > <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b1" android:layout_margin="5dp" android:background="@android:color/transparent" android:textsize="26sp" android:text="a"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b2" android:layout_margin="5dp" android:background="@android:color/transparent" android:textsize="26sp" android:text="b"/> </tablerow> <tablerow > <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b3" android:layout_margin="5dp" android:background="@android:color/transparent" android:textsize="26sp" android:text="1"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b4" android:layout_margin="5dp" android:background="@android:color/transparent" android:textsize="26sp" android:text="2"/> </tablerow> <tablerow > <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b5" android:layout_margin="5dp" android:background="@android:color/transparent" android:textsize="26sp" android:text="a"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b6" android:layout_margin="5dp" android:background="@android:color/transparent" android:textsize="26sp" android:text="b"/> </tablerow> </tablelayout> mainactivity.java
package com.example.in; import android.os.build; import android.os.bundle; import android.annotation.suppresslint; import android.annotation.targetapi; import android.app.activity; import android.content.clipdata; import android.graphics.color; import android.graphics.typeface; import android.view.dragevent; import android.view.menu; import android.view.motionevent; import android.view.view; import android.view.view.dragshadowbuilder; import android.view.view.ondraglistener; import android.view.view.ontouchlistener; import android.widget.button; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { button b1,b2,b3,b4,b5,b6; @targetapi(build.version_codes.honeycomb) @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); final string btn_tag ="button"; setcontentview(r.layout.activity_main); button b1=(button) findviewbyid(r.id.b1); button b2=(button) findviewbyid(r.id.b2); button b3=(button) findviewbyid(r.id.b3); button b4=(button) findviewbyid(r.id.b4); button b5=(button) findviewbyid(r.id.b5); button b6=(button) findviewbyid(r.id.b6); //b1.settag(btn_tag); b1.setontouchlistener(new choicetouchlistener()); b2.setontouchlistener(new choicetouchlistener()); b3.setontouchlistener(new choicetouchlistener()); b4.setondraglistener(new choicedraglistener()); b5.setondraglistener(new choicedraglistener()); b6.setondraglistener(new choicedraglistener()); } @suppresswarnings("unused") private final class choicetouchlistener implements ontouchlistener { @suppresslint("newapi") @override public boolean ontouch(view view, motionevent motionevent) { if (motionevent.getaction() == motionevent.action_down) { clipdata data = clipdata.newplaintext("", ""); dragshadowbuilder shadowbuilder = new view.dragshadowbuilder(view); view.startdrag(data, shadowbuilder, view, 0); return true; } else { return false; } } } @suppresslint("newapi") private class choicedraglistener implements ondraglistener { @override public boolean ondrag(view v, dragevent event) { switch (event.getaction()) { case dragevent.action_drag_started: break; case dragevent.action_drag_entered: break; case dragevent.action_drag_exited: //no action necessary break; case dragevent.action_drop: view view = (view) event.getlocalstate(); button droptarget = (button) v; button dropped = (button) view; droptarget.settext(droptarget.gettext().tostring() + dropped.gettext().tostring()); case dragevent.action_drag_ended: //no action necessary break; default: break; } return true; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
- you can use local count variable count of number times drag , drop happen.
- you can id of drag , dropped view using
v.getid();in switch case statements...
Comments
Post a Comment