Toggle button using two images in different states

I need to make a switch button using two images instead of the ON / OFF state.

In the off state, I set the background image. But the text OFF cannot be deleted when I use the background image.

And I can't set the other image to ON by pressing the toggle button :( I am new to android. I hope you guys help me out of this problem.

+82
android togglebutton
Jul 16 2018-12-12T00:
source share
3 answers

Do it:

<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 the folder with the ability to transfer

 <?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> 
+189
Jul 16 '12 at 7:10
source share

AkashG solution does not work for me. When I set check.xml for the background, it just splits in the vertical direction. To solve this problem, you should configure check.xml to the "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> 
+37
Jul 31 '13 at 3:16
source share

You can try something like this. Here, when I click the image button, I switch the image.

 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; } } }); 
+2
Jul 24. '14 at 10:34
source share



All Articles