How to put a button in between a sticky 'header' and a Listview in Android -


my goal have sticky header, clear button, , listview (in order).

+-----------------+   |      header     |    (sticky) +-----------------+ | "clear" button  |    (not sticky) +-----------------+ |    listview     |    (also not sticky) |                 | |                 | |                 | |                 | |                 | |                 | +-----------------+ 

this following approach almost want. header sticky, , listview functions correctly. button sticky (it tags along right below sticky header).

<relativelayout>      <!-- used sticky header -->     <relativelayout     android:id="@+id/top_control_bar"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:minheight="?attr/actionbarsize">          <button/>          <textview/>     </relativelayout>   <!-- end xml sticky hader -->      <!--  button clear bookmarks  -->     <button         android:id="@+id/clearbutton"         android:text="clear"         android:layout_height="wrap_content"         android:layout_width="wrap_content"         android:layout_below="@+id/top_control_bar"         android:onclick="clearbookmarks"/>       <!--  listview displaying questions  -->     <listview         android:id="@+id/listviewbrowse"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_below="@id/clearbutton" >     </listview>  </relativelayout> 

i think need stop using property

    android:layout_below="@+id/top_control_bar" 

for "clear" button. doesn't work if rid of (the button overlays header if that). should adjust? or should try instead of approach?

* **solution ***

following these steps solved problem:

  1. create new layout file: non_sticky_header.xml (which contains clear button).
  2. remove clear button main layout.
  3. dynamically create view object inflating 'non_sticky_header' resource file. view object header.
  4. add header object listview, addheaderview().

code steps 3 , 4:

view header = getlayoutinflater().inflate(r.layout.non_sticky_header, null);   list.addheaderview(header);    

sources
-see third answer post @ndroiddev; solution varies steps 3 , 4.
-(see this s.o. post using addheaderview(), explains steps 3 , 4 (from @user370305).

i think , instead add ing clear button realtive layout, should add in listview's header this..

1) create new layout file non_sticky_header.xml , add following lines..

<relativelayout     android:layout_width="match_parent"     android:layout_height="wrap_content">  <!--  button clear bookmarks  --> <button         android:id="@+id/clearbutton"         android:text="clear"         android:layout_height="wrap_content"         android:layout_width="wrap_content"         android:layout_below="@+id/top_control_bar"         android:onclick="clearbookmarks"/>   </relativelayout> 

2) remove clear button main layout , update this... keep stick headre , listview in main layout..

<relativelayout>      <!-- used sticky header -->     <relativelayout     android:id="@+id/top_control_bar"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:minheight="?attr/actionbarsize">          <button/>          <textview/>     </relativelayout>   <!-- end xml sticky hader -->      <!--  listview displaying questions  -->     <listview         android:id="@+id/listviewbrowse"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_below="@id/clearbutton" >     </listview>  </relativelayout> 

3) @ last, add non_sticky_header.xml header of list in java file, this...

 //code add header , footer listview         layoutinflater inflater = getlayoutinflater();         viewgroup header = (viewgroup) inflater.inflate(r.layout.non_sticky_header, listview, false);         listview.addheaderview(header, null, false); 

hope helps... :)


Comments