billiout
billiout

Reputation: 705

Android: Change the style of ListView

I've made a ListView and it looks like the first image but I want to make it like the normal ListView(like the second image). I'm using TextView for the items and a simple custom adapter.

1st

enter image description here

2nd

enter image description here

activity.xml

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

     <ListView
          android:id="@+id/list"
          android:layout_height="wrap_content"
          android:layout_width="match_parent">
     </ListView>

</LinearLayout>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Item name here..."/>

Custom adaptor

public class TextVAdapter extends ArrayAdapter<CategoryItem> {

    Context mContext;
    int layoutResourceId;
    List<CategoryItem> data = null;

    public TextVAdapter(Context retrieveFeedTask, int layoutResourceId, List<CategoryItem> messages) {

        super(retrieveFeedTask, layoutResourceId, messages);

        this.layoutResourceId = layoutResourceId;
        this.mContext = retrieveFeedTask;
        this.data = messages;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        /*
         * The convertView argument is essentially a "ScrapView" as described is Lucas post
         * http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
         * It will have a non-null value when ListView is asking you recycle the row layout.
         * So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
         */
        if(convertView==null){
            // inflate the layout
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();                
            convertView = inflater.inflate(layoutResourceId, parent, false);
        }

        // object item based on the position
        CategoryItem objectItem = data.get(position);

        // get the TextView and then set the text (item name) and tag (item ID) values
        TextView tv = (TextView) super.getView(position, convertView, parent);
        tv.setText(objectItem.getTitle());

        return convertView;

    }

}

Upvotes: 0

Views: 951

Answers (3)

OmerFaruk
OmerFaruk

Reputation: 292

It's simple to do that. Because there is sample listviews in android system. You dont have to use much code.Just Like this

public class MainActivity extends ListActivity{
String stringarray[]={"Apple","Grape","Orange"};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stringarray));
}


}

Upvotes: 1

Devrath
Devrath

Reputation: 42824

It is really simple - you can do this in many ways

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@android:color/black"
android:baselineAligned="false">

     <ListView
          android:id="@+id/list"
          android:background="@android:color/black"
          android:layout_height="wrap_content"
          android:layout_width="match_parent">
     </ListView>

</LinearLayout>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="@android:color/white"
android:gravity="center"
android:text="Item name here..."/>

test and do minor changes as you wish, let me know if you need any help

Upvotes: 1

Andrii Bas
Andrii Bas

Reputation: 623

if you need it to be simple, do as an answer from @OmerFaruk,

if you need more control over your code, try to extract properties of standard layouts (like padding, textAppearence, background, gravity) for list items, such as android.R.layout.simple_list_item_1, android.R.layout.simple_list_item_activated_1, android.R.layout.simple_list_item_single_choice or similar, and insert them into your "items.xml"

Upvotes: 1

Related Questions