Android: Extending Checkedtextview Source Code
I was thinking of adding an icon to the items in a ListView who already has a text and a checkbox: simple_list_item_multiple_choice.xml which is nothing but a < CheckedTextView
Solution 1:
As mentioned in the comments above, you don't need to go to source if all you want to do is add an icon to the ListView items. Just create a new layout that described what you want. I've included an example below, but that's just one way of doing it.
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="?android:attr/listPreferredItemHeight"android:paddingLeft="6dp"android:paddingRight="6dp"><ImageViewandroid:id="@+id/icon1"android:layout_width="wrap_content"android:layout_height="match_parent"android:src="@drawable/ic_launcher" /><CheckedTextViewandroid:id="@android:id/text1"android:layout_width="match_parent"android:layout_height="match_parent"android:textAppearance="?android:attr/textAppearanceLarge"android:gravity="center_vertical"android:checkMark="?android:attr/listChoiceIndicatorMultiple"android:layout_toRightOf="@id/icon1"
/></RelativeLayout>
then, in your Activity code do something like:
ListViewlistView= (ListView) findViewById(R.id.listView1);
// I'm just going to use an ArraySdapter, for simplicity...
listView.setAdapter(newArrayAdapter<String>(this, R.layout.item, android.R.id.text1, getResources().getStringArray(R.array.items)));
// or, for the sake of example (note, not optimized at all)
listView.setAdapter(newArrayAdapter<String>(this, R.layout.item, android.R.id.text1, getResources().getStringArray(R.array.items)) {
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
Viewview=super.getView(position, convertView, parent);
if (position % 2 == 0) {
((ImageView) view.findViewById(R.id.icon1)).setImageResource(R.drawable.ic_launcher);
} else {
((ImageView) view.findViewById(R.id.icon1)).setImageResource(R.drawable.ic_launcher);
}
return view;
}
});
Note, the above gives more flexibility, but you could also have just added an android:drawableLeft attribute to the CheckedTextView instead of adding the RelativeLayout and ImageView.
Post a Comment for "Android: Extending Checkedtextview Source Code"