android - How to Get Child position for Linear layout -


i have linearlayout, contains several child imageview (added imageviews dynamically linearlayout horizontally). how can position of child views of linerlayout when doing long press child view(imageview)?

added settag:

 image.setimagebitmap(resizedbitmap);  image.settag(fullurl);    imageviewlinearlayout.setonlongclicklistener(new view.onlongclicklistener(){      @override      public boolean onlongclick(view v) {            log.d("mobi","path :"+v.gettag());            return false;        }  }); 

added set tag file path return v.gettag() null"?

add test.xml

<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content" >      <button         android:id="@+id/btnaddimage"         style="?android:attr/buttonstylesmall"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="add image" />      <horizontalscrollview         android:layout_width="fill_parent"         android:layout_height="0dp"         android:layout_weight="1.5"         android:fadingedge="none"         android:orientation="horizontal" >          <linearlayout             android:id="@+id/lnrparent"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="horizontal" >         </linearlayout>     </horizontalscrollview>  </tablelayout> 

and layout lay_image.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/lnrchild"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="horizontal" >      <button         android:id="@+id/btnexample"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="button" />      <imageview         android:id="@+id/imgview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginleft="50sp"         android:src="@drawable/ic_launcher" />  </linearlayout> 

and here coding

setcontentview(r.layout.test);          final linearlayout lnrlayout = (linearlayout) findviewbyid(r.id.lnrparent);          button btnaddimage = (button) findviewbyid(r.id.btnaddimage);          btnaddimage.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 final linearlayout lnrchildlayout = (linearlayout) getlayoutinflater()                         .inflate(r.layout.lay_image, null);                 lnrchildlayout.setlayoutparams(new layoutparams(                         viewgroup.layoutparams.match_parent,                         viewgroup.layoutparams.wrap_content));                  lnrlayout.addview(lnrchildlayout);                  int indexvalue = lnrlayout.indexofchild(lnrchildlayout);                 lnrchildlayout.settag(integer.tostring(indexvalue));                  final imageview imgview = (imageview) lnrchildlayout                         .findviewbyid(r.id.imgview);                  int imageposition = lnrchildlayout.indexofchild(imgview);                 imgview.settag(integer.tostring(imageposition));                  imgview.setonlongclicklistener(new onlongclicklistener() {                     @override                     public boolean onlongclick(view v) {                          // position                         string strposition = (string) v.gettag();                         toast.maketext(test.this,                                 "imageview gettag : >" + strposition,                                 toast.length_short).show();                         // getid                         int strid = v.getid();                         toast.maketext(test.this,                                 "imageview getid : >" + strid,                                 toast.length_short).show();                          return true;                      }                 });                  lnrchildlayout                         .setonlongclicklistener(new onlongclicklistener() {                             @override                             public boolean onlongclick(view v) {                                  string strposition = (string) v.gettag();                                 toast.maketext(test.this, "" + strposition,                                         toast.length_short).show();                                  return true;                             }                         });                  button btnexample = (button) lnrchildlayout                         .findviewbyid(r.id.btnexample);                 int buttonposition = lnrchildlayout.indexofchild(btnexample);                 btnexample.settag(integer.tostring(buttonposition));                  btnexample.setonlongclicklistener(new onlongclicklistener() {                     @override                     public boolean onlongclick(view v) {                          string strposition = (string) v.gettag();                         toast.maketext(test.this,                                 "button gettag : >" + strposition,                                 toast.length_short).show();                          int strid = v.getid();                         toast.maketext(test.this, "button getid : >" + strid,                                 toast.length_short).show();                          return true;                     }                 });              }         }); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -