Changing the layout of a listview

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

Changing the layout of a listview

Postby GeorgeOfTheBush » Mon Dec 29, 2008 8:41 pm

Hi .. I was able to create a list of strings and connect them to android.R.layout.simple_list_item_1 layout through an arrayadapter with examples given by plusminus and others in anddev.org forums.
simple_list_item_1 is a built-in layout prepared by the android developers. I would like to have a layout of my own and show the list of strings in a different layout. How do I do that?? Any code snippets would be helpful.


Thanks!
GeorgeOfTheBush
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 29, 2008 8:24 pm

Top

Postby GeorgeOfTheBush » Tue Dec 30, 2008 6:55 am

This is the code I am trying.

Instead of using android.R.layout.simple_list_item_1, how to use a different layout that I define? For example, change the background, font,etc.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package com.list1;
  5.  
  6.  
  7.  
  8. import android.app.ListActivity;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.widget.ArrayAdapter;
  13.  
  14.  
  15.  
  16. public class list1 extends ListActivity {
  17.  
  18.     /** Called when the activity is first created. */
  19.  
  20.     @Override
  21.  
  22.     public void onCreate(Bundle savedInstanceState) {
  23.  
  24.         super.onCreate(savedInstanceState);
  25.  
  26.         //setContentView(R.layout.main);
  27.  
  28.    
  29.  
  30.         // Create an array of Strings, that will be put to our ListActivity
  31.  
  32.         String[] mStrings = new String[]{"String1", "String2", "String3"};
  33.  
  34.  
  35.  
  36.        
  37.  
  38.         this.setListAdapter (new ArrayAdapter<String> (
  39.  
  40.                         this,
  41.  
  42.                         android.R.layout.simple_list_item_1,
  43.  
  44.                         mStrings));
  45.  
  46.     }
  47.  
  48. }
  49.  
  50.  
  51.  
  52.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
GeorgeOfTheBush
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 29, 2008 8:24 pm

Postby strongsauce » Tue Dec 30, 2008 10:29 am

You will need to create your own xml layout and place it in your layout folder under /<program name>/res/layout/ then replace "android.R.layout.simple_list_item_1" with "R.layout.<layout name without the '.xml' extension>"

So for example if I create a xml file called "new_list_view.xml" then to reference that layout for the adapter you would replace android.R.layout.simple_list_item_1 with R.layout.new_list_view.
strongsauce
Freshman
Freshman
 
Posts: 5
Joined: Wed Dec 03, 2008 8:30 pm

Postby GeorgeOfTheBush » Wed Dec 31, 2008 11:45 am

Thanks strongsauce.

So that means "R.layout.new_list_view" (i.e. new_list_view.xml) specifies the layout for the TEXTVIEW (and not for the entire screen) that is shown inside the listview. and you can use this layout to make any changes to the font, color,etc. of the TEXTVIEW. Is that correct?

Is this new_list_view.xml correct?

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  2.    android:id="@android:id/text1"
  3.    android:layout_width="fill_parent"
  4.    android:layout_height="?android:attr/listPreferredItemHeight"
  5.    android:textAppearance="?android:attr/textAppearanceLarge"
  6.    android:gravity="center_vertical"
  7.    android:paddingLeft="5dip"
  8.    android:singleLine="true"
  9. />
  10.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4



How to set a background color or background image for the whole screen which has this listview?
GeorgeOfTheBush
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 29, 2008 8:24 pm

Postby strongsauce » Wed Dec 31, 2008 5:51 pm

Yes, but you can do more than that, you could also add an ImageView into the layout so you could have an icon for each list item.

Also your XML is correct but you still need to declare it as an XML doc so you'd have to prepend
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

to the top of the file.
strongsauce
Freshman
Freshman
 
Posts: 5
Joined: Wed Dec 03, 2008 8:30 pm

Postby Emmanuel7 » Thu Jan 01, 2009 3:38 am

GeorgeOfTheBush wrote:How to set a background color or background image for the whole screen which has this listview?


To set a background color/image for the whole screen, you have to add this line :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:background="@drawable/My_Image"
  2.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Where My_Image is the name of your background image in your drawable directory.
This line should be inserted in your topmost view of your screen. It could be your listview, I personnally have quite always a LinearLayout on top of my listView, in order to add some stuff.

One last very important thing : if you use a listview with a background image, add a android:cacheColorHint="#00000000" entry in your list View XML definition, to prevent an optimization that mess the display up with background image.
It took me some time to understand this issue !
( if you're interested, see my posts : listview Issue and
solution )


Hope it helps !
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Top

Postby GeorgeOfTheBush » Thu Jan 01, 2009 3:23 pm

I am unable to find out what is wrong with this piece of code?

The text view is for a list view. It works fine if the linearlayout part of the xml is removed. All I wanted to do was add the textview (listview) to the linearlayout and then set a background image for the linearlayout so that the entire screen has a background image.


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4.  
  5.  
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.    android:orientation="vertical"
  9.  
  10.    android:layout_width="fill_parent"
  11.  
  12.    android:layout_height="fill_parent"
  13.  
  14.    >
  15.  
  16.  
  17.  
  18. <TextView
  19.  
  20.    android:id="@android:id/text1"
  21.  
  22.    android:layout_width="fill_parent"
  23.  
  24.    android:layout_height="wrap_content"
  25.  
  26.    android:gravity="center_horizontal"
  27.  
  28.    android:paddingLeft="20dip"
  29.  
  30.    android:singleLine="true"
  31.  
  32.    android:textColor="#ffffffff"
  33.  
  34.    />
  35.  
  36.  
  37.  
  38. </LinearLayout>
  39.  
  40.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
GeorgeOfTheBush
Freshman
Freshman
 
Posts: 9
Joined: Mon Dec 29, 2008 8:24 pm

Postby defdwight » Fri Jan 02, 2009 10:39 pm

You don't want that LinearLayout code in your TextView file. Only the <?xml version="1.0" encoding="utf-8"?> and the TextView code. You need to uncomment your setContentView call in the list1 class and modify your main.xml file. If you want to create a custom layout, you will format it in your main.xml file.

For this to work, you MUST have a ListView in that main.xml file though and the ID must be as follows: android:id="@android:id/list"

Here's a very simple example of the ListView code that would be in your main.xml file:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ListView
  2.  
  3.         android:id="@android:id/list"
  4.  
  5.         android:layout_width="fill_parent"
  6.  
  7.         android:layout_height="wrap_content"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


You would add any custom formatting from that point to achieve the look you wanted. In other words, if you want a background image to fill the whole screen, that coding is done in main.xml, not the file you created for your list's TextView. The ListActivity will automatically use the TextView file you specify to populate each item in the ListView, that is all it is there for. All other formatting is to be done in your main.xml file. Hope this helps.
defdwight
Freshman
Freshman
 
Posts: 6
Joined: Fri Jan 02, 2009 9:29 pm
Location: FL

Postby Zeba Momin » Thu Dec 17, 2009 10:19 am

hi,
I hav a ListView control in my layout wid the id: "@+id/android:list"
Like:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
  2.  
  3.                 android:layout_height="fill_parent" android:background="@drawable/back_grnd"
  4.  
  5.                 android:listSelector="@drawable/list_select_back" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Plzz can sumone tell me..
how do i access this control in my java class??
I tried:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ListView lvList=(ListView)findViewById(android.R.id.list);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

and
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ListView lvList=(ListView)findViewById(R.id.list);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

But both these lines throw an error.... :(
Plzzz help...
Thanl you
Zeba Momin
Developer
Developer
 
Posts: 49
Joined: Thu Sep 17, 2009 6:53 am

Postby achie1266 » Thu Dec 17, 2009 12:12 pm

Zeba Momin wrote:how do i access this control in my java class??
I tried:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ListView lvList=(ListView)findViewById(android.R.id.list);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

and
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ListView lvList=(ListView)findViewById(R.id.list);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

But both these lines throw an error.... :(
Plzzz help...
Thanl you


android.R.id.list will only work when it is used as defined for list activity layouts and your class should extend listActivity. Also for that to work your listView id in xml should be like this
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ListView android:id="@android:id/list" .../>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

But when you are using more than one list in a layout or you need to use your own id for the listView as
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ListView android:id="@+id/my_list"..../>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
you can access it by using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ListView lvList=(ListView)findViewById(R.id.my_list);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
That does not work for you since the id is not set properly in the layout. It is probably throwing a nullPointerException since it could not find a view with such an id.

You should also inflate the layout before you access the views in that layout.
Is the ListView in the same layout as your activity layout?
If it is then you need to call setContentView first.
Then try accessing the list.

You may also try accessing the layout to be sure that your layout has been inflated and call the listView in that layout like this.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 LinearLayout myLayout = (LinearLayout)findViewById(R.id.layout_id);
  2.                 ListView lvList = (ListView)myLayout.findViewById(R.id.my_list);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

If all this does not work then post your error message and your layout file and the java code which you are using to access the listView. That will help people better to understand your problem.

hope it works for you.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby Zeba Momin » Thu Dec 17, 2009 1:34 pm

Thank you so much achie for replying,
Wat i did now is..extend my class with ListActivity
and use

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ListView lvList = getListView();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


and it worked...

I needed dis to set an "OnItemClickListener" like...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. lvList.setOnItemClickListener(listClickListener);
  2.                 listClickListener = new OnItemClickListener() {
  3.                         public void onItemClick(AdapterView<?> parent, View v,
  4.                                         int position, long id) {
  5.                                 System.out.println("::::list clicked from listener::::"+position);
  6.  
  7.                         }
  8.                 };
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


but the callback is not invoked on click of list item!! :(
Do hav any idea w.r.t this??
Zeba Momin
Developer
Developer
 
Posts: 49
Joined: Thu Sep 17, 2009 6:53 am

Postby achie1266 » Thu Dec 17, 2009 2:17 pm

Zeba Momin wrote:Thank you so much achie for replying,
Wat i did now is..extend my class with ListActivity
and use

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ListView lvList = getListView();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


and it worked...

I needed dis to set an "OnItemClickListener" like...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. lvList.setOnItemClickListener(listClickListener);
  2.                 listClickListener = new OnItemClickListener() {
  3.                         public void onItemClick(AdapterView<?> parent, View v,
  4.                                         int position, long id) {
  5.                                 System.out.println("::::list clicked from listener::::"+position);
  6.  
  7.                         }
  8.                 };
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


but the callback is not invoked on click of list item!! :(
Do hav any idea w.r.t this??

I will continue this discussion on the other thread you created for this problem
problem_with_onlistitemclick_when_using_multiple_listviews-t8690.html
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests