Is it possible to add a footer to a ListView from the XML?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Is it possible to add a footer to a ListView from the XML?

Postby gust0208 » Fri Nov 14, 2008 1:59 am

Hello everyone,

I am nearing the end of my project and was working on the GUI portions. My question is it possible to add the footer view to the ListView from the XML? I am extending ListActivity for my main class and am trying to add a footer to the base of my ListView.

I read some tutorials on adding a footer view in the java code, but I am not sure how to directly access the ListView when extending ListActivity given that the ListView in the XML layout is defined with android:id="@+id/android:list".

So the solution it to add the footer in the XML layouts OR figure out how to bind the ListView in a ListActivity to a variable (e.g. ListView test = (ListView) findViewById(R.id.?????)) and can then bind a footer in the java code.

Thank you very much in advance.

Cheers,
Tom
gust0208
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 14, 2008 1:55 am

Top

Postby rmanromga » Fri Nov 14, 2008 4:23 am

Is your list view contained within a LinearLayout ? or are you using AbsoluteLayouts to set the height of the ListView? If the ListView is contained within a LinearLayout on your screen, then why don't you just add another Container eg. a LinearLayout, right after the ListView and if you are using AbsoluteLayouts, just be sure to set the dimenstions. Check out the product DroidDraw at http://www.droiddraw.org/ they have a nice WYSIWYG Android screen editor that you can play with to try and get your layouts...

Cheers
--D
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Postby gust0208 » Fri Nov 14, 2008 4:41 am

It is contained within a linear layout. The problem is, if the buttons or text you want at the bottom of the screen are not contained in a formal footer view of the ListView, then they will be "pushed off" the screen if the ListView gets long enough to fill the screen with data. Thus the need to define it is a footer of the ListView.

Is there a way to reference the list defined as in XML when using a ListActivity? I know how to add a footer, but not in the situation of using a ListView in a ListActivity.

Thanks,
Tom

rmanromga wrote:Is your list view contained within a LinearLayout ? or are you using AbsoluteLayouts to set the height of the ListView? If the ListView is contained within a LinearLayout on your screen, then why don't you just add another Container eg. a LinearLayout, right after the ListView and if you are using AbsoluteLayouts, just be sure to set the dimenstions. Check out the product DroidDraw at http://www.droiddraw.org/ they have a nice WYSIWYG Android screen editor that you can play with to try and get your layouts...

Cheers
--D
gust0208
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 14, 2008 1:55 am

Postby Sweevo » Fri Nov 14, 2008 4:52 am

There is no need to do that.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:orientation="vertical"
  7.  
  8.         android:layout_width="fill_parent"
  9.  
  10.         android:layout_height="fill_parent"
  11.  
  12.         >
  13.  
  14.                
  15.  
  16.         <ImageView
  17.  
  18.                 android:src="@drawable/header"
  19.  
  20.                 android:layout_width="fill_parent"
  21.  
  22.                 android:layout_height="wrap_content"
  23.  
  24.         />
  25.  
  26.                
  27.  
  28.         <ListView
  29.  
  30.                 android:id="@android:id/list"
  31.  
  32.                 android:layout_width="fill_parent"
  33.  
  34.                 android:layout_height="wrap_content"
  35.  
  36.                 android:layout_weight="1"
  37.  
  38.         />
  39.  
  40.        
  41.  
  42.         <ImageView
  43.  
  44.                 android:src="@drawable/footer"
  45.  
  46.                 android:layout_width="fill_parent"
  47.  
  48.                 android:layout_height="wrap_content"
  49.  
  50.         />
  51.  
  52.  
  53.  
  54. </LinearLayout>
  55.  
  56.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Sweevo
Developer
Developer
 
Posts: 35
Joined: Thu Oct 16, 2008 9:27 am

Postby gust0208 » Fri Nov 14, 2008 5:05 am

Thank you for the reply. Unfortunately, I need to use TextViews and am still having the problem of the ListView pushing the TextViews off the screen when filled with data. Here is my XML for example. I may have to go with an AbsoluteLayout in order to restrain the ListView, it does not seem to be possible with only LinearLayout tags.

Any other ideas before giving in to Absolute Layout?

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:orientation="vertical"
  7.  
  8.         android:layout_width="fill_parent"
  9.  
  10.         android:layout_height="fill_parent">
  11.  
  12.    
  13.  
  14.     <ListView android:id="@+id/android:list"
  15.  
  16.          android:layout_width="fill_parent"
  17.  
  18.                 android:layout_height="wrap_content"/>
  19.  
  20.         <TextView android:id="@+id/android:empty"
  21.  
  22.          android:layout_width="wrap_content"
  23.  
  24.                 android:layout_height="wrap_content"
  25.  
  26.                 android:text="No Periods Entered"/>
  27.  
  28.                
  29.  
  30.      <Button android:id="@+id/add_period"
  31.  
  32.           android:text="@string/menu_insert"
  33.  
  34.                 android:layout_width="fill_parent"
  35.  
  36.                 android:layout_height="wrap_content" />        
  37.  
  38.  
  39.  
  40. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  41.  
  42.         android:orientation="horizontal"
  43.  
  44.         android:layout_width="fill_parent"
  45.  
  46.         android:layout_height="wrap_content">
  47.  
  48.        
  49.  
  50.                 <TextView android:id="@+id/next_period_label"
  51.  
  52.          android:layout_width="wrap_content"
  53.  
  54.                 android:layout_height="wrap_content"
  55.  
  56.                 android:text="Likely start of next period:"
  57.  
  58.                 android:padding="7px"/>
  59.  
  60.                
  61.  
  62.         <TextView android:id="@+id/next_period_value"
  63.  
  64.          android:layout_width="wrap_content"
  65.  
  66.                 android:layout_height="wrap_content"
  67.  
  68.                 android:text=""
  69.  
  70.                 android:textSize="17sp"/>
  71.  
  72. </LinearLayout>
  73.  
  74.  
  75.  
  76. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  77.  
  78.         android:orientation="horizontal"
  79.  
  80.         android:layout_width="fill_parent"
  81.  
  82.         android:layout_height="wrap_content">
  83.  
  84.        
  85.  
  86.                 <TextView android:id="@+id/next_ovulation_label"
  87.  
  88.          android:layout_width="wrap_content"
  89.  
  90.                 android:layout_height="wrap_content"
  91.  
  92.                 android:text="Likely next ovulation:"
  93.  
  94.                 android:padding="7px"/>
  95.  
  96.                
  97.  
  98.         <TextView android:id="@+id/next_ovulation_value"
  99.  
  100.          android:layout_width="fill_parent"
  101.  
  102.                 android:layout_height="wrap_content"
  103.  
  104.                 android:text=""
  105.  
  106.                 android:textSize="17sp"/>
  107.  
  108. </LinearLayout>
  109.  
  110.  
  111.  
  112. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  113.  
  114.         android:orientation="horizontal"
  115.  
  116.         android:layout_width="fill_parent"
  117.  
  118.         android:layout_height="wrap_content">
  119.  
  120.        
  121.  
  122.                 <TextView android:id="@+id/next_fertile_label"
  123.  
  124.          android:layout_width="wrap_content"
  125.  
  126.                 android:layout_height="wrap_content"
  127.  
  128.                 android:text="Likely dates of next \n fertile window:"
  129.  
  130.                 android:padding="7px"/>
  131.  
  132.                
  133.  
  134.         <TextView android:id="@+id/next_fertile_value"
  135.  
  136.          android:layout_width="wrap_content"
  137.  
  138.                 android:layout_height="wrap_content"
  139.  
  140.                 android:text=""
  141.  
  142.                 android:textSize="17sp"/>
  143.  
  144. </LinearLayout>
  145.  
  146.                                
  147.  
  148. </LinearLayout>
  149.  
  150.  
Parsed in 0.009 seconds, using GeSHi 1.0.8.4
gust0208
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 14, 2008 1:55 am

Postby rmanromga » Fri Nov 14, 2008 5:06 am

Yes do it the way sweevo demostrated... the only issue may be if your do not use the value "fill parent" for the height value of the ListView and provide an explicit height in pixels that is too long for the screen... otherwise what sweevo shows above should work, and your ListView should be able to scroll without pushing the ImageView that is at the bottom off the screen...
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Top

Postby rmanromga » Fri Nov 14, 2008 5:13 am

I Also forgot, a ListView also has a function called ListView.addFooter(View v)... which also adds a view at the bottom of the list which would contain your buttons so you could try that out.. I've never used it though...
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Postby gust0208 » Fri Nov 14, 2008 5:16 am

Thanks for the help. I added the ImageView tags and I am getting errors from those two lines in my XML file stating "no resource found with that name". I tried importing android.graphics.drawable in my main class, but no difference. Am I missing something in my main class to make the "@drawable/header" and "@drawable/footer" id's work?

Thanks again,
Tom

rmanromga wrote:Yes do it the way sweevo demostrated... the only issue may be if your do not use the value "fill parent" for the height value of the ListView and provide an explicit height in pixels that is too long for the screen... otherwise what sweevo shows above should work, and your ListView should be able to scroll without pushing the ImageView that is at the bottom off the screen...
gust0208
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 14, 2008 1:55 am

Postby rmanromga » Fri Nov 14, 2008 5:50 am

The @drawable/footer and @drawable/header applied specifically to Sweevo's case it was just an example for you to follow. In his example, he is loading images into the header and footer section with files called header and footer, so if you try to do the same thing without those files in your drawables directory, you will get the no resources error. In your case, I think yours would be buttons (from looking at your xml file) and not images....
rmanromga
Developer
Developer
 
Posts: 43
Joined: Thu Dec 20, 2007 6:21 am
Location: Baltimore, MD USA

Postby Sweevo » Fri Nov 14, 2008 5:57 am

Do you have a header.png and footer.png in your res/drawable directory?

Edit: yes, exactly what rmanromga said.
Sweevo
Developer
Developer
 
Posts: 35
Joined: Thu Oct 16, 2008 9:27 am

Postby rjones54 » Fri Nov 14, 2008 10:40 pm

The original XML that you posted it just fine, you just need to add to your ListView:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:layout_weight="1.0"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


I built a quick test project based off of the ApiDemos List13 activity and used your XML, and it displays the way you want. However, if you don't have the layout_weight specified, the list will take over.
-rich
rjones54
Freshman
Freshman
 
Posts: 4
Joined: Sat Nov 01, 2008 1:51 am

Postby gust0208 » Sat Nov 15, 2008 5:49 am

Thank you!

That worked like magic! I will have to go and reread the section on layout_weight in the docs.

Thanks again everyone for the help.

Cheers,
Tom

rjones54 wrote:The original XML that you posted it just fine, you just need to add to your ListView:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:layout_weight="1.0"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


I built a quick test project based off of the ApiDemos List13 activity and used your XML, and it displays the way you want. However, if you don't have the layout_weight specified, the list will take over.
-rich
gust0208
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 14, 2008 1:55 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests