Reputation: 101
I am working with list view in android. I want to set background color for each item in list view.
If I use setbackgroudcolor function, it displays incorrect color. It seems there is the mix color of application background and new color. If I use setbackgroundresource fund, it displays ok. However, when I click on an item, color is not change.
i need to set background for for each item on list view, and when click on an item, background is changed to other color or background
My code: OnView
// Set background
RelativeLayout root = (RelativeLayout) vi.findViewById(R.id.p60001_layout_info);
if (position % 2 == 0){
root.setBackgroundResource(R.drawable.cell_g_02);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}else{
root.setBackgroundResource(R.drawable.cell_g_01);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}
Row layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/p60001_layout_info"
android:layout_width="match_parent"
android:layout_height="30dp">
<TextView
android:id="@+id/p60001_item_info"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="15dp" android:layout_marginLeft="10dp" android:fadeScrollbars="false" android:textColor="@color/txt_normal" android:scrollHorizontally="true" android:gravity="center_vertical">
</TextView>
<ImageView
android:id="@+id/icon"
android:layout_width="22px"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow" android:layout_marginRight="15dp"/>
<ImageView
android:id="@+id/p60001_image_notice_num"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_marginRight="30dp"
android:layout_toLeftOf="@+id/icon"
android:layout_marginTop="5dp" android:src="@drawable/fukidashi" android:visibility="invisible" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:longClickable="false"/>
<TextView
android:id="@+id/p60001_item_notices"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/icon"
android:ems="10" android:layout_marginRight="40dp" android:background="@android:color/transparent" android:freezesText="false" android:focusableInTouchMode="false" android:focusable="false" android:clickable="false" android:selectAllOnFocus="false" android:textSize="20px" android:layout_marginTop="4dp" android:visibility="invisible">
<requestFocus />
</TextView>
</RelativeLayout>
Upvotes: 1
Views: 5644
Reputation: 34775
If you are using custom adapter concept and the above is your getView code.
you must a convert view(xml file of each row) change the background of it like below.
if (position % 2 == 0){
converterView.setBackgroundResource(R.drawable.cell_g_02);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}else{
converterView.setBackgroundResource(R.drawable.cell_g_01);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}
if you need to change the color on focus or pressed you use the below xml as background.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/red" />
<item android:state_selected="true"
android:drawable="@color/red" />
</selector>
also if need to set background for clicked items then a way to do this is::::
to have a bool variable(=false)in your pojo(bean) where you are storing the data of each item. so when ever you click on item make that variable to true. And in getView() just check whether that variable is true or false and do the need full accordingly.
Upvotes: 1
Reputation: 13562
Try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use selected state color -->
<item android:drawable="@drawable/your_drawable_when_selected"
android:state_selected="true" />
<!-- When not selected, use default item color-->
<item android:drawable="@drawable/your_drawable_when_unselected" />
</selector>
Upvotes: 1