I have a listview in that i want when my activity starts then the first item will have background and the other items don't have any background.After that if the user selects any other item then the background of that particular item will now be blue and the rest don't have any background.Please help me on this.
My layout_effect.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
<item>
<shape>
<solid android:color="#00a7eb" />
</shape>
</item>
</selector>
My GetView Code where i am setting the default Background
public View getView(int position, View convertView, ViewGroup parent) {
if(position == 0){
vi.setBackgroundResource(R.drawable.selection_effect);
vi.setSelected(true);
}
}
Any my onclickListner of the listview
onewayListView .setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Clicked", 1000).show();
}
});
My Listview xml
<ListView
android:id="@+id/lvDepartures"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:choiceMode="singleChoice"
android:layout_height="wrap_content"
android:listSelector="@drawable/selection_effect"
android:layout_width="wrap_content"/>
Please help me how could i solve this problem i am stucked in it from last 1day.
To select a single list view item, you can use various actions provided by Android ListView object: TouchItem , LongTouchItem , and similar actions – simulate touch or long touch on a specific list view item. SelectItem – selects or unselects the specified list view item.
It's worth to mentioned that the ListView is a kind of deprecated because the RecyclerView was introduced with the API 21 (Android Lollipop).
To add rows to a ListView you need to add it to your layout and implement an IListAdapter with methods that the ListView calls to populate itself. Android includes built-in ListActivity and ArrayAdapter classes that you can use without defining any custom layout XML or code.
For doing this what you can do is create a selector and apply it to parent view of your row.xml
Define color in strings.xml
<color name="blue">#009FE3</color>
Create a selector
<item android:drawable="@color/blue" android:state_activated="true" />
<item android:drawable="@color/blue" android:state_focused="true" />
Now apply it as android:background="@drawable/row_selector"
for parent view of your row.
Set ListView as CHOICE_MODE_SINGLE in java code
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
And finally setAdapter to your ListView.
listview.setAdapter(adapter);
For selection of default item in ListView use,
listview.setItemChecked(position, true);
I had created a demo example for the same you can download it from my github
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With