How to change the color of toggleButton in listView?
This is how I deigned 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">
<!-- When selected, use tic -->
<item android:drawable="@mipmap/done"
android:state_checked="true" >
<solid
android:color="@color/red" />
</item>
<!-- When not selected, use un tic-->
<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;
}
It displays log
when toggleButton
is clicked, but the color still remain white(its original color). How can I change the color of toggleButton
?
Updated
I have updated my code, but the color still remains white ! I want to use the done.png and I have placed it to res/drawable/done.png
.
selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use tic -->
<item android:drawable="@drawable/toggle_on"
android:state_checked="true" >
</item>
<!-- When not selected, use un tic-->
<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>
<item android:drawable="@mipmap/done"
is the same for both selected and unselected states: it should be 2 different drawables. 2 - Themipmaps
folder is to be used for the app icon only. – Fricationandroid:tint
cannot be used in selector ? – Bomanandroid:tint
attribute does not work on all API Levels. – Frication<item android:drawable="@mipmap/done"/>
as I want to use this icon – Bomanres/drawable-*dpi
folder? Where * depends on the device density (l,m, h, xh,xxh, xxxh). – Fricationdone
is an icon where I place it inres/mipmap-hdpi
folder. I want to use it for 2 different states. I just want to change their color. – Bomanmipmap-*dpi
is only and exclusively to be used for your app icon. Not for all the other icons in your app. Make 2 drawables:done_on
anddone_off
and refer those ones in your selector. – Fricationdone
todrawable-*dpi
.Refer to vrund's answer, I also make two drawable folder....But where should I put<item android:drawable="@drawable/done
so I can use the image? – Bomanres/drawable
. It's a drawable, and doesn't depend on the density. – FricationI want to use the done.png
as I said, you should use 2 pngs. I'd name themdone_on.png
anddone_off.png
– Frication