0

I have been trying to give a "Cards UI" type layout to my ListView and came across this tutorial. Using this implementation, I have tried to set up a ListView with the following code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_activity);
    // Show the Up button in the action bar.
    setupActionBar();

    ListView list = (ListView) findViewById(R.id.cardListView);
    final String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" }; // You have the necessary data to bind the list.

            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

            list.setAdapter(adapter);
}

However, I have been unable to give the ListView elements the background that is specified in the linked tutorial. They instead appear as follows (no cards background).

No Background

Could you please guide me on what I'm doing wrong?

2
  • Here's a better idea, use a Card-UI library - I liked this one: github.com/afollestad/Cards-UI Commented Jan 1, 2014 at 1:12
  • @svenoaks I did come across that but since this is my first android app, that was a bit over my head. Commented Jan 1, 2014 at 1:25

1 Answer 1

1

You need to set the list_item.xml file as the layout to use for the items, try this:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.list_item, android.R.id.text1, values);

Also, in the list_item.xml, add this line

android:id="@+android:id/text1"

In order for it to fill in the TextView.

Sign up to request clarification or add additional context in comments.

6 Comments

hmm do you have a pic of what it's supposed to look like?
I fixed it, thanks. I'm now trying to figure out how I can move the scrollbar to the right edge of the screen instead of its current position.
Try changing ListView margin's to padding.
inside ListView tag change android:layout_marginLeft="16dp" to android:paddingLeft="16dp" same for right.
nm i see it doesn't work, you would have to set the margin on the list items themselves maybe
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.