android - How to align image and text one under another in two linear layouts for dynamic content -


xml file --->>>>     <horizontalscrollview         android:id="@+id/dashboard_hs"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margintop="10dp"         android:fillviewport="false"         android:scrollbarsize="3dp">         <relativelayout             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/rlforimggallery">              <linearlayout                 android:id="@+id/imagegallery"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:orientation="horizontal"                 android:layout_gravity="center_vertical"                 android:paddingtop="50dp">             </linearlayout>             <linearlayout                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:id="@+id/view_name"                 android:gravity="center"                 android:layout_below="@+id/imagegallery"                 android:orientation="horizontal"                 android:layout_margintop="5dp">              </linearlayout>          </relativelayout>     </horizontalscrollview> 

java file---->>>

  linearlayout.layoutparams lp = new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content);             linearlayout.layoutparams lp1 = new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content);              lp.gravity = gravity.center_vertical;              //relativelayout.layoutparams rp = new relativelayout.layoutparams(viewgroup.layoutparams.wrap_content,                   //  viewgroup.layoutparams.wrap_content);          //   rp.addrule(relativelayout.below, r.id.imagegallery);             lp.setmargins(0, 0, 60, 0);             lp1.setmargins(60,0,150,0);             lp1.gravity = gravity.center_vertical;           //   tv.setlayoutparams(rp);             tv.settextcolor(color.parsecolor("#000000"));             imagegallery.addview(imgview);             //imagegallery.addview(tv);              name_view.addview(tv);               //relativelayout.addview(tv);               imgview.setlayoutparams(lp);             tv.setlayoutparams(lp1); 

i have 2 lineat layouts inside horizontalscrollview, 1 linearlayout displays image dynamically , linearlayout display text(i.e name of user) . problem is,it not displaying text under corresponding image.

since, using wrap_content both imageview , textview, won't align corresponding. troubling point in layout use <relativelayout> (performance issue here) can avoided. i'd suggest make parent layout below:

<horizontalscrollview     android:id="@+id/dashboard_hs"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margintop="10dp"     android:fillviewport="false"     android:scrollbarsize="3dp">        <!-- add views here -->       </horizontalscrollview> 

now, can define fixed 'cell' layout hold each of user data. define (vague example):

// cell_layout.xml <linearlayout     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="vertical"     android:gravity="center_horizontal">      <imageview         android:layout_width="160dp"         android:layout_height="160dp"         android:padding="20dp"         android:id="@+id/imgview"         android:src="@android:color/holo_red_dark"/>      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/textview"         android:text="funk :)"         android:layout_margin="20dp"/>  </linearlayout> 

since, defined layout ourselves, guaranteed definite arrangement. can inflate layout @ runtime:

scrollview scrollview = (scrollview) findviewbyid(r.id.dashboard_hs);  layoutinflater inflater = getlayoutinflater();  // ----- // following segment repeated each of users linearlayout ll_main = (linearlayout) inflater.inflate(r.layout.cell_layout, scrollview, false);  imageview imgview = (imageview) ll_main.findviewbyif(r.id.imgview); imgview.setimageresource(r.id.user); textview textview = (textview) ll_main.findviewbyid(r.id.textview); textview.settext("username");  scrollview.addview(ll_main);  // ----- 

done! let me know if have confusion.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -