|
1 | 1 | # LoadingToggleButton
|
| 2 | +LoadingToggleButton is a toggle button shows loading animation when toggled, from 'checked' to 'unchecked', or from 'unchecked' to 'checked'. |
2 | 3 |
|
| 4 | +| Design | Screenshot | |
| 5 | +| --------------------------------------- | ------------------------ | |
| 6 | +|  |  | |
3 | 7 |
|
4 |
| -A toggle button shows loading animation when toggled from 'off' to 'on' status. |
| 8 | +# Install |
5 | 9 |
|
6 |
| -## * Work In Pregress |
| 10 | +LoadingToggleButton is available on [JitPack](https://jitpack.io/#intret/LoadingToggleButton). |
7 | 11 |
|
8 |
| - |
| 12 | +[](https://jitpack.io/#intret/LoadingToggleButton) |
| 13 | + |
| 14 | +**Step 1.** Add the JitPack repository to your build file |
| 15 | + |
| 16 | +Add it in your root build.gradle at the end of repositories: |
| 17 | + |
| 18 | +```groovy |
| 19 | + allprojects { |
| 20 | + repositories { |
| 21 | + ... |
| 22 | + maven { url 'https://jitpack.io' } |
| 23 | + } |
| 24 | + } |
| 25 | +``` |
| 26 | + |
| 27 | +**Step 2.** Add the dependency |
| 28 | + |
| 29 | +```groovy |
| 30 | + dependencies { |
| 31 | + compile 'com.github.intret:LoadingToggleButton:master-SNAPSHOT' |
| 32 | + } |
| 33 | +``` |
| 34 | + |
| 35 | +See detailed available versions at https://jitpack.io/#intret/LoadingToggleButton |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +**Add toggle button to .xml layout file:** |
| 40 | + |
| 41 | +```xml |
| 42 | +<?xml version="1.0" encoding="utf-8"?> |
| 43 | +<android.support.constraint.ConstraintLayout |
| 44 | + xmlns:android="http://schemas.android.com/apk/res/android" |
| 45 | + xmlns:app="http://schemas.android.com/apk/res-auto" |
| 46 | + xmlns:tools="http://schemas.android.com/tools" |
| 47 | + android:layout_width="match_parent" |
| 48 | + android:layout_height="match_parent" |
| 49 | + tools:context="cn.intret.lab.loadingtogglebuttondemo.MainActivity"> |
| 50 | + |
| 51 | + |
| 52 | + <cn.intret.lab.library.LoadingToggleButton |
| 53 | + android:layout_width="wrap_content" |
| 54 | + android:layout_height="wrap_content" |
| 55 | + app:layout_constraintLeft_toLeftOf="parent" |
| 56 | + app:layout_constraintRight_toRightOf="parent" |
| 57 | + app:layout_constraintTop_toTopOf="parent" |
| 58 | + android:layout_marginTop="16dp" |
| 59 | + app:ltbToggleChecked="true" |
| 60 | + app:ltbBackgroundCheckedColor="@android:color/black" |
| 61 | + app:ltbBackgroundUncheckedColor="@android:color/darker_gray" |
| 62 | + app:ltbTogglePadding="2dp" |
| 63 | + |
| 64 | + app:ltbIndicatorVisibility="show" |
| 65 | + app:ltbIndicatorAnimationType="lineSpinner" |
| 66 | + app:ltbIndicatorAnimatingWhen="toggleToOn|toggleToOff" |
| 67 | + /> |
| 68 | + |
| 69 | +</android.support.constraint.ConstraintLayout> |
| 70 | +``` |
| 71 | + |
| 72 | +**In your code** |
| 73 | + |
| 74 | +Example in Kotlin language (Java is also easy): |
| 75 | + |
| 76 | +```kotlin |
| 77 | +class MainActivity : AppCompatActivity() { |
| 78 | + |
| 79 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 80 | + super.onCreate(savedInstanceState) |
| 81 | + setContentView(R.layout.activity_main) |
| 82 | + |
| 83 | + val toggle = findViewById<LoadingToggleButton>(R.id.btn_toggle) |
| 84 | + |
| 85 | + // LoadingToggleButton interaction event |
| 86 | + toggle.setOnCheckChangeListener {loadingToggleButton, checked -> |
| 87 | + Log.d("DEMO", "toggle button is checked = " + checked) |
| 88 | + } |
| 89 | + |
| 90 | + toggle.setOnLoadingChangeListener { loadingToggleButton, isLoading -> |
| 91 | + Log.d("DEMO", "toggle button loading animation is showing = " + isLoading) |
| 92 | + } |
| 93 | + |
| 94 | + // show loading indicator animation |
| 95 | + btn_start_loading.setOnClickListener { toggle.start() } |
| 96 | + |
| 97 | + // hide loading indicator animation |
| 98 | + btn_start_loading.setOnClickListener { toggle.stop() } |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
9 | 102 |
|
10 | 103 | ## License
|
11 | 104 |
|
|
0 commit comments