Toggle button using two image on different state

AndroidTogglebutton

Android Problem Overview


I need to make a toggle button using two image instead of ON/OFF state.

At off state i set a background image.But the OFF text can not removed while i use background image.

And i can not set another image on ON state by clicking the toggle button :( I am new in android. I hope you guys help me get out of this problem

Android Solutions


Solution 1 - Android

Do this:

<ToggleButton 
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/check"   <!--check.xml-->
        android:layout_margin="10dp"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_centerVertical="true"/>

create check.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/selected_image"
          android:state_checked="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/unselected_image"
        android:state_checked="false"/>

 </selector>

Solution 2 - Android

AkashG's solution don't work for me. When I set up check.xml to background it's just stratched in vertical direction. To solve this problem you should set up check.xml to "android:button" property:

<ToggleButton 
    android:id="@+id/toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/check"   //check.xml
    android:background="@null"/>

check.xml:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/selected_image"
          android:state_checked="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/unselected_image"
          android:state_checked="false"/>
    </selector>

Solution 3 - Android

You can try something like this. Here on click of image button I toggle the imageview.

holder.imgitem.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View view) {
			if(!onclick){
            mSparseBooleanArray.put((Integer) view.getTag(), true);
            holder.imgoverlay.setImageResource(R.drawable.ipad_768x1024_editmode_delete_overlay_com);
			onclick=true;}
			else if(onclick)
			{
				 mSparseBooleanArray.put((Integer) view.getTag(), false);
				  holder.imgoverlay.setImageResource(R.drawable.ipad_768x1024_editmode_selection_com);
			
			onclick=false;
			}
		}
	});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMBMJView Question on Stackoverflow
Solution 1 - AndroidAkashGView Answer on Stackoverflow
Solution 2 - AndroidMistaGreenView Answer on Stackoverflow
Solution 3 - AndroidSrishti RoyView Answer on Stackoverflow