TriStateToggleButton


Source link: https://github.com/BeppiMenozzi/TriStateToggleButton

TriState Toggle Button

A fully customizable and super-easy tri-state toggle button (switch button if you prefer) for Android, based on iOS look and feel. Can act with three independent states, or with two states like a standard checkbox, or with two states plus one undefined.


  • Out-of-the-box working 3-state toggle
  • Fully customizable and styleable
  • Can become a classic 2-state toggle returning booleans
  • Can become a 2.5-state toggle: on/off and an unselectable mid button
  • Can be enabled / disabled
  • Can be programmatically controlled
  • Works both with clicks and with swipes


Setup (Gradle)

In your project's build.gradle file:

allprojects {

  repositories {

...

maven {
 url "https://jitpack.io" 
}

...
  
}
 
}
 

In your Application's or Module's build.gradle file:

dependencies {

  ...
  compile 'com.github.BeppiMenozzi:TriStateToggleButton:1.1.4'
  ... 
}
 

Setup (Eclipse)

Whaaaaat?

Minimal usage

Layout:

... xmlns:app="http://schemas.android.com/apk/res-auto" ... <it.beppi.tristatetogglebutton_library.TriStateToggleButton

android:layout_width="80dp"

android:layout_height="40dp"

android:id="@+id/tstb_1"  /> 

Listener:

... TriStateToggleButton tstb_1 = (TriStateToggleButton) findViewById(R.id.tstb_1);
 tstb_1.setOnToggleChanged(new TriStateToggleButton.OnToggleChanged() {

@Override

public void onToggle(TriStateToggleButton.ToggleStatus toggleStatus, boolean booleanToggleStatus, int toggleIntValue) {

 switch (toggleStatus) {

  case off: break;

  case mid: break;

  case on: break;

 
}

}

  
}
);
 ... 

Inside onToggle() you can use the ToggleStatus type values, or limit yourself to use booleans or integers (0, 1, 2) if you want it easy.

To have a two-states toggle button:

 <it.beppi.tristatetogglebutton_library.TriStateToggleButton

android:layout_width="80dp"

android:layout_height="40dp"

android:id="@+id/tstb_1"

app:tbIsMidSelectable="false"  /> 

To have a two-states toggle button, with an undefined starting value:

 <it.beppi.tristatetogglebutton_library.TriStateToggleButton

android:layout_width="80dp"

android:layout_height="40dp"

android:id="@+id/tstb_1"

app:tbIsMidSelectable="false"

app:tbDefaultStatus="mid"  /> 

Browse the full example here: Example

Attributes description

List of attributes with description:

tbBorderWidthWidth of the border of the widget
tdOffBorderColorColor of the width that appears with the button in state off. Used also for animations.
tbOffColorColor of the background of the toggle when off
tbMidColorColor of the background of the toggle with in mid position
tbOnColorColor of the background of the toggle when on
tbSpotColorColor of the handle of the toggle
tbAnimateTrue for animation
tbDefaultStatusStarting value for the toggle
tbIsMidSelectableIf false, the toggle becomes a standard two-states toggle, but can still assume the mid value if forced programmatically or set as default
tbSwipeSensitivityPixelsNumber of pixels a swipe must travel to fire a toggle event. Default is 200. If set to zero, swipes are disabled

New in 1.1.3

  • Fixed: error drawing toggle in mid position when visibility passed from GONE to VISIBLE

New in 1.1.0

  • Added swipe gesture management together with normal click
  • Gradle update
  • More documentation

New in 1.0.5

  • Fixed: added super in setEnabled()

New in 1.0.4

  • Fixed: setting a boolean value programmatically sometimes didn't update status

New in 1.0.3

  • Toggle now can set and return integer values (0, 1, 2)
  • Warning: onToggle() changed to include integer values
  • Added static functions to convert from/to booleans and integers to/from toggleStatus

Used in

Credits

This project is strongly based on (and contains parts of code of) the very beautiful Toggle Button by zcweng.

Author

  • Beppi Menozzi

License

The MIT License (MIT)  Copyright (c) 2016 Beppi Menozzi  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

Resources

Swipe detects swipe events on Android with listener and RxJava Observable.

An easy to use android color picker library.

A library which can get phone number geo location and other information from local or network (baidu, juhe or custom api).

Detects gestures on Android with listener and RxJava Observable.

AndRouter is a android framework used to map url to activities or actions.

APT processor to generate update methods for POJOs.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes