How To Highlight Selected List Item In Android?
Solution 1:
Your Query :
my problem is how can i highlight the selected item after user clicks on list item.
I think you are asking about selector. Mean if the list row in focus state then it should be look different form all other row. Same thing when you press or Touch the Row.
For that you have to make Selector.xml File in Drawable folder and just put that selector file in your list row
That file should have different tag like Focus-Click-Press
and change the Drawable as per state.
Update :
Just Replace Your icon and save in Drawable folder.
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><!-- Pressed --><itemandroid:drawable="@drawable/p_paly_press"android:state_pressed="true"/><!-- selected --><itemandroid:drawable="@drawable/p_play"android:state_selected="true"/><!-- focused --><itemandroid:drawable="@drawable/p_paly_press"android:state_focused="true"/><!-- default --><itemandroid:drawable="@drawable/p_play"/></selector>
Solution 2:
The code similar to the following code below can be used to highlight the selected item for sure if other ways do not do that you need:
classAdapterextendsArrayAdapter<String> {
privateintselectedPos= -1;
Drawable selectedBackground;
publicMainSelectAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
selectedBackground =
context.getResources().getDrawable(R.color.selecteditembackground);
}
publicvoidsetSelectedPosition(int pos){
selectedPos = pos;
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
Viewv=super.getView(position, convertView, parent);
if (selectedPos == position) {
v.setBackgroundDrawable(selectedBackground);
} else {
v.setBackgroundDrawable(null);
}
return v;
}
}
And the in the main activity
Adapteradapter=newAdapter(this, android.R.layout.simple_list_item_1,
myItemsToShow);
list = (ListView) findViewById(R.id.flows);
list.setItemsCanFocus(true);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(newAdapterView.OnItemClickListener() {
publicvoidonItemClick(AdapterView<?> adapter, View view,
int pos, long id) {
adapter.setSelectedPosition(pos);
}
});
With this approach, you take own control on the selected item highlighting, you have a listener to capture selection events and you set yourself the required Drawable.
Solution 3:
You can define in your styles.xml
<stylename="Theme.Base"parent="..."><itemname="activatableItemBackground">@drawable/activatable_item_background</item></style><stylename="ListItemContainerBase"><itemname="android:background">?activatableItemBackground</item></style>
In res/drawable define activatable_item_background.xml
<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/item_pressed"android:state_pressed="true" /><itemandroid:drawable="@drawable/item_focused"android:state_focused="true" /><itemandroid:drawable="@drawable/item_focused"android:state_selected="true" /><itemandroid:drawable="@drawable/item_activated"android:state_activated="true" /><itemandroid:drawable="@drawable/item_checked"android:state_checked="true" /><itemandroid:drawable="@android:color/transparent" /></selector>
item_pressed, item_focused..... are images in res/drawable-xxx
Define for each items in your view a layout like this:
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"style="@style/ListItemContainerBase">
Post a Comment for "How To Highlight Selected List Item In Android?"