Questions tagged [android-switch]

Switch is an android class for a two state toggle switch

From Android Developer Documentation:

A Switch is a two-state toggle switch widget that can select between two options. The user may drag the "thumb" back and forth to choose the selected option, or simply tap to toggle as if it were a checkbox. The text property controls the text displayed in the label for the switch, whereas the off and on text controls the text on the thumb. Similarly, the textAppearance and the related setTypeface() methods control the typeface and style of label text, whereas the switchTextAppearance and the related seSwitchTypeface() methods control that of the thumb.

The Android Developer Guide regarding toggle buttons also states, that

The ToggleButton and Switch controls are subclasses of CompoundButton and function in the same manner, so you can implement their behavior the same way.

201 questions
123
votes
4 answers

How can I style an Android Switch?

The switch widget introduced in API 14 is styled by default with holo theme. I want to style it slightly different, changing its colors and shape a bit for branding reasons. How does one go about this? I know it must be possible, as ive seen the…
Glenn.nz
  • 2,261
  • 2
  • 17
  • 20
86
votes
10 answers

onCheckedChanged called automatically

I have a switch in a recyclerview and data is displayed in the recyclerview after retrieving data from DB. When the recyclerview is opened I read DB and if a field in DB is "Y" I enable the switch or else I disable the switch. Now the problem is…
73
votes
7 answers

How to change the size of a Switch Widget

In Ice Cream Sandwich a Switch Widget was introduced that displays an On Off Slider. I added the Switch like this:
Janusz
  • 187,060
  • 113
  • 301
  • 369
70
votes
15 answers

How to custom switch button?

I am looking to Custom The Switch Button to becoming as following : How to achieve this ?
TooCool
  • 10,598
  • 15
  • 60
  • 85
64
votes
12 answers

Android - programmatically change the state of a switch without triggering OnCheckChanged listener

I'm looking for a method of programmatically changing the state of an Android Switch widget using switch.setChecked(true); without triggering OnCheckedChangedlistener. My first thought was to swap it out for an OnClickListener but as this only…
56
votes
1 answer

Android Switch widget textOn and textOff not working in Lollipop

The behavior of the switch widget changed in Lollipop (5.0).
Jim Vitek
  • 4,174
  • 3
  • 23
  • 32
45
votes
8 answers

Change Switch state without animation

In my Android project, I have a ListView with rows containing SwitchCompat items (AppCompat for Switch widget). My problem occurs when I scroll into the list and getView(...) method of MyAdapter is invoked with a recycled view. I redefine the…
alex
  • 5,661
  • 6
  • 33
  • 54
44
votes
4 answers

How to use data binding for Switch onCheckedChageListener event?

As question indicates, how to bind checked change listener to Switch button in xml ? I am not using recycler view. Just a simple layout. Any help appreciated.
Mohanish Nerurkar
  • 541
  • 1
  • 5
  • 10
28
votes
2 answers

SwitchCompat throwing error

I am trying to use switchcompat in my fragment. The min supported API is 14 and max is 21. I am trying to apply the material view to switch for all pre lollipop android versions. But when use the below code I get an error: How can I correct…
Psypher
  • 10,717
  • 12
  • 59
  • 83
27
votes
4 answers

Warning : Use SwitchCompat from AppCompat or SwitchMaterial from Material library

What is the difference between these 2 objects (SwitchCompat and SwitchMaterial)? I have tried them and visually they are identical. By the way, why did they remove the Switch class? Do you know which UI element is supposed to replace it in the…
24
votes
1 answer

Preferences Support Library - SwitchPreference not working

A few days ago Google introduced the Preference Support Library (Link). I've just tried to implement it in my application though it seems like it's not working with SwitchPreferences which is weird because Google states that we can use the same…
22
votes
7 answers

How to have a Lollipop switch button

I want to have the Lollipop style switch button for my app: How could I implement this button so it looks like this also on older versions of android?
stoefln
  • 14,498
  • 18
  • 79
  • 138
21
votes
3 answers

Custom Switch - track and selector size not working below 21 API

Custom shape switch looks like that: Above API 21 Below API 21 Seems like block doesn't work in for pre 21 APIs. Any ideas how to solve this? CODE container.xml:
AnZ
  • 1,040
  • 24
  • 54
17
votes
2 answers

Handle touch event for items inside Recyclerview - android

i'm building a simple recyclerview with custom layout (Text view and switch) and a custom adapter. My problem is i can't handle the click event for the switch (Which make it toggle its state), i have built a touch listener for the RecyclerView and…
Fareed
  • 560
  • 2
  • 7
  • 23
16
votes
2 answers

How can I create custom switches in android with text on both side of switch's track an in thumb?

How can I design custom switch in android as like in below image: When it's turned on, it needs to look like this I also need to show toggle animation effects while switching between two categories. How Can I achieve it? Is there any 3rd party…
Thedroider
  • 229
  • 1
  • 5
  • 18
1
2 3
13 14