AndroidDev
AndroidDev

Reputation: 4559

Changing style of listview

I had created a listview in my app..now i want to change the fontcolor of text..background of text and make the list disabled..so how that can be done..i am sending my code of crating listview..anyone please check it where this chnages can be done..

super.onCreate(icicle);
        setContentView(R.layout.contact_activity);
        lv1=(ListView)findViewById(R.id.ListView01);    
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
        lv1.setOnItemClickListener(new OnItemClickListener() 
        {

            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            {

                      String selecteditem = lv_arr[position];
                      Intent myIntent = new Intent(view.getContext(), ContactInfo.class);
                      myIntent.putExtra("item", selecteditem);
                      startActivity(myIntent);
            }
        });

Upvotes: 0

Views: 379

Answers (2)

Kenny
Kenny

Reputation: 5542

You need to change the rows, not the ListView. Instead of using android.R.layout.simple_list_item_1, when calling setAdapter, create your own row layout that is styled the way you want it to be.

For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
>
<ImageView
android:id="@+id/icon"
android:padding="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tick"
/>
<TextView
  android:id="@+id/label"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="40sp"
/>
</LinearLayout

This layout uses a LinearLayout to set up a row, with a icon on the left and the text (in a nice big font) on the right.

Then your .setAdapter would be:

lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.row , lv_arr));

Hope this helps to get you started, not quite sure what you mean by wanting the list disabled, perhaps you could clarify a bit!

Upvotes: 0

manuel
manuel

Reputation: 847

You can extend the ArrayAdapter to use a custom row (defined by you) which has the colors and any styles you want. I have an example here http://manuelzs.posterous.com/creating-a-custom-listview.

Upvotes: 1

Related Questions