# SwitchView is an iOS-style switch in one Android class file

> iielse/switchbutton is a small Android view library that ships a single class, SwitchView, imitating the iOS toggle with a delay and a rollback action when a state change fails. It is trivial to integrate and equally trivial to fork, and its release history shows a project that has been quiet for a long time.

**iielse/switchbutton** — iOS风格SwitchButton。 只涉及到一个类文件，异常容易集成进入你的项目。支持开关的"延时和回滚"操作。only one class file, very easy to integrate into your program. support the delay operation and rolling back action of the switch

- Repository: https://github.com/iielse/switchbutton
- Stars: 684 · Forks: 187
- Language: Java
- License: not declared
- Published: 2026-10-21 · Updated: 2026-10-21 · Language: en
- Canonical page: https://hysenlabs.com/projects/iielse-switchbutton

## One class file, one view, no framework

The whole premise of this library is in its description. It is an iOS style SwitchButton for Android, it involves only one class file, and integrating it into a project is described as very easy. The follow-on detail is the interesting one: it supports the delay operation and the rolling back action of the switch. That is the reason to use this instead of the platform SwitchMaterial, because a plain toggle has no concept of a state change that fails and has to be undone. The repository layout backs up the single file claim, with a switchbutton/ directory holding the library module, an androidApp/ directory holding the demo application, a previews/ directory with the screenshots and the demo APK, and the usual Gradle scaffolding at the root including gradlew, gradlew.bat, settings.gradle.kts and build.gradle.kts. So this is a conventional Android library module with a deliberately tiny surface, not a framework with a dependency graph behind it.

## The Gradle coordinate and the XML view

Adding the library is a single dependency line, and the README gives the coordinate as implementation 'com.github.iielse:switchbutton:1.0.5' in a Gradle project. The view itself is then declared in a layout, using the fully qualified class name.

```xml
<com.github.iielse.switchbutton.SwitchView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
```

The class is in the package com.github.iielse.switchbutton and the view is called SwitchView, which is worth noticing because the repository is named switchbutton while the view class is named SwitchView. There is no custom attribute namespace to declare in this example, no styling reference and no theme requirement, so a layout with only the two wrap_content dimensions is enough to get a working control. The README also links a demo APK, so you can see the actual rendering and the animations before you add anything to your own build.

## Two listener styles, one of which hands you control

The library offers two ways to react to user input, and the difference between them is the whole design point. The first is an ordinary click listener, where you read the current state after the tap.

```java
switchView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        boolean isOpened = switchView.isOpened();
    }
});
```

The second is a state change listener with two callbacks, one for moving to the on state and one for moving to the off state, and each callback receives the view so you can call toggleSwitch on it.

```java
switchView.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
    @Override
    public void toggleToOn(SwitchView view) {
        view.toggleSwitch(true); // or false
    }

    @Override
    public void toggleToOff(SwitchView view) {
        view.toggleSwitch(false); // or true
    }
});
```

The comments in those snippets are the crux. In toggleToOn you can call toggleSwitch(true) to accept the change, or toggleSwitch(false) to refuse it and put the switch back where it was. The same applies in the other callback with the value inverted. So the control does not merely notify you, it asks permission, and a state change that your code rejects is visually rolled back. That is the mechanism behind the delay and rollback behaviour the description advertises, and it is a pattern the platform toggle does not offer, where a rejected write leaves the UI lying about the state.

## The attribute set is small and mostly visual

The documentation lists eight attributes, and reading them tells you most of what the control does. hasShadow controls whether the button shows a shadow. primaryColor is the background colour in the on state, and primaryColorDark is the border colour of the button in that state. offColor is the border colour in the off state, and offColorDark is the button border colour in the off state, which reads oddly but is how the table names it. shadowColor sets the button shadow colour. ratioAspect is the width to height shape ratio of the button, with a documented range of zero to one inclusive, and the table warns against adjusting it by a large amount. isOpened sets the initial default state. The mix tells you the scope: this is a drawing and state machine, not a themed Material component. There is no documented support for accessibility labels, no documented dark mode variant, and no animation duration setting, so the visual defaults are fixed and the only real configuration levers are colour and the on/off initial state.

## The release history tells you what kind of dependency this is

The version list is worth reading carefully. The current release is 1.0.5, dated 2026-06-10, and the previous one is 1.0.4 from 2020-01-29, with 1.0.3 from 2019-07-10 before that. So the project went quiet for six years and then shipped one release in June 2026, and the last push to the repository was on the same day, 2026-06-10. The repository is not archived, so this reads as a maintainer returning to a small project rather than a project that was formally ended. What that means for you depends on how you want to use it. If you are comfortable vendoring a single class file into your own source tree, the maintenance question mostly disappears, because the code is small enough to read, and the alternative to depending on it is writing the same thing. If you want an artifact that someone else upgrades, the record is thin, and the licence field is empty, which is a separate problem: the repository records no licence, so the terms under which you may redistribute the class are not stated, and that is a question for the author rather than one this article can answer. The practical recommendation for a one-file view is to copy the file into your project and own it, which also removes the dependency question entirely.

## Where the iOS look is the wrong goal

Two situations where reaching for this control is a mistake. The first is a design system that already has a switch. If your app uses Material 3 and the platform SwitchMaterial, a hand-drawn iOS toggle will not match the rest of your settings screen, and mixing the two is visible to users. The second is any screen where the state is genuinely asynchronous, because the rollback mechanism is exactly the right tool there but the library is not the place to enforce it. When a toggle triggers a network write, the correct pattern is a switch whose state follows the server response, and the delay and rollback behaviour has to be driven by your own state model rather than by an animation callback, because the delay the library offers is a UI delay, not a request. There is also the accessibility question, which the documentation simply does not address. No content description handling, no announced state change and no talk-back behaviour is described, and for a control whose entire purpose is to represent a boolean, that gap matters more than the visual fidelity. If accessibility is a requirement in your organisation, the platform switch with a correct content description is the safer choice.

## Conclusion

Adopt switchbutton if you need the iOS look for a toggle in an Android app and you want a dependency you can read in one sitting, since the whole control is a single class file with a small attribute set. Skip it if you need accessibility semantics, animation control or a maintained component library, because none of those are described in the documentation, and skip it if you need a licensed dependency you can ship, since the repository records no licence. Before you vendor the file, check the delay and rollback behaviour against your own state model, and confirm the Gradle coordinate and version you pin still resolve, because the published artifact is 1.0.5 from 2026-06-10 and the version before that dates from 2020.

## FAQ

### How do I add the switchbutton library to an Android project?

Add the Gradle dependency, given in the README as implementation 'com.github.iielse:switchbutton:1.0.5', then declare the view in a layout as com.github.iielse.switchbutton.SwitchView with wrap_content width and height.

### How do I roll back a switch change in switchbutton?

Use the OnStateChangedListener rather than a plain click listener. In the toggleToOn callback you call view.toggleSwitch(true) to accept the change or toggleSwitch(false) to refuse it, and the toggleToOff callback works the same way with the values inverted.

### Which attributes does the SwitchView support?

The documented attributes are hasShadow, primaryColor, primaryColorDark, offColor, offColorDark, shadowColor, ratioAspect for the button shape ratio between zero and one, and isOpened for the initial state.

### Is the switchbutton library still maintained?

The repository is not archived, and version 1.0.5 was released on 2026-06-10 with the last push on the same day, but the release before that was 1.0.4 from 2020-01-29. The repository records no licence, so the terms for redistributing the class are not stated.

## Sources

- [iielse/switchbutton on GitHub](https://github.com/iielse/switchbutton)
- [Issues](https://github.com/iielse/switchbutton/issues)
- [README](https://github.com/iielse/switchbutton/blob/master/README.md)
- [Releases](https://github.com/iielse/switchbutton/releases)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/iielse-switchbutton
