Adding custom switches in android

I am trying to get a radio object effect for regular buttons in android

I have a simple switch for android below

enter image description here

Code for this :

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="RadioButton1" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton2" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton3" /> </RadioGroup> </RelativeLayout> 



How to configure it as below:

enter image description here

Thank you !

[EDIT] using code from one of the answers

enter image description here

But the name of the button is clouded by the selection option, how to remove it?




{EDIT} more changes

Final changes should, at least, I should know which button I selected from the three switches ... can I get it as below?

enter image description here

+86
android xml
03 Oct '13 at 15:50
source share
9 answers

Add a background selection that references the image or selector (for example, below), and make the button transparent:

 <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/yourbuttonbackground" android:button="@android:color/transparent" android:checked="true" android:text="RadioButton1" /> 

If you want your switches to have a different resource when it's set, create a selector background selection:

Res / draw / yourbuttonbackground.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/b" android:state_checked="true" android:state_pressed="true" /> <item android:drawable="@drawable/a" android:state_pressed="true" /> <item android:drawable="@drawable/a" android:state_checked="true" /> <item android:drawable="@drawable/b" /> </selector> 

In the selector above, we refer to two drawings, a and b , here, how we create them:

res / drawable / a.xml - Selected state

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="5dp" /> <solid android:color="#fff" /> <stroke android:width="2dp" android:color="#53aade" /> </shape> 

res / drawable / b.xml - Regular state

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="5dp" /> <solid android:color="#fff" /> <stroke android:width="2dp" android:color="#555555" /> </shape> 

Read more about drawables here: http://developer.android.com/guide/topics/resources/drawable-resource.html

+183
03 Oct '13 at 16:08
source share

Use the same XML file format from Evan's answer, but for formatting you only need one extractable file.

 <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/custom_button_background" android:button="@android:color/transparent" android:checked="true" android:text="RadioButton1" /> 

And your separate file:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" > <shape android:shape="rectangle" > <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#333333" /> <solid android:color="#cccccc" /> </shape> </item> <item android:state_checked="true"> <shape android:shape="rectangle" > <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#333333" /> <solid android:color="#cccccc" /> </shape> </item> <item> <shape android:shape="rectangle" > <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#cccccc" /> <solid android:color="#ffffff" /> </shape> </item> </selector> 
+32
Mar 10 '14 at 4:22
source share

You must populate the "Button" attribute of the CompoundButton class using the XML drawing path ( my_checkbox ). In an XML drawing, you should have:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="false" android:drawable="@drawable/checkbox_not_checked" /> <item android:state_checked="true" android:drawable="@drawable/checkbox_checked" /> <item android:drawable="@drawable/checkbox_not_checked" /> <!-- default --> </selector> 

Remember to replace my_checkbox in your file name with a check box, checkbox_not_checked with your PNG code, which is your check box if it is not set, and checkbox_checked with your image when it is checked.

For size, directly update the layout options.

+7
Oct 03 '13 at 16:11
source share

To hide the default switch, I suggest removing the button instead of making it transparent, since all visual feedback is handled by the extracted background:

 android:button="@null" 

It would also be better to use styles , as there are several switches:

 <RadioButton style="@style/RadioButtonStyle" ... /> <style name="RadioButtonStyle" parent="@android:style/Widget.CompoundButton"> <item name="android:background">@drawable/customButtonBackground</item> <item name="android:button">@null</item> </style> 

You will need a Seslyn customButtonBackground .

+3
Jun 11 '15 at 2:36
source share

The best way to add a custom way:

  <RadioButton android:id="@+id/radiocar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@android:color/transparent" android:button="@drawable/yourbuttonbackground" android:checked="true" android:drawableRight="@mipmap/car" android:paddingLeft="5dp" android:paddingRight="5dp" android:text="yourtexthere"/> 

Shadow overlay using custom drawable is removed here.

+3
Oct 10 '16 at 4:32
source share

An easy way to try it

  • Create a new layout in the drop-down folder and name it custom_radiobutton (you can also rename it)

      <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="false" android:drawable="@drawable/your_radio_off_image_name" /> <item android:state_checked="true" android:drawable="@drawable/your_radio_on_image_name" /> </selector> 
  • Use this in your layout activities

     <RadioButton android:id="@+id/radiobutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/custom_radiobutton"/> 
+2
Apr 03 '17 at 7:27
source share

The following is an example of a custom switch. follow these steps.

  • Xml file.

      <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.5"> <TextView android:id="@+id/text_gender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:gravity="center" android:text="@string/gender" android:textColor="#263238" android:textSize="15sp" android:textStyle="normal" /> <TextView android:id="@+id/text_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:gravity="center" android:text="@string/male" android:textColor="#263238" android:textSize="15sp" android:textStyle="normal"/> <RadioButton android:id="@+id/radio_Male" android:layout_width="28dp" android:layout_height="28dp" android:layout_gravity="right|center_vertical" android:layout_marginRight="4dp" android:button="@drawable/custom_radio_button" android:checked="true" android:text="" android:onClick="onButtonClicked" android:textSize="15sp" android:textStyle="normal" /> </FrameLayout> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.6"> <RadioButton android:id="@+id/radio_Female" android:layout_width="28dp" android:layout_height="28dp" android:layout_gravity="right|center_vertical" android:layout_marginLeft="10dp" android:layout_marginRight="0dp" android:button="@drawable/custom_female_button" android:text="" android:onClick="onButtonClicked" android:textSize="15sp" android:textStyle="normal"/> <TextView android:id="@+id/text_female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:gravity="center" android:text="@string/female" android:textColor="#263238" android:textSize="15sp" android:textStyle="normal"/> <RadioButton android:id="@+id/radio_Other" android:layout_width="28dp" android:layout_height="28dp" android:layout_gravity="center_horizontal|bottom" android:layout_marginRight="10dp" android:button="@drawable/custom_other_button" android:text="" android:onClick="onButtonClicked" android:textSize="15sp" android:textStyle="normal"/> <TextView android:id="@+id/text_other" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:layout_marginRight="34dp" android:gravity="center" android:text="@string/other" android:textColor="#263238" android:textSize="15sp" android:textStyle="normal"/> </FrameLayout> </LinearLayout> 

    2.add custom xml for switches

    2.1.other available

    custom_other_button.xml

     <item android:state_checked="true" android:drawable="@drawable/select_radio_other" /> <item android:state_checked="false" android:drawable="@drawable/default_radio" /> 

    2.2. Modified female

    custom_female_button.xml

     <item android:state_checked="true" android:drawable="@drawable/select_radio_female" /> <item android:state_checked="false" android:drawable="@drawable/default_radio" /> 

    2.3. male traction

    custom_radio_button.xml

     <item android:state_checked="true" android:drawable="@drawable/select_radio_male" /> <item android:state_checked="false" android:drawable="@drawable/default_radio" /> 

    1. Output: This is the output screen.
+1
Aug 19 '17 at 19:30
source share

I understand that this is a belated answer, but looking through developer.android.com it seems that the Toggle button is perfect for your situation.

Toggle button image http://developer.android.com/guide/topics/ui/controls/togglebutton.html

And, of course, you can still use other offers to get a wallpaper to get your desired custom look.

 <ToggleButton android:id="@+id/togglebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/custom_button_background" android:textOn="On" android:textOff="Off" /> 

Now, if you want to go to the final editing and have a β€œhalo” effect around your buttons, you can use another custom selector to do just that.

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" > <!-- selected --> <shape> <solid android:color="@android:color/white" /> <stroke android:width="3px" android:color="@android:color/holo_blue_bright" /> <corners android:radius="5dp" /> </shape> </item> <item> <!-- default --> <shape> <solid android:color="@android:color/white" /> <stroke android:width="1px" android:color="@android:color/darker_gray" /> <corners android:radius="5dp" /> </shape> </item> </selector> 
0
Jun 11 '14 at
source share

Hi, here is a modern way to do what you want. Create a theme here, ideally, from the very beginning of the project.

Android Holo Colors Generator

Then you will see how this is done and you can configure it as you want. Enjoy it!

0
Nov 26 '15 at 12:54
source share



All Articles