setup background color in listview
Asked Answered
B

5

6

How do I get my whole backgroundpage white? I have a listview and tried to set the backgroundcolor white in the xml but it didn't worked. These are my xmls:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView 
android:id="@+id/list" android:layout_width="fill_parent" 
android:clickable="true" android:layout_height="fill_parent"></ListView>

The only thing that actually gets white is this:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/naam" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>

this is my java code:

public class Contactenlijst extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final HashMap<Integer, Personeel> personeelmap = new HashMap<Integer, Personeel>();
    ArrayList<String> list = new ArrayList<String>();
    // Get the data (see above)

    JSONObject json = Database
            .getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");

    try {
        JSONArray contactinfo = json.getJSONArray("contactlijst");
        // Loop the Array
        for (int i = 0; i < contactinfo.length(); i++) {

            JSONObject e = contactinfo.getJSONObject(i);
            Personeel p = new Personeel(
                    Integer.parseInt(e.getString("id")),
                    e.getString("staff_name"),
                    e.getString("staff_lastname"),
                    e.getString("staff_dateofbirth"),
                    e.getString("staff_address"),
                    e.getString("staff_address_postal"),
                    e.getString("staff_address_city"),
                    e.getString("staff_mobile"));
            personeelmap.put(Integer.parseInt(e.getString("id")), p);
            list.add(p.toString());
        }

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    // onclick stuur array naar contactinfo

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            String text = ((TextView) view).getText().toString();
            Intent i = new Intent(Contactenlijst.this, Contactinfo.class);
            String uittekst[] = text.split(" ");
            String vnaam = uittekst[0].toString();

            ArrayList<String> al = new ArrayList<String>();
            int a = personeelmap.size();

            a = a + 1;
            for (int c = 1; c < a; c++) {

                if (personeelmap.get(c).getStaff_name().toString()
                        .equals(vnaam)) {
                    al.add(personeelmap.get(c).getStaff_name());
                    al.add(personeelmap.get(c).getStaff_lastname());
                    al.add(personeelmap.get(c).getDateofbirth());
                    al.add(personeelmap.get(c).getStaff_address());
                    al.add(personeelmap.get(c).getStaff_address_postal());
                    al.add(personeelmap.get(c).getStaff_address_city());
                    al.add(personeelmap.get(c).getStaff_mobile());
                }
                ;
            }

            i.putStringArrayListExtra("array", al);
            startActivity(i);
        }
    });
}

}

Bedrabble answered 16/9, 2011 at 14:31 Comment(1)
What does exactly happen right now? Could you post a screen shot?Edgebone
H
15

There is a very easy way to do this.

First, create a ListView variable in your activity (above onCreate):

ListView LV = null;

Then in onCreate to set the variable you can do this, assuming you extend ListActivity:

LV = getListView();

or this works instead assuming mylist id is set in XML to the listview:

LV = (ListView) findViewById(R.id.mylist);

finally, set the background to a color:

LV.setBackgroundColor(Color.WHITE);

or set it to a drawable:

int r = getResources().getIdentifier("subtle_white_gradient", "drawable", "com.my.package");
LV.setBackgroundResource(r);
Harcourt answered 15/2, 2012 at 18:38 Comment(2)
Setting programmatically works but layout XML way android:background="@color/background" doesnt seem to work. Any idea why this behavior ?Flynn
Things like @color/white will work with predefined colors or you can make your own in the strings.xml see here: #2749330Harcourt
A
5

if you want to set background color at index '0',U should be try this it works nice i have used this code.

list.post(new Runnable() {

    @Override
    public void run() {
        list.setSelected(true);
        list.setBackgroundColor(Color.BLACK);
        list.getChildAt(0).setBackgroundColor(Color.BLUE);          
    }
});
Amharic answered 24/2, 2015 at 12:18 Comment(0)
S
3

You need to create the color.xml file in the res/value folder of your project. color.xml is

 <?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="orange">#ff5500</color>
 <color name="white">#ffffff</color>
 <color name="transparent">#00000000</color>
 <color name="black">#000000</color>
 <color name="gray">#999999</color>
 <color name="blue">#0066cc</color>

</resources>

Now call this color in your Lisiview

<ListView  
        android:id="@+id/list" 
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:background="@color/blue"/>
Schroder answered 3/9, 2015 at 11:49 Comment(0)
B
1

The simplest way is to set a theme. In your manifest.xml file, just add this attribute below to your activity:

android:theme="@android:style/Theme.Light"
Bielefeld answered 15/11, 2011 at 19:17 Comment(0)
A
-1

Try this in the XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
        android:orientation="vertical" 
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">
        <ListView  android:id="@+id/list" 
            android:layout_width="fill_parent"  
            android:clickable="true" 
            android:layout_height="fill_parent"
            android:background="@color/background" >
        </ListView>

And create an XML file called colors.xml that defines the color you want:

<color name="background">#CDB38B </string>
Abettor answered 16/9, 2011 at 14:45 Comment(1)
@color attribute should be defined in /values/colors.xmlFlynn

© 2022 - 2024 — McMap. All rights reserved.