android - On Click event for Parent and Child view -
i have relativelayout
, imageview
.
layout given below -
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rlmain" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" > <imageview android:id="@+id/ivicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_launcher" /> </relativelayout>
the activity
code -
public class mainactivity extends activity implements onclicklistener{ private relativelayout rlmain; private imageview ivicon; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); rlmain = (relativelayout) findviewbyid(r.id.rlmain); ivicon = (imageview) findviewbyid(r.id.ivicon); rlmain.setonclicklistener(this); ivicon.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()) { case r.id.rlmain: toast.maketext(this, "relative layout clicked", toast.length_short).show(); break; case r.id.ivicon: toast.maketext(this, "image view clicked", toast.length_short).show(); break; } } }
i have applied on click listener both relativelayout
(parent) , imageview
(child).
while clicking on relative layout - handled relative layout click handler.(this seems correct).
while clicking on imagview - handled imageview. (here confusion).
should click on imageview handled both parent(relative layout) , child (imageview)? logic if child view handling click?
a clickevent delivered lowest child element in layout hierarchy. if element not have onclick behaviour pass event parent until event gets handled.
therefore can treat linearlayout 1 single block onclick behaviour. if create clickable element inside layout sure make big enough reduce chance of user missing correct item.
source: does making parent clickable make child element clickable well?
Comments
Post a Comment