0

I am new to Android programming so any help would be appreciated! I have a ListActivity as shown:

public class BasicViews5Activity extends ListActivity {

String[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setTextFilterEnabled(true);

    names = getResources().getStringArray(R.array.names_array);
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,names));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Toast.makeText(this,"You have selected " + names[position], Toast.LENGTH_SHORT).show();
}

}

How can I change the drawable of the CheckBox within the ListView?

My Xml looks like this:

    <?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="fill_parent"
    android:orientation="vertical" >

    <Button android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Show selected items"
        android:onClick="onClick"/>

    <ListView
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/checked"/>


</LinearLayout>
Cam1989
  • 1,241
  • 2
  • 9
  • 9

2 Answers2

0

Your question already has a response here:

I didn´t like the appearance of the checkbox default image when the background layout was dark, so I changed it using a xml selector in order to have a white background in the button.

The selector is needed in android:button="@drawable/selector_check"

Community
  • 1
  • 1
webo80
  • 3,365
  • 5
  • 35
  • 52
  • Thanks webo80, is this possible if I havn't got a ListView within my xml? I am Using ListActivity so I havn't created a Layout containing a ListView? – Cam1989 Jun 16 '15 at 14:25
  • OK, you are using a LIstView, but you must be using a XML for defining a row, a item of the list, with a checkbox. Or you are creating the item layout programmatically? – webo80 Jun 16 '15 at 14:27
  • Sorry, I now have some xml set up. I have put my XML in the question, do I have to have a custom row xml aswell? Is there a way to just set the icon from the ListView element? – Cam1989 Jun 16 '15 at 14:41
  • 1
    I have it :D Thank you soo much – Cam1989 Jun 16 '15 at 15:10
0

If any other newbies get stuck with a similar question:

You have a class that extends ListActivity or ListFragment. Within the onActivityCreated() method you need to set the list Adapater:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
 R.layout.rowlayout, R.id.label, values);

 setListAdapter(adapter);

Within your rowlayout.xml you need a TextView with the id 'label', this will be filled with the strings from your 'values' array.

Within rowlayout.xml you need a checkbox with the button attribute set:

android:button="@drawable/custom_checkbox_design"

Within custom_checkbox_design.xml you need the following:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/checked" android:state_checked="true"/>
<item android:drawable="@drawable/unchecked" android:state_checked="false"/>

</selector>
Cam1989
  • 1,241
  • 2
  • 9
  • 9