Checkable Relative Layout As Item In Multiselect List
I have android app with list view with choice mode set to multiple choice modal. Items on this list are defined as relative layouts. How can I change the background of items when t
Solution 1:
You can use a selector
In your Relative Layout Add
android:background=@drawable/bkg"
Then in drawable folder have bk.xml
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:drawable="@drawable/pressed" /><itemandroid:state_focused="false"android:drawable="@drawable/normal" /></selector>
pressed.xml
<?xml version="1.0" encoding="UTF-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"><solidandroid:color="#FF1A47"/> // change the colors to meet your requirement
<strokeandroid:width="3dp"android:color="#0FECFF"/><paddingandroid:left="5dp"android:top="5dp"android:right="5dp"android:bottom="5dp"/><cornersandroid:bottomRightRadius="7dp" // forroundedcornerremovethisifnotrequiredandroid:bottomLeftRadius="7dp"android:topLeftRadius="7dp"android:topRightRadius="7dp"/></shape>
normal.xml
<?xml version="1.0" encoding="UTF-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"><solidandroid:color="#FFFFFF"/><strokeandroid:width="3dp"android:color="#0FECFF" /><paddingandroid:left="5dp"android:top="5dp"android:right="5dp"android:bottom="5dp"/><cornersandroid:bottomRightRadius="7dp"android:bottomLeftRadius="7dp"android:topLeftRadius="7dp"android:topRightRadius="7dp"/></shape>
Edit:
The sample can be found @
android-sdk-linux/samples/android-17/ApiDemos/src/com/example/android/apis/view/List16
From your comments and discussion i assume this is what you want.
publicclassMainActivityextendsListActivity {
String[] GENRES = newString[] {
"Action", "Adventure", "Animation", "Children", "Comedy",
"Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller"
};
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListViewlv= getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(newModeCallback());
setListAdapter(newArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, GENRES));
}
@OverrideprotectedvoidonPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getActionBar().setSubtitle("Long press to start selection");
}
privateclassModeCallbackimplementsListView.MultiChoiceModeListener {
publicbooleanonCreateActionMode(ActionMode mode, Menu menu) {
MenuInflaterinflater= getMenuInflater();
inflater.inflate(R.menu.list_select_menu, menu);
mode.setTitle("Select Items");
returntrue;
}
publicbooleanonPrepareActionMode(ActionMode mode, Menu menu) {
returntrue;
}
publicbooleanonActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
Toast.makeText(MainActivity.this, "Shared " + getListView().getCheckedItemCount() +
" items", Toast.LENGTH_SHORT).show();
mode.finish();
break;
default:
Toast.makeText(MainActivity.this, "Clicked " + item.getTitle(),
Toast.LENGTH_SHORT).show();
break;
}
returntrue;
}
publicvoidonDestroyActionMode(ActionMode mode) {
}
publicvoidonItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
finalintcheckedCount= getListView().getCheckedItemCount();
switch (checkedCount) {
case0:
mode.setSubtitle(null);
break;
case1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + checkedCount + " items selected");
break;
}
}
}
}
list_select_menu.xml
<menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/share"android:title="share"android:icon="@android:drawable/ic_menu_share"android:showAsAction="always" /></menu>
Snap shot
Solution 2:
You can use a drawable selector for this. Create a new xml in the res/drawable folder e.g. list_item_background.xml and use this to select 2 different drawables for default state and selected state:
list_item_background.xml:
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/listitem_selected"android:state_selected="true" /><itemandroid:drawable="@drawable/listitem_normal" /></selector>
Post a Comment for "Checkable Relative Layout As Item In Multiselect List"