Android - TextView onClick Layout onTouch -
i have little problem, in xml have this:
<linearlayout android:id="@+id/mainview" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <textview android:id="@+id/title" android:layout_width="match_parent" android:layout_height="45dp" android:gravity="center" android:text="@string/app_name" android:textcolor="@android:color/white" android:textstyle="bold" android:textsize="18sp" android:layout_margin="10dp" /> <linearlayout android:id="@+id/fistitem" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="15dp" android:layout_margintop="15dp" android:orientation="vertical" > <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/app_name" android:textcolor="#fff" android:textsize="20sp" /> </linearlayout> <linearlayout android:id="@+id/seconditem" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="15dp" android:layout_margintop="15dp" android:orientation="vertical" > <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/app_name" android:textcolor="#fff" android:textsize="20sp" /> </linearlayout> </linearlayout>
and have in activity file :
public class myactivity extends activity implements ontouchlistener, onclicklistener { linearlayout mainview; linearlayout firstitem; textview header; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.my_activity); mainview = (linearlayout) findviewbyid(r.id.mainview); firstitem = (linearlayout) findviewbyid(r.id.fistitem); header = (textview) findviewbyid(r.id.title); mainview.setontouchlistener(this); firstitem.setonclicklistener(this); } int n = 0; @override public void onclick(view v) { header.settext("first item clicked: " + ++n + " times"); } @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_down) { toast.maketext(getapplicationcontext(), "view touched", toast.length_short).show(); } return false; } }
the problem comes when click firstitem. can see how tittle text change toast doesn't pop-up, when click seconditem does.
you're missing this
firstitem.setontouchlistener(this);
in oncreate()
Comments
Post a Comment