How to change toggleButton color in listView?
This is how I set out my toggleButton
<ToggleButton android:id="@+id/donePic" android:layout_width="30dp" android:layout_marginLeft="270dp" android:layout_height="30dp" android:background="@drawable/selector" android:focusable="false" android:focusableInTouchMode="false" android:textOff="" android:textOn=""/>
Selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/done" android:state_checked="true" > <solid android:color="@color/red" /> </item> <item android:drawable="@mipmap/done" android:state_checked="false"> <solid android:color="@color/green" /> </item> </selector>
Myactivity
public View getView(int position, View convertView, ViewGroup parent) { // inside adapter class ViewHolder holder; ToggleButton toggle; if (convertView == null) { convertView = mInflater.inflate(R.layout.item_to_do, null); toggle =(ToggleButton)convertView.findViewById(R.id.donePic); toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Log.e("A","a"); } else { // The toggle is disabled } } }); convertView.setTag(holder); } holder = (ViewHolder) convertView.getTag(); return convertView; }
Clicking toggleButton displays a log , but the color still remains white (its original color). How to change toggleButton color?
Update
I have updated my code, but the color is still white! I want to use done.png and I put it in res/drawable/done.png .
Selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/toggle_on" android:state_checked="true" > </item> <item android:drawable="@drawable/toggle_off" android:state_checked="false"> </item> </selector>
toggle_on
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/done" android:state_checked="true"> <solid android:color="@color/green" /> </item> </selector>
toggle_off
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/done" android:state_checked="false"> <solid android:color="@color/red" /> </item> </selector>
android colors listview togglebutton
John Sep 29 '16 at 2:00 p.m. 2016-09-29 14:00
source share