Android List View , how to give a border and a background color to individual items in android list -
i have created android list , want give border color individual list items checking variable value , color background according value. work far .
the layout
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <listview android:id="@+id/android:myalertlist" android:layout_width="250dp" android:layout_height="wrap_content" android:choicemode="singlechoice" android:clickable="true" android:background="@drawable/border_ui" android:drawselectorontop="false" > </listview> </relativelayout>
my list view
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/txtone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textisselectable="false" /> <textview android:id="@+id/txttwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textisselectable="false" /> </linearlayout>
@drawable/border_ui declaration
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
-->
activity java code
package test.application;
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); hashmap<string, string> hashmapone = new hashmap<string, string>(); hashmapone.put("keyone", "export"); hashmapone.put("key_two", "a"); hashmap<string, string> hashmaptwo = new hashmap<string, string>(); hashmaptwo.put("keyone", "import"); hashmaptwo.put("key_two", "b"); hashmap<string, string> hashmapthree = new hashmap<string, string>(); hashmapthree.put("keyone", "import"); hashmapthree.put("key_two", "c"); hashmap<string, string> hashmapfour = new hashmap<string, string>(); hashmapfour.put("keyone", "export"); hashmapfour.put("key_two", "c"); arraylist<hashmap<string, string>> hashlist = new arraylist<hashmap<string, string>>(); hashlist.add(hashmapone); hashlist.add(hashmaptwo); hashlist.add(hashmapthree); hashlist.add(hashmapfour); setcontentview(r.layout.activity_main); simpleadapter simpleadapter = new simpleadapter(mainactivity.this, hashlist, r.layout.list_test, new string[] { "keyone", "key_two" }, new int[] { r.id.txtone, r.id.txttwo }); final listview lv = (listview) findviewbyid(r.id.android_myalertlist); lv.setadapter(simpleadapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
i still have no clue highlight , color individual items. if can guide me thankful.
you should create own adepter class , row layout. please @ exaple.
and in getviev event should change background
public class mycustomadapter extends simpleadapter { public mycustomadapter(context context, list<? extends map<string, ?>> data, int resource, string[] from, int[] to) { super(context, data, resource, from, to); } static class viewholder { protected textview label; protected linearlayout layout; } @override public view getview(int position, view convertview, viewgroup parent) { view view = null; layoutinflater inflator = context.getlayoutinflater(); view = inflator.inflate(r.layout.row_task_layout, null); final viewholder viewholder = new viewholder(); viewholder.label = (textview) view.findviewbyid(r.id.label); viewholder.layout= (lineerlayout) view.findviewbyid(r.id.layout); view.settag(viewholder); viewholder holder = (viewholder) view.gettag(); holder.label.settext(data.get(position).title); if(statement) { holder.layout.setbackground(background); } return view; } }
rowlayout
<?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="wrap_content" android:background="#f3f3f3" android:gravity="center" android:padding="5dip" > <linearlayout android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="wrap_parent" android:background="@drawable/button_style_main_single" android:paddingleft="5dip" android:paddingright="10dip" > <textview android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" /> </linearlayout> </linearlayout>
Comments
Post a Comment