Android Library for Custom Switches.

Overview

Toggle

Android Library for Custom Switches.

Developed by

Angad Singh (@angads25)

Benchmark:

API Build Status

Mentions:

Download Maven Central

Read all about internal classes and functions in the wiki.

Installation

  • Library is also Available in MavenCentral, So just put this in your app dependencies to use it:
    implementation 'com.github.angads25:toggle:1.1.0'

Usage

  1. Start by adding a Switch (eg. LabeledSwitch) in your xml layout as:

    ">
        <com.github.angads25.toggle.widget.LabeledSwitch
            android:id="@+id/switch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:textSize="14sp"
            app:on="false"
            app:colorBorder="@color/colorAccent"/>
  2. To the reference of Switch in your Activity/Fragment class set a Toggle Event Handler to it as below:

        LabeledSwitch labeledSwitch = findViewById(R.id.switch);
        labeledSwitch.setOnToggledListener(new OnToggledListener() {
            @Override
            public void onSwitched(LabeledSwitch labeledSwitch, boolean isOn) {
                // Implement your switching logic here
            }
        });

    That's It. All your switching callbacks would be handled in onSwitched method, parameter isOn will provide the current state of the switch.

Switches Available

Labeled Switch

Day Night Switch

License

Copyright (C) 2018 Angad Singh

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Fix enabled support

    Fix enabled support

    The old version was replicating android:enabled but that is already available at the View level. When calling label.setEnabled() programmatically, the enabled flag would not be changed because it was not overriden.

    Now everything works as expected, both from XML and code, using the internal enabled flag (at the View level, no need to create another).

    opened by natario1 2
  • Exception when change state on/off

    Exception when change state on/off

    Hi,

    I tried your wonderfull toggle button on my a demo app but I have some trouble when clicking it :

    java.lang.BootstrapMethodError: Exception from call site #6 bootstrap method at com.github.angads25.toggle.widget.LabeledSwitch.performClick(LabeledSwitch.java:355) at com.github.angads25.toggle.widget.LabeledSwitch.onTouchEvent(LabeledSwitch.java:409) at android.view.View.dispatchTouchEvent(View.java:12529) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3030) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2719) at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:452) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1836) at android.app.Activity.dispatchTouchEvent(Activity.java:3400) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68) at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:410) at android.view.View.dispatchPointerEvent(View.java:12768) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5127) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4930) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4447) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4500) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4466) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4606) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4474) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4663) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4447) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4500) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4466) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4474) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4447) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7124) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7093) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7054) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7227) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:187) at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:178) at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7198) 2018-11-19 16:18:54.983 14675-14675/com.test.demo E/AndroidRuntime: at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7250) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1004) at android.view.Choreographer.doCallbacks(Choreographer.java:816) at android.view.Choreographer.doFrame(Choreographer.java:745) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:990) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:280) at android.app.ActivityThread.main(ActivityThread.java:6710) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.ClassCastException: Bootstrap method returned null

    If seems that "switchColor.addUpdateListener(animation -> {" on line 365 return null. This the code in the layout:

    <com.github.angads25.toggle.widget.LabeledSwitch
        android:id="@+id/option_classifier_enabled"
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:textSize="14sp"
        app:colorBorder="@color/colorAccent"
        app:layout_column="1"
        app:layout_gravity="center_vertical"
        app:layout_row="0"
        app:on="false"
        app:textOff="No"
        app:textOn="Yes" />
    

    I used you compoment in a fragment. It displays correctly but this error occured when clicking on it.

    If you have any idea about what's append.

    Thanks in advance,

    Best regards

    opened by CITIZENGATE 1
  • Updates and fix enabled support

    Updates and fix enabled support

    • Updates gradle wrapper
    • Updates build tools version
    • Updates android gradle plugin
    • Fix enabled support by simply removing it (it is already built-in in the View class).
    opened by natario1 0
  • Error opening dialog

    Error opening dialog

    Error appears despite Activity using Theme.AppCompat style in manifest Snippet: E/InputEventReceiver: Exception dispatching input event. E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback E/MessageQueue-JNI: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

    opened by oyiekeallen 0
  • Set Text Color

    Set Text Color

    Is there a way to set text color that is common for both on & off state? I would like to set the text color to white. I didn't find an attribute for this, did I miss something?

    Maybe something like app:colorOnText & app:colorOffText, would be amazing.

    Thanks!

    wontfix 
    opened by RamithRD 0
  • ToggleListener lacks generalization

    ToggleListener lacks generalization

    The library is suppost to support multiple type of switches, a generalized class would be helpful for implementing interface for different type of switches. ToggleListener

    enhancement 
    opened by singhangadin 0
  • [LabeledSwitch] No callback on performClick()

    [LabeledSwitch] No callback on performClick()

    The toggleListener returns a value when the switch is manually moved on touch. However, there is no callback when performClick() method is called explicitly.

    good first issue 
    opened by singhangadin 0
  • Font style issue

    Font style issue

    in this lable switch not accept the style in android is this possible to change the font style...? if it is possible how...? is there any other way to change the font for android toggle....?

    opened by crazyThamrai 0
  • Toggle text and color update

    Toggle text and color update

    Added different toggle and text color app:colorText="#ffffff" app:colorToggle="#ffffff" Added text modification option app:textUpperCase app:textLowerCase

    opened by jozsefmezei 0
  • Switch/Toggle size modification by XML

    Switch/Toggle size modification by XML

    This library is good. But if we have an open option to control toggle switch width/height and the corner radius it will be helpful for the developer I hope so.

    image

    opened by IstiyakV 0
Releases(v1.1.0)
Owner
Angad Singh
Angad Singh
Useful library to use custom fonts in your android app

EasyFonts A simple and useful android library to use custom fonts in android apps without adding fonts into asset/resource folder.Also by using this l

Vijay Vankhede 419 Sep 9, 2022
DailyTags - a flexible markdown library that supports custom tags and markups

The library parses a given markup into rich text for Jetpack Compose. DailyTags comes with Markdown and HTML support by default (please, see the supported features) and is very easy to extend to support custom markups.

Dmytro 134 Dec 25, 2022
Custom android music player view.

InteractivePlayerView Custom android music player view. Screen Check it on youtube Usage(XML) Define it in your xml file. <co.mobiwise.library.Intera

Mert Şimşek 744 Dec 25, 2022
Custom wheel widget for android

Wheel widget for Android To include the wheel widget in the current layout, you should add in the layout xml this lines: <it.sephiroth.android

Alessandro Crugnola 384 Nov 21, 2022
IconicDroid is a custom Android Drawable which allows to draw icons from several iconic fonts.

IconicDroid IconicDroid is a custom Android Drawable which allows to draw icons from several iconic fonts. Try out the sample application on the Googl

Artur Termenji 387 Nov 20, 2022
Set custom font in Android application

MagicViews Provides a simple way to set custom font in Android application. Adding to your project Add the library as a dependency to your build.gradl

Ivan Kocijan 225 Nov 20, 2022
A beautiful Android custom View that works similar to a range or seekbar. With animations.

ValueBar A beautiful Android custom View that works similar to a range or seekbar. Selection by gesture. With animations. Supporting API level 11+. De

Philipp Jahoda 147 Nov 20, 2022
An android custom view that displays a circle with a colored arc given a mark

MarkView An android custom view that displays a circle with a colored arc given a mark. Usage Add as a dependency compile 'com.github.xiprox.markv

İhsan Işık 200 Nov 25, 2022
Custom UI control for android which is showing data as a segments and a value inside them.

Segmented Bar View for Android Custom UI control for android which is showing data as a segments and a value inside them. Screenshots Install From rep

GSPD 354 Nov 10, 2022
This is a android custom view , like a scratch card effect!

ScratchView This is a android custom view , like a scratch card effect! Last Update 采纳DearZack童鞋的优化思路,把计算擦除面积比例的操作放在手指离开屏幕时,以降低对CPU的占用。 Articles Scrat

D_clock爱吃葱花 316 Nov 29, 2022
An Android custom view to display digits rendered as dots in a grid, with a style like a 1970s LED clock.

#DotMatrixView This is an Android library project providing a custom view that can display things on a grid of dots. When the displayed value changes,

Mark Roberts 48 Apr 21, 2022
NumberPickerView - Custom Android View to provide a user friendly way of picking numbers. 🧪

?? Custom view for Android which provides a modern design and gestures for picking numbers in a user friendly way.

Mirkamal 6 Feb 16, 2022
Android Custom CountDownView

Android Custom CountDownView

the Sun. 4 Dec 11, 2022
(Deprecated) A custom view component that mimics the new Material Design Bottom Navigation pattern.

BottomBar (Deprecated) I don't have time to maintain this anymore. I basically wrote the whole library in a rush, without tests, while being a serious

Iiro Krankka 8.4k Dec 29, 2022
a custom view that provides dragged and scaled

DragScaleCircleView A custom imageview that provides the circle window can be dragged and scaled, crop image. How does it look? Why? Sometimes need to

null 514 Dec 22, 2022
Custom & highly configurable seek slider with sliding intervals, disabled state and every possible setting to tackle!

LabeledSeekSlider Custom & highly configurable seek slider with sliding intervals, disabled state and every possible setting to tackle! Minimum target

Edgar Žigis 78 Sep 27, 2022
A custom liquidbounce for redesky.com

FDPClient A free mixin-based injection hacked-client for Minecraft using Minecraft Forge based on LiquidBounce. Website: https://fdp.liulihaocai.pw/ L

null 69 Dec 26, 2022
Sliders for compose with custom styles

Custom compose sliders This package allows you to build highly customizable sliders and tracks for compose for android Add to your project Add it in y

Vladislav Krot 52 Dec 15, 2022
🗨️ Beautiful Dialog is a Simple and Beautiful custom dialog

Beautiful Dialog ??️ Beautiful Dialog is a Simple and Beautiful custom dialog. Screenshots Including in your project Gradle Add below codes to your ro

Geovani Amaral 21 Jan 6, 2023