Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a divider with setDivider in a ListActivity without a custom ListView?

I can't seem to get a customized divider, using a Drawable I've defined, to work when using a ListActivity and not creating a custom ListView. It almost seems like when the VM creates its own ListView for me, with the ListActivity, it uses a theme with the default divider provided; and if I try to provide one, no dividers appear in the ListView at all.

I know that I can create a custom ListView using XML and define android:divider on that ListView, and this does recognize my custom divider Drawable. But I would prefer to just let the ListActivity create its own ListView, if I can figure out how to get my own divider working on it.

Here's the code I'm using now:

public class Categories extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final String[] OPTIONS = {
            "Hello",
            "Goodbye",
            "Good Morning",
            "Greetings",
            "Toodaloo"
        };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, OPTIONS);
        setListAdapter(adapter);

        ListView lv = getListView();
        PaintDrawable sage = new PaintDrawable(R.drawable.sage);
        lv.setDivider(sage);
        lv.setDividerHeight(1);
    }
}
like image 880
hotshot309 Avatar asked Oct 29 '10 04:10

hotshot309


People also ask

What is setAdapter in android?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.

How does ListView work?

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.


3 Answers

I figured it out. The issue had nothing to do with the ListActivity generating a ListView for me. It was in how I was defining the divider in Java code.

There are two ways that I see to define the divider (border between ListView rows) on a ListView that is automatically inflated from a ListActivity, if you want to define the color in XML:

Method 1:

In res/values/colors.xml, put the following:

<resources>
 <color name="sage">#cceebb</color>
</resources>

In your ListActivity-extending class, do this:

ListView lv = getListView();
ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.color.sage));
lv.setDivider(sage);
lv.setDividerHeight(1);

Method 2:

In res/values/colors.xml:

<resources>
 <drawable name="sage">#cceebb</drawable>
</resources>

And in your class that extends ListActivity:

ListView lv = getListView();
ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.drawable.sage));
lv.setDivider(sage);
lv.setDividerHeight(1);
like image 106
hotshot309 Avatar answered Oct 04 '22 13:10

hotshot309


To set divider in listview programatically:

These code put inside in your .java Class

   ListView lv = (ListView) findViewById(R.id.lv);
   lv.setDivider(getResources().getDrawable(R.drawable.drawable_divider));
   lv.setDividerHeight(1);

Creating Drawable: {res > drawable > drawable_divider.xml}

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <solid android:color="#ececec"></solid>

</shape>
like image 33
Dhruv Raval Avatar answered Oct 04 '22 14:10

Dhruv Raval


Try this code:

searchText.setBackgroundColor(getResources().getColor(R.color.wordColorBlack));
ListView lv = getListView();
lv.setDivider(getResources().getDrawable(R.drawable.divider2));
lv.setDividerHeight(2);
like image 34
Stefan Cizmar Avatar answered Oct 04 '22 12:10

Stefan Cizmar