ijkzen/ToggleButton

★ 0Forks 0KotlinGitHub ↗Compare

README

bage week_download month_download build status

Preview

Usage

  1. Add it in your root build.gradle at the end of repositories
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
  1. Add the dependency

bage

dependencies {
    ...
    implementation 'com.github.ijkzen:ToggleButton:<latest_version>'
}

Practice

  1. Example
<com.github.ijkzen.ToggleButton
        android:id="@+id/toggle2"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/toggle"
        android:layout_marginTop="30dp"
        android:layout_width="120dp"
        android:layout_height="70dp"/>
  1. Use attributes on xml

attrs

  1. Set attributes programmatically
    //  set button color

    fun setCheckedBackgroundColor(@ColorRes color: Int) {
        mCheckedBackgroundColor = color
        invalidate()
    }

    fun setUncheckedBackgroundColor(@ColorRes color: Int) {
        mUncheckedBackgroundColor = color
        invalidate()
    }

    fun setCheckedRoundColor(@ColorRes color: Int) {
        mCheckedRoundColor = color
        invalidate()
    }

    fun setUncheckedRoundColor(@ColorRes color: Int) {
        mUncheckedRoundColor = color
        invalidate()
    }

    fun setDisableCheckedBackgroundColor(@ColorRes color: Int) {
        mDisableCheckedBackgroundColor = color
        invalidate()
    }

    fun setDisableUncheckBackgroundColor(@ColorRes color: Int) {
        mDisableUncheckedBackgroundColor = color
        invalidate()
    }

    fun setDisableCheckedRoundColor(@ColorRes color: Int) {
        mDisableCheckedRoundColor = color
        invalidate()
    }

    fun setDisableUncheckedRoundColor(@ColorRes color: Int) {
        mDisableUncheckedRoundColor = color
        invalidate()
    }

//  set button status

    fun setChecked(checked: Boolean) {
        if (checked != mIsChecked) {
            mIsChecked = !mIsChecked
            mIsChanged = true
            mTouchUpTime = System.currentTimeMillis()
            invalidate()
        }
    }

    fun isChecked() = mIsChecked

    fun toggle() {
        if (isButtonEnabled()) {
            setChecked(!mIsChecked)
        }
    }

    fun setButtonEnabled(enabled: Boolean) {
        if (mIsEnabled != enabled) {
            mIsEnabled = enabled
            invalidate()
        }
    }

    fun isButtonEnabled() = mIsEnabled

    fun setDuration(duration: Int) {
        mDuration = if (duration < DEFAULT_DURATION) {
            DEFAULT_DURATION
        } else {
            duration
        }
    }

More

If you have any questions, please ask me here

Contributors

ijkzen

Issues