├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── androidtoggleswitch-sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── llollox │ │ └── androidtoggleswitch_sample │ │ ├── BaseSamplesFragment.java │ │ ├── BasicSamplesFragment.java │ │ ├── CustomSamplesFragment.java │ │ ├── CustomViewSamplesFragment.java │ │ ├── MainActivity.java │ │ ├── MatchParentWidthHeightFragment.java │ │ └── SeparatedSamplesFragment.java │ └── res │ ├── drawable │ ├── ic_add_black_24dp.xml │ ├── ic_arrow_back_black_24dp.xml │ ├── ic_arrow_downward_black_24dp.xml │ ├── ic_arrow_forward_black_24dp.xml │ ├── ic_arrow_upward_black_24dp.xml │ ├── ic_camera_alt_black_24dp.xml │ ├── ic_camera_roll_black_24dp.xml │ ├── ic_delete_black_24dp.xml │ └── ic_edit_black_24dp.xml │ ├── layout │ ├── activity_main.xml │ ├── drawer_list_item.xml │ ├── fragment_basic_samples.xml │ ├── fragment_custom_samples.xml │ ├── fragment_custom_view_samples.xml │ ├── fragment_match_parent_width_height.xml │ ├── fragment_separated_samples.xml │ ├── view_image_left_and_text_toggle_button.xml │ ├── view_image_text_toggle_button.xml │ └── view_images_toggle_button.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── androidtoggleswitch ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── llollox │ │ └── androidtoggleswitch │ │ └── widgets │ │ ├── BaseToggleSwitch.kt │ │ ├── IRightToLeftProvider.kt │ │ ├── MultipleToggleSwitch.kt │ │ ├── ToggleSwitch.kt │ │ ├── ToggleSwitchButton.kt │ │ └── Util.kt │ └── res │ ├── layout │ ├── toggle_switch_button.xml │ ├── toggle_switch_button_view.xml │ └── widget_toggle_switch.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-ldrtl │ └── booleans.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── attrs.xml │ ├── booleans.xml │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── docs ├── basic.gif ├── custom.gif ├── custom_views.gif ├── match_width_height.gif ├── screen.jpg └── separated.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | #built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Windows thumbnail db 19 | Thumbs.db 20 | 21 | # OSX files 22 | .DS_Store 23 | 24 | # Eclipse project files 25 | .classpath 26 | .project 27 | 28 | # Android Studio 29 | *.iml 30 | .idea 31 | #.idea/workspace.xml - remove # and delete .idea if it better suit your needs. 32 | .gradle 33 | build/ 34 | 35 | #NDK 36 | obj/ -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## [2.0.0] 8 | ### Added 9 | - Support for right-to-left languages. 10 | - Ripple effect on click from Material Design. 11 | - Customizable toggle button dimensions 12 | - Customizable border's color and width 13 | - Customizable separator's color and visibility 14 | - Added `android:entries` attribute to set the entries from xml. 15 | - Added `android:layout_width` and `android:layout_height` attributes to support `match_parent` width and height. 16 | - Added `android:enabled` attribute to support disable toggle buttons. 17 | - Added `app:checkedBorderColor` and `app:uncheckedBorderColor` attributes. 18 | - Added `app:border_width` attribute. 19 | - Added `app:elevation` attribute to support button's elevation. 20 | - Added `app:separatorVisible` attribute to set if the separator should be visible or not. 21 | - Added `app:toggleMargin` attribute to support separated toggle switch. 22 | - Added `app:toggleWidth` and `app:toggleHeight` attributes to set the dimensions of the toggle buttons. 23 | 24 | ### Changed 25 | 26 | - `app:activeBgColor` renamed to `app:checkedBackgroundColor`. 27 | - `app:inactiveBgColor` renamed to `app:uncheckedBackgroundColor`. 28 | - `app:activeTextColor` renamed to `app:checkedTextColor`. 29 | - `app:inactiveTextColor` renamed to `app:uncheckedTextColor`. 30 | - `app:cornerRadius` renamed to `app:borderRadius` 31 | 32 | #### Toggle Switch 33 | - Method `getCheckedTogglePosition` renamed to `getCheckedPosition`. 34 | - Method `setCheckedTogglePosition` renamed to `setCheckedPosition`. 35 | - Listener `OnToggleSwitchChangeListener` renamed to `OnChangeListener`. 36 | - Changed listener method `onToggleSwitchChangeListener(int pos, boolean checked)` changed to `onToggleSwitchChanged(int pos)`. 37 | 38 | #### Multiple Toggle Switch 39 | - Method `getCheckedTogglePositions` renamed in `getCheckedPositions` and its return type is `List`. 40 | - Method `setCheckedTogglePosition` renamed in `setCheckedPositions`and its argument is now a `List`. 41 | - Listener `OnToggleSwitchChangeListener` renamed to `OnChangeListener`. 42 | - Renamed listener method `onToggleSwitchChangeListener` to `onMultipleToggleSwitchChanged`. 43 | 44 | ### Removed 45 | - Removed method `setUncheckedTogglePosition` from `MultipleToggleSwitch` 46 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Belka s.r.l. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-Toggle-Switch 2 | 3 | ![Alt text](https://img.shields.io/badge/license-MIT-green.svg?style=flat) 4 | [![API](https://img.shields.io/badge/API-16%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16) 5 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Android%20Toggle%20Switch-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/3235) 6 | 7 | A customizable extension of Android Switches that supports also more than 2 items. 8 | 9 | ## Installation 10 | 11 | #### Gradle 12 | Add Gradle dependency: 13 | 14 | ```groovy 15 | dependencies { 16 | compile 'com.llollox:androidtoggleswitch:2.0.1' 17 | } 18 | ``` 19 | 20 | #### Maven 21 | ```xml 22 | 23 | com.llollox 24 | androidtoggleswitch 25 | 2.0.1 26 | pom 27 | 28 | ``` 29 | 30 | ## Basic Usage 31 | 32 | ![Basic Samples](docs/basic.gif) 33 | 34 | #### 2 Items 35 | 36 | ```xml 37 | 42 | ``` 43 | 44 | #### 3 Items 45 | 46 | ```xml 47 | 53 | ``` 54 | 55 | #### N - Items support 56 | 57 | This can be accomplished in two ways: 58 | * `xml`: In this way you have to define the `android:entries` attributes 59 | as a `string-array`. 60 | * `programmatically`: you have to set the entries as `List`, `CharSequence[]`, etc. 61 | 62 | XML 63 | ```xml 64 | 68 | ``` 69 | 70 | Programmatically 71 | ```java 72 | ToggleSwitch toggleSwitch = (ToggleSwitch) findViewById(R.id.multiple_switches); 73 | ArrayList labels = new ArrayList<>(); 74 | labels.add("AND"); 75 | labels.add("OR"); 76 | labels.add("XOR"); 77 | labels.add("NOT"); 78 | labels.add("OFF"); 79 | toggleSwitch.setLabels(labels); 80 | ``` 81 | 82 | NOTE: Providing the entries using the `android:entries` attribute, 83 | the attributes textToggle[Left/Center/Right] will be ignored. 84 | 85 | #### Multiple checked items support 86 | 87 | Simply use `MultipleToggleSwitch` instead of `ToggleSwitch`. 88 | 89 | ```xml 90 | 97 | ``` 98 | 99 | ## Getters and Setters 100 | 101 | #### Toggle Switch 102 | 103 | * `int getCheckedPosition()` Returns the current checked pos, -1 if none is checked. 104 | 105 | ```java 106 | int pos = toggleSwitch.getCheckedPosition(); 107 | ``` 108 | 109 | * `void setCheckedPosition(int pos)` Checks the pos passed as argument. 110 | 111 | ```java 112 | int pos = 3; 113 | multipleToggleSwitch.setCheckedPosition(pos); 114 | ``` 115 | 116 | #### Multiple Toggle Switch 117 | 118 | * `List getCheckedPositions()` Returns the list of the current checked positions 119 | 120 | ```java 121 | List checkedPositions = multipleToggleSwitch.getCheckedPositions(); 122 | ``` 123 | 124 | * `void setCheckedPositions(List checkedPositions)` Checks the pos passed as argument 125 | 126 | ```java 127 | List checkedPositions = new ArrayList(); 128 | checkedPositions.add(3); 129 | checkedPositions.add(5); 130 | multipleToggleSwitch.setCheckedTogglePositions(checkedPositions); 131 | ``` 132 | 133 | ## Listeners 134 | 135 | #### Toggle Switch 136 | 137 | ```java 138 | toggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener(){ 139 | @Override 140 | public void onToggleSwitchChanged(int position) { 141 | // Your code ... 142 | } 143 | }); 144 | ``` 145 | 146 | #### Multiple Toggle Switch 147 | 148 | ```java 149 | multipleToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener(){ 150 | @Override 151 | public void onMultipleToggleSwitchChanged(int position, boolean checked) { 152 | // Your code ... 153 | } 154 | }); 155 | ``` 156 | 157 | ## Customization 158 | 159 | ![Basic Samples](docs/custom.gif) 160 | 161 | 162 | #### Border 163 | 164 | It is possible to customize: 165 | * the color of the border of both checked and unchecked buttons with `app:checkedBorderColor` and `app:uncheckedBorderColor` respectively 166 | * the border width with the attribute `app:borderWidth` 167 | * the border radius with the attribute `app:borderRadius` 168 | 169 | Since by default the `borderWidth` is 0, it is important to set it at least 170 | to `1dp` in order to show the border. 171 | 172 | ```xml 173 | 181 | ``` 182 | 183 | #### Color 184 | 185 | It is possible to customize: 186 | * the background color of both checked and unchecked buttons with `app:checkedBackgroundColor` and `app:uncheckedBackgroundColor` respectively 187 | * the text color of both checked and unchecked buttons with `app:checkedTextColor` and `app:uncheckedTextColor` respectively 188 | 189 | ```xml 190 | 199 | ``` 200 | 201 | #### Size 202 | 203 | By default each button has a predefined width and height. 204 | It is possible to customize both of them with the attributes `app:toggleWidth` and `app:toggleHeight` respectively. 205 | 206 | NB. In order to let the attributes `app:toggleHeight` and `app:toggleWidth`to work 207 | properly, it is **very** important to set the attributes `android:layout_height` 208 | and `android:layout_width` setted as `wrap_content` respectively. 209 | 210 | ```xml 211 | 219 | ``` 220 | 221 | In fact, setting one of them to `match_parent` the respectively `app:toggle` 222 | will be ignored since the following cases happen: 223 | * `android:layout_width=match_parent` then the parent's width is distributed evenly 224 | between the buttons. 225 | * `android:layout_height=match_parent` then each button takes all the parent view height. 226 | 227 | An example of setting both attributes to `match_parent` is shown below. 228 | 229 | ![Match parent width and height sample](docs/match_width_height.gif) 230 | 231 | #### Programmatically Customization 232 | 233 | All customizations can be done also programmatically. 234 | Once all the properties has been properly set, it's necessary 235 | to call the method `reDraw()` 236 | 237 | ## Separated Buttons 238 | 239 | In order to create separated buttons, just set the `android:toggleMargin` attribute 240 | with a value (in `dp`) greater than 0. 241 | 242 | Obviously when the toggle buttons are separated the separator is hidden. 243 | That's why in this case both the attributes 244 | `app:separatorVisible` and `app:separatorColor` will be ignored. 245 | 246 | An example of separate toggle buttons is shown below. 247 | 248 | ![Separated samples](docs/separated.gif) 249 | 250 | ## Custom View 251 | 252 | In case you want something more complex than a simple text into each button, 253 | you can specify your own view. 254 | 255 | ![Custom view samples](docs/custom_views.gif) 256 | 257 | Let's suppose you want toggle buttons with an icon and a label below. 258 | The layout xml of your view should be something similar to the following one, 259 | called `view_image_text_toggle_button.xml` 260 | 261 | ```xml 262 | 269 | 270 | 275 | 276 | 283 | 284 | 285 | ``` 286 | 287 | In order to assign this layout to each toggle switch button call the method: 288 | 289 | ```java 290 | public void setView(int layoutId, int numEntries, 291 | ToggleSwitchButton.ToggleSwitchButtonDecorator prepareDecorator, 292 | ToggleSwitchButton.ViewDecorator checkedDecorator, 293 | ToggleSwitchButton.ViewDecorator uncheckedDecorator) 294 | ``` 295 | 296 | Here a detailed explanation of the arguments: 297 | * **layoutId**: the resourceId of the layout to be inflated into each toggle button 298 | * **numEntries**: the number of entries (buttons) 299 | * **prepareDecorator**: set a prepare decorator in order to set the customization to be 300 | done for each button, like setting for each one the proper label, icon, etc. 301 | * **checkedDecorator**: set a checked decorator in order to set the customization to be 302 | done for each checked button, like a proper background color, text color etc. 303 | * **uncheckedDecorator**: set an unchecked decorator in order to set the customization to be 304 | done for each unchecked button, like a proper background color, text color etc. 305 | 306 | ```java 307 | toggleSwitch.setView( 308 | R.layout.view_image_text_toggle_button, 309 | 2, 310 | new ToggleSwitchButton.ToggleSwitchButtonDecorator() { 311 | @Override 312 | public void decorate(ToggleSwitchButton toggleSwitchButton, @NotNull View view, int position) { 313 | TextView textView = (TextView) view.findViewById(R.id.text_view); 314 | textView.setText(getCameraGalleryLabel(position)); 315 | 316 | ImageView imageView = (ImageView) view.findViewById(R.id.image_view); 317 | imageView.setImageDrawable(getCameraGalleryDrawable(position)); 318 | } 319 | }, 320 | new ToggleSwitchButton.ViewDecorator() { 321 | @Override 322 | public void decorate(@NotNull View view, int position) { 323 | TextView textView = (TextView) view.findViewById(R.id.text_view); 324 | textView.setTextColor(ContextCompat.getColor(getActivity(), android.R.color.white)); 325 | 326 | ImageView imageView = (ImageView) view.findViewById(R.id.image_view); 327 | imageView.setColorFilter(ContextCompat.getColor(getActivity(), android.R.color.white)); 328 | } 329 | }, 330 | new ToggleSwitchButton.ViewDecorator() { 331 | @Override 332 | public void decorate(@NotNull View view, int position) { 333 | 334 | TextView textView = (TextView) view.findViewById(R.id.text_view); 335 | textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray)); 336 | 337 | ImageView imageView = (ImageView) view.findViewById(R.id.image_view); 338 | imageView.setColorFilter(ContextCompat.getColor(getActivity(), R.color.gray)); 339 | } 340 | }); 341 | 342 | private String getCameraGalleryLabel(int position) { 343 | switch (position) { 344 | case 0: return getString(R.string.camera); 345 | case 1: return getString(R.string.gallery); 346 | default: throw new RuntimeException("Unknown position"); 347 | } 348 | } 349 | 350 | private Drawable getCameraGalleryDrawable(int position) { 351 | switch (position) { 352 | case 0: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_camera_alt_black_24dp); 353 | case 1: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_camera_roll_black_24dp); 354 | default: throw new RuntimeException("Unknown position"); 355 | } 356 | } 357 | ``` 358 | 359 | Both checked and unchecked decorator are optional. 360 | In case you have no customization to be done you can call the simpler method 361 | 362 | ```java 363 | public void setView(int layoutId, int numEntries, 364 | ToggleSwitchButton.ToggleSwitchButtonDecorator prepareDecorator) 365 | ``` 366 | 367 | Since you are using your own view only the following attributes will not work: 368 | * `android:entries` 369 | * `app:checkedTextColor` 370 | * `app:uncheckedTextColor` 371 | 372 | 373 | ## Bonus 374 | 375 | #### Disabled 376 | 377 | You can disable the toggle switch buttons by: 378 | * **xml**: set the attribute `android:enabled=false` 379 | * **programmatically**: ```java toggleSwitch.setEnabled(false)``` 380 | 381 | In this case they become not clickable. 382 | You can see an example at the top of the documentation in basic samples gif example. 383 | 384 | #### Elevation 385 | 386 | In order to specify the elevation of the toggle buttons, 387 | add the following attribute: 388 | 389 | ```xml 390 | app:elevation= 391 | ``` 392 | 393 | You can see an example in the custom view gif example. 394 | 395 | ## Attributes 396 | 397 | Summarizing the full list of all available attributes to customize the 398 | toggle switch buttons is shown below. 399 | 400 | | Option Name | Format | Description | 401 | | ---------------- | -------------- | ----------------------------- | 402 | | android:enabled | `boolean` | Enable or disable the toggle switch buttons | 403 | | android:entries | `array` | Set the labels of each button | 404 | | android:textSize | `dimension` | Text size of each button | 405 | | app:checkedBackgroundColor | `color` | Background color of a checked button | 406 | | app:checkedBorderColor | `color` | Border color of a checked button | 407 | | app:checkedTextColor | `color` | Text color of a checked button | 408 | | app:borderRadius | `dimension` | The border radius of each button in dp | 409 | | app:borderWidth | `dimension` | The width of the border of each button in dp | 410 | | app:uncheckedBackgroundColor | `color` | Background color of the unchecked buttons | 411 | | app:uncheckedBorderColor | `color` | Border color of a unchecked button | 412 | | app:uncheckedTextColor | `color` | Text color of the unchecked buttons | 413 | | app:separatorColor | `color` | Color of the vertical separator between buttons | 414 | | app:separatorVisible | `boolean` | Set if the separator is visible or not | 415 | | app:toggleMargin | `dimension` | Margin between each button in dp | 416 | | app:toggleHeight | `dimension` | Height of each button | 417 | | app:toggleWidth | `dimension` | Width of each button | 418 | 419 | 420 | ## Contributors 421 | Lorenzo Rigato 422 | 423 | ## License 424 | Android-Toggle-Switch is Copyright (c) 2017 Lorenzo Rigato. 425 | It is free software, and may be redistributed under the terms specified in the LICENSE file. 426 | 427 | ## About Author 428 | 429 | [Lorenzo Rigato](http://lorenzorigato.it/) is a dedicated Web, Android & iOS developer with 5+ years of experience. 430 | Clean code lover and beautiful UI fascinated. 431 | You can [see my portfolio](http://lorenzorigato.it/#portfolio) with all my recent projects. 432 | 433 | Interested? [Get in touch](mailto:lore91tanz@gmail.com). 434 | 435 | [www.lorenzorigato.it](http://lorenzorigato.it/) 436 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion '28.0.3' 6 | 7 | dataBinding { 8 | enabled = true 9 | } 10 | 11 | defaultConfig { 12 | applicationId "com.llollox.androidtoggleswitch_sample" 13 | minSdkVersion 16 14 | targetSdkVersion 28 15 | versionCode 1 16 | versionName "1.0" 17 | 18 | testInstrumentationRunner "android.support.prova.runner.AndroidJUnitRunner" 19 | 20 | } 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | } 28 | 29 | dependencies { 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | implementation 'com.android.support:appcompat-v7:28.0.0' 32 | testImplementation 'junit:junit:4.12' 33 | implementation project(':androidtoggleswitch') 34 | 35 | // compile 'com.llollox:androidtoggleswitch:2.0.1' 36 | 37 | } 38 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/fabriziorizzonelli/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/java/com/llollox/androidtoggleswitch_sample/BaseSamplesFragment.java: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch_sample; 2 | 3 | import android.support.v4.app.Fragment; 4 | import android.widget.Toast; 5 | 6 | /** 7 | * Created by lorenzorigato on 24/06/2017. 8 | */ 9 | 10 | public abstract class BaseSamplesFragment extends Fragment { 11 | 12 | protected void showToggleChangeToast(String[] array, int position) { 13 | Toast.makeText(getActivity(), "Checked: " + array[position], Toast.LENGTH_SHORT).show(); 14 | } 15 | 16 | protected void showMultipleToggleChangeToast(String[] array, int position, boolean checked) { 17 | String label = array[position]; 18 | Toast.makeText(getActivity(), 19 | label + "[" + position + "] => " + checked, 20 | Toast.LENGTH_SHORT).show(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/java/com/llollox/androidtoggleswitch_sample/BasicSamplesFragment.java: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch_sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import com.llollox.androidtoggleswitch.widgets.MultipleToggleSwitch; 10 | import com.llollox.androidtoggleswitch.widgets.ToggleSwitch; 11 | import com.llollox.androidtoggleswitch_sample.databinding.FragmentBasicSamplesBinding; 12 | 13 | 14 | /** 15 | * Created by lorenzorigato on 16/06/2017. 16 | */ 17 | 18 | public class BasicSamplesFragment extends BaseSamplesFragment { 19 | 20 | @Nullable 21 | @Override 22 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 23 | FragmentBasicSamplesBinding binding = FragmentBasicSamplesBinding.inflate(inflater); 24 | 25 | binding.twoItemsToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 26 | @Override 27 | public void onToggleSwitchChanged(int position) { 28 | String[] array = {getString(R.string.apple), getString(R.string.lemon)}; 29 | showToggleChangeToast(array, position); 30 | } 31 | }); 32 | 33 | binding.threeItemsToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 34 | @Override 35 | public void onToggleSwitchChanged(int position) { 36 | String[] array = {getString(R.string.apple), getString(R.string.orange), getString(R.string.lemon)}; 37 | showToggleChangeToast(array, position); 38 | } 39 | }); 40 | 41 | binding.nItemsToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 42 | @Override 43 | public void onToggleSwitchChanged(int position) { 44 | String[] planets = getResources().getStringArray(R.array.planets); 45 | showToggleChangeToast(planets, position); 46 | } 47 | }); 48 | 49 | 50 | binding.multipleToggleSwitch.setOnChangeListener(new MultipleToggleSwitch.OnChangeListener() { 51 | @Override 52 | public void onMultipleToggleSwitchChanged(int position, boolean checked) { 53 | String[] planets = getResources().getStringArray(R.array.planets); 54 | showMultipleToggleChangeToast(planets, position, checked); 55 | } 56 | }); 57 | 58 | return binding.getRoot(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/java/com/llollox/androidtoggleswitch_sample/CustomSamplesFragment.java: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch_sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.Toast; 9 | 10 | import com.llollox.androidtoggleswitch.widgets.MultipleToggleSwitch; 11 | import com.llollox.androidtoggleswitch.widgets.ToggleSwitch; 12 | import com.llollox.androidtoggleswitch_sample.databinding.FragmentCustomSamplesBinding; 13 | 14 | 15 | /** 16 | * Created by lorenzorigato on 16/06/2017. 17 | */ 18 | 19 | public class CustomSamplesFragment extends BaseSamplesFragment { 20 | @Nullable 21 | @Override 22 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 23 | FragmentCustomSamplesBinding binding = FragmentCustomSamplesBinding.inflate(inflater); 24 | 25 | binding.matchParentWidthToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 26 | @Override 27 | public void onToggleSwitchChanged(int position) { 28 | String[] operators = getResources().getStringArray(R.array.operators); 29 | showToggleChangeToast(operators, position); 30 | } 31 | }); 32 | 33 | binding.customColorsToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 34 | @Override 35 | public void onToggleSwitchChanged(int position) { 36 | String[] planets = getResources().getStringArray(R.array.planets); 37 | showToggleChangeToast(planets, position); 38 | } 39 | }); 40 | 41 | binding.customSizesToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 42 | @Override 43 | public void onToggleSwitchChanged(int position) { 44 | String[] array = {getString(R.string.apple), getString(R.string.lemon)}; 45 | showToggleChangeToast(array, position); 46 | } 47 | }); 48 | 49 | binding.noSeparatorToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 50 | @Override 51 | public void onToggleSwitchChanged(int position) { 52 | String[] planets = getResources().getStringArray(R.array.planets); 53 | showToggleChangeToast(planets, position); 54 | } 55 | }); 56 | 57 | binding.customBordersToggleSwitch.setOnChangeListener(new MultipleToggleSwitch.OnChangeListener() { 58 | @Override 59 | public void onMultipleToggleSwitchChanged(int position, boolean checked) { 60 | String[] planets = getResources().getStringArray(R.array.planets); 61 | String label = planets[position]; 62 | Toast.makeText(getActivity(), 63 | label + "[" + position + "] => " + checked, 64 | Toast.LENGTH_SHORT).show(); 65 | } 66 | }); 67 | 68 | return binding.getRoot(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/java/com/llollox/androidtoggleswitch_sample/CustomViewSamplesFragment.java: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch_sample; 2 | 3 | import android.graphics.drawable.Drawable; 4 | import android.os.Bundle; 5 | import android.support.v4.content.ContextCompat; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | 12 | import com.llollox.androidtoggleswitch.widgets.ToggleSwitch; 13 | import com.llollox.androidtoggleswitch.widgets.ToggleSwitchButton; 14 | import com.llollox.androidtoggleswitch_sample.databinding.FragmentCustomViewSamplesBinding; 15 | 16 | /** 17 | * Created by rigatol on 23/06/2017. 18 | */ 19 | 20 | public class CustomViewSamplesFragment extends BaseSamplesFragment { 21 | 22 | @Override 23 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 24 | FragmentCustomViewSamplesBinding binding = FragmentCustomViewSamplesBinding.inflate(inflater); 25 | 26 | binding.imageTextToggleSwitch.setView(R.layout.view_image_text_toggle_button, 2, 27 | new ToggleSwitchButton.ToggleSwitchButtonDecorator() { 28 | @Override 29 | public void decorate(ToggleSwitchButton toggleSwitchButton, View view, int position) { 30 | TextView textView = view.findViewById(R.id.text_view); 31 | textView.setText(getCameraGalleryLabel(position)); 32 | 33 | ImageView imageView = view.findViewById(R.id.image_view); 34 | imageView.setImageDrawable(getCameraGalleryDrawable(position)); 35 | } 36 | }, 37 | new ToggleSwitchButton.ViewDecorator() { 38 | @Override 39 | public void decorate(View view, int position) { 40 | TextView textView = view.findViewById(R.id.text_view); 41 | textView.setTextColor(ContextCompat.getColor(getActivity(), android.R.color.white)); 42 | 43 | ImageView imageView = view.findViewById(R.id.image_view); 44 | imageView.setColorFilter(ContextCompat.getColor(getActivity(), android.R.color.white)); 45 | } 46 | }, new ToggleSwitchButton.ViewDecorator() { 47 | @Override 48 | public void decorate(View view, int position) { 49 | 50 | TextView textView = view.findViewById(R.id.text_view); 51 | textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray)); 52 | 53 | ImageView imageView = view.findViewById(R.id.image_view); 54 | imageView.setColorFilter(ContextCompat.getColor(getActivity(), R.color.gray)); 55 | } 56 | }); 57 | 58 | binding.imageTextToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 59 | @Override 60 | public void onToggleSwitchChanged(int position) { 61 | String[] labels = {getCameraGalleryLabel(0), getCameraGalleryLabel(1)}; 62 | showToggleChangeToast(labels, position); 63 | } 64 | }); 65 | 66 | 67 | 68 | binding.arrowImagesToggleSwitch.setView(R.layout.view_images_toggle_button, 4, 69 | new ToggleSwitchButton.ToggleSwitchButtonDecorator() { 70 | @Override 71 | public void decorate(ToggleSwitchButton toggleSwitchButton, View view, int position) { 72 | ImageView imageView = view.findViewById(R.id.image_view); 73 | imageView.setImageDrawable(getArrowDrawable(position)); 74 | } 75 | }, 76 | new ToggleSwitchButton.ViewDecorator() { 77 | @Override 78 | public void decorate(View view, int position) { 79 | ImageView imageView = view.findViewById(R.id.image_view); 80 | imageView.setColorFilter(ContextCompat.getColor(getActivity(), android.R.color.white)); 81 | } 82 | }, new ToggleSwitchButton.ViewDecorator() { 83 | @Override 84 | public void decorate(View view, int position) { 85 | ImageView imageView = (ImageView) view.findViewById(R.id.image_view); 86 | imageView.setColorFilter(ContextCompat.getColor(getActivity(), R.color.gray)); 87 | } 88 | }); 89 | 90 | binding.arrowImagesToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 91 | @Override 92 | public void onToggleSwitchChanged(int position) { 93 | String[] labels = {getString(R.string.left), getString(R.string.up), 94 | getString(R.string.right), getString(R.string.down)}; 95 | showToggleChangeToast(labels, position); 96 | } 97 | }); 98 | 99 | binding.multipleColorsToggleSwitch.setView(R.layout.view_image_left_and_text_toggle_button, 3, 100 | new ToggleSwitchButton.ToggleSwitchButtonDecorator() { 101 | @Override 102 | public void decorate(ToggleSwitchButton toggleSwitchButton, View view, int position) { 103 | ImageView imageView = view.findViewById(R.id.image_view); 104 | imageView.setImageDrawable(getCrudDrawable(position)); 105 | toggleSwitchButton.setCheckedBackgroundColor(getCrudActiveBackgroundColor(position)); 106 | 107 | TextView textView = view.findViewById(R.id.text_view); 108 | textView.setText(getCrudLabel(position)); 109 | } 110 | }, 111 | new ToggleSwitchButton.ViewDecorator() { 112 | @Override 113 | public void decorate(View view, int position) { 114 | ImageView imageView = view.findViewById(R.id.image_view); 115 | imageView.setColorFilter(ContextCompat.getColor(getActivity(), android.R.color.white)); 116 | 117 | TextView textView = view.findViewById(R.id.text_view); 118 | textView.setTextColor(ContextCompat.getColor(getActivity(), android.R.color.white)); 119 | } 120 | }, 121 | new ToggleSwitchButton.ViewDecorator() { 122 | @Override 123 | public void decorate(View view, int position) { 124 | ImageView imageView = view.findViewById(R.id.image_view); 125 | imageView.setColorFilter(ContextCompat.getColor(getActivity(), R.color.gray)); 126 | 127 | TextView textView = view.findViewById(R.id.text_view); 128 | textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray)); 129 | } 130 | } 131 | ); 132 | 133 | binding.multipleColorsToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 134 | @Override 135 | public void onToggleSwitchChanged(int position) { 136 | String[] labels = {getCrudLabel(0), getCrudLabel(1), getCrudLabel(2)}; 137 | showToggleChangeToast(labels, position); 138 | } 139 | }); 140 | 141 | 142 | return binding.getRoot(); 143 | } 144 | 145 | private String getCameraGalleryLabel(int position) { 146 | switch (position) { 147 | case 0: return getString(R.string.camera); 148 | case 1: return getString(R.string.gallery); 149 | default: throw new RuntimeException("Unknown position"); 150 | } 151 | } 152 | 153 | private Drawable getCameraGalleryDrawable(int position) { 154 | switch (position) { 155 | case 0: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_camera_alt_black_24dp); 156 | case 1: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_camera_roll_black_24dp); 157 | default: throw new RuntimeException("Unknown position"); 158 | } 159 | } 160 | 161 | private Drawable getArrowDrawable(int position) { 162 | switch (position) { 163 | case 0: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_arrow_back_black_24dp); 164 | case 1: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_arrow_upward_black_24dp); 165 | case 2: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_arrow_forward_black_24dp); 166 | case 3: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_arrow_downward_black_24dp); 167 | default: throw new RuntimeException("Unknown position"); 168 | } 169 | } 170 | 171 | private Drawable getCrudDrawable(int position) { 172 | switch (position) { 173 | case 0: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_add_black_24dp); 174 | case 1: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_edit_black_24dp); 175 | case 2: return ContextCompat.getDrawable(getActivity(), R.drawable.ic_delete_black_24dp); 176 | default: throw new RuntimeException("Unknown position"); 177 | } 178 | } 179 | 180 | private String getCrudLabel(int position) { 181 | switch (position) { 182 | case 0: return getString(R.string.add); 183 | case 1: return getString(R.string.edit); 184 | case 2: return getString(R.string.delete); 185 | default: throw new RuntimeException("Unknown position"); 186 | } 187 | } 188 | 189 | private int getCrudActiveBackgroundColor(int position) { 190 | switch (position) { 191 | case 0: return ContextCompat.getColor(getActivity(), R.color.green); 192 | case 1: return ContextCompat.getColor(getActivity(), R.color.blue); 193 | case 2: return ContextCompat.getColor(getActivity(), android.R.color.holo_red_dark); 194 | default: throw new RuntimeException("Unknown position"); 195 | } 196 | } 197 | 198 | 199 | } 200 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/java/com/llollox/androidtoggleswitch_sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch_sample; 2 | 3 | import android.content.res.Configuration; 4 | import android.os.Bundle; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.app.FragmentManager; 7 | import android.support.v4.widget.DrawerLayout; 8 | import android.support.v7.app.ActionBarDrawerToggle; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.view.MenuItem; 11 | import android.view.View; 12 | import android.widget.AdapterView; 13 | import android.widget.ArrayAdapter; 14 | import android.widget.ListView; 15 | 16 | public class MainActivity extends AppCompatActivity { 17 | 18 | 19 | private String[] mMenuTitles; 20 | private DrawerLayout mDrawerLayout; 21 | private ActionBarDrawerToggle mDrawerToggle; 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_main); 27 | 28 | mMenuTitles = getResources().getStringArray(R.array.menu_titles); 29 | mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 30 | 31 | mDrawerToggle = new ActionBarDrawerToggle( 32 | this, /* host Activity */ 33 | mDrawerLayout, /* DrawerLayout object */ 34 | R.string.open_drawer, /* "open drawer" description */ 35 | R.string.close_drawer /* "close drawer" description */ 36 | ) { 37 | 38 | /** Called when a drawer has settled in a completely closed state. */ 39 | public void onDrawerClosed(View view) { 40 | super.onDrawerClosed(view); 41 | invalidateOptionsMenu(); 42 | } 43 | 44 | /** Called when a drawer has settled in a completely open state. */ 45 | public void onDrawerOpened(View drawerView) { 46 | super.onDrawerOpened(drawerView); 47 | invalidateOptionsMenu(); 48 | } 49 | }; 50 | 51 | mDrawerLayout.addDrawerListener(mDrawerToggle); 52 | 53 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 54 | getSupportActionBar().setHomeButtonEnabled(true); 55 | 56 | ListView mDrawerList = (ListView) findViewById(R.id.left_drawer); 57 | 58 | // Set the adapter for the list view 59 | mDrawerList.setAdapter(new ArrayAdapter(this, 60 | R.layout.drawer_list_item, mMenuTitles)); 61 | 62 | // Set the list's click listener 63 | mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 64 | @Override 65 | public void onItemClick(AdapterView parent, View view, int position, long id) { 66 | setTitle(mMenuTitles[position]); 67 | showFragment(getFragmentByPosition(position)); 68 | mDrawerLayout.closeDrawers(); 69 | } 70 | }); 71 | 72 | setTitle(mMenuTitles[0]); 73 | showFragment(getFragmentByPosition(0)); 74 | } 75 | 76 | @Override 77 | protected void onPostCreate(Bundle savedInstanceState) { 78 | super.onPostCreate(savedInstanceState); 79 | // Sync the toggle state after onRestoreInstanceState has occurred. 80 | mDrawerToggle.syncState(); 81 | } 82 | 83 | @Override 84 | public void onConfigurationChanged(Configuration newConfig) { 85 | super.onConfigurationChanged(newConfig); 86 | mDrawerToggle.onConfigurationChanged(newConfig); 87 | } 88 | 89 | @Override 90 | public boolean onOptionsItemSelected(MenuItem item) { 91 | // Pass the event to ActionBarDrawerToggle, if it returns 92 | // true, then it has handled the app icon touch event 93 | if (mDrawerToggle.onOptionsItemSelected(item)) { 94 | return true; 95 | } 96 | // Handle your other action bar items... 97 | 98 | return super.onOptionsItemSelected(item); 99 | } 100 | 101 | private Fragment getFragmentByPosition(int position) { 102 | switch (position) { 103 | case 0: return new BasicSamplesFragment(); 104 | case 1: return new CustomSamplesFragment(); 105 | case 2: return new MatchParentWidthHeightFragment(); 106 | case 3: return new SeparatedSamplesFragment(); 107 | case 4: return new CustomViewSamplesFragment(); 108 | default: throw new RuntimeException("Unsupported pos"); 109 | } 110 | } 111 | 112 | private void showFragment(Fragment fragment) { 113 | // Insert the fragment by replacing any existing fragment 114 | FragmentManager fragmentManager = getSupportFragmentManager(); 115 | fragmentManager.beginTransaction() 116 | .replace(R.id.content_frame, fragment) 117 | .commit(); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/java/com/llollox/androidtoggleswitch_sample/MatchParentWidthHeightFragment.java: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch_sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import com.llollox.androidtoggleswitch.widgets.ToggleSwitch; 10 | import com.llollox.androidtoggleswitch_sample.databinding.FragmentMatchParentWidthHeightBinding; 11 | 12 | /** 13 | * Created by lorenzorigato on 20/06/2017. 14 | */ 15 | 16 | public class MatchParentWidthHeightFragment extends BaseSamplesFragment { 17 | 18 | @Nullable 19 | @Override 20 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 21 | FragmentMatchParentWidthHeightBinding binding = FragmentMatchParentWidthHeightBinding.inflate(inflater); 22 | 23 | binding.matchParentWidthToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 24 | @Override 25 | public void onToggleSwitchChanged(int position) { 26 | String[] planets = getResources().getStringArray(R.array.operators); 27 | showToggleChangeToast(planets, position); 28 | } 29 | }); 30 | 31 | return binding.getRoot(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/java/com/llollox/androidtoggleswitch_sample/SeparatedSamplesFragment.java: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch_sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import com.llollox.androidtoggleswitch.widgets.ToggleSwitch; 10 | import com.llollox.androidtoggleswitch_sample.databinding.FragmentSeparatedSamplesBinding; 11 | 12 | 13 | /** 14 | * Created by lorenzorigato on 22/06/2017. 15 | */ 16 | 17 | public class SeparatedSamplesFragment extends BaseSamplesFragment { 18 | @Nullable 19 | @Override 20 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 21 | FragmentSeparatedSamplesBinding binding = FragmentSeparatedSamplesBinding.inflate(inflater); 22 | 23 | binding.operatorsSeparatedToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 24 | @Override 25 | public void onToggleSwitchChanged(int position) { 26 | String[] operators = getResources().getStringArray(R.array.operators); 27 | showToggleChangeToast(operators, position); 28 | } 29 | }); 30 | 31 | binding.planetsSeparatedCircleToggleSwitch.setOnChangeListener(new ToggleSwitch.OnChangeListener() { 32 | @Override 33 | public void onToggleSwitchChanged(int position) { 34 | String[] operators = getResources().getStringArray(R.array.planets); 35 | showToggleChangeToast(operators, position); 36 | } 37 | }); 38 | 39 | return binding.getRoot(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_add_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_arrow_back_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_arrow_downward_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_arrow_forward_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_arrow_upward_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_camera_alt_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_camera_roll_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_delete_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/drawable/ic_edit_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 13 | 14 | 15 | 23 | 24 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/drawer_list_item.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/fragment_basic_samples.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 18 | 19 | 22 | 23 | 29 | 30 | 33 | 34 | 41 | 42 | 45 | 46 | 51 | 52 | 55 | 56 | 61 | 62 | 65 | 66 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/fragment_custom_samples.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 18 | 19 | 22 | 23 | 28 | 29 | 32 | 33 | 43 | 44 | 47 | 48 | 57 | 58 | 61 | 62 | 71 | 72 | 75 | 76 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/fragment_custom_view_samples.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 12 | 13 | 16 | 17 | 24 | 25 | 28 | 29 | 37 | 38 | 41 | 42 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/fragment_match_parent_width_height.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 17 | 18 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/fragment_separated_samples.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 14 | 15 | 18 | 19 | 25 | 26 | 27 | 30 | 31 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/view_image_left_and_text_toggle_button.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 16 | 17 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/view_image_text_toggle_button.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 23 | 24 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/layout/view_images_toggle_button.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #009688 8 | #4CAF50 9 | #FF9800 10 | 11 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 80dp 6 | 7 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Open Drawer 4 | Close Drawer 5 | 6 | Toggle Switch Sample 7 | 8 | 9 | 10 | 2 Items 11 | 3 Items 12 | N Items 13 | Multi Select 14 | Disabled 15 | 16 | 17 | Match parent width 18 | Custom colors 19 | Custom sizes 20 | Custom borders 21 | No separator 22 | 23 | 24 | Match parent width and height 25 | 26 | 27 | Basic separated 28 | Circle separated 29 | 30 | 31 | Image and text 32 | Only images 33 | Multiple colors 34 | 35 | 36 | Apple 37 | Lemon 38 | Orange 39 | 40 | Camera 41 | Gallery 42 | 43 | Left 44 | Up 45 | Right 46 | Down 47 | 48 | Add 49 | Edit 50 | Delete 51 | 52 | 53 | Earth 54 | Jupiter 55 | Mars 56 | Venus 57 | 58 | 59 | 60 | AND 61 | OR 62 | NOT 63 | 64 | 65 | 66 | Basic Samples 67 | Custom Samples 68 | Match Parent Width and Height Sample 69 | Separated Samples 70 | Custom View Samples 71 | 72 | 73 | -------------------------------------------------------------------------------- /androidtoggleswitch-sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /androidtoggleswitch/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /androidtoggleswitch/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | 4 | ext { 5 | PUBLISH_GROUP_ID = 'com.llollox' 6 | PUBLISH_ARTIFACT_ID = 'androidtoggleswitch' 7 | PUBLISH_VERSION = '2.0.1' 8 | } 9 | 10 | android { 11 | compileSdkVersion 28 12 | buildToolsVersion "28.0.3" 13 | 14 | defaultConfig { 15 | minSdkVersion 16 16 | targetSdkVersion 28 17 | versionCode 1 18 | versionName "1.0" 19 | } 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | } 27 | 28 | dependencies { 29 | implementation fileTree(dir: 'libs', include: ['*.jar']) 30 | implementation 'com.android.support:appcompat-v7:28.0.0' 31 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 32 | } 33 | 34 | apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle' 35 | repositories { 36 | mavenCentral() 37 | } -------------------------------------------------------------------------------- /androidtoggleswitch/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/lorenzorigato/Development/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/java/com/llollox/androidtoggleswitch/widgets/BaseToggleSwitch.kt: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch.widgets 2 | 3 | import android.content.Context 4 | import android.os.Build 5 | import android.support.annotation.RequiresApi 6 | import android.support.v4.content.ContextCompat 7 | import android.support.v4.view.ViewCompat 8 | import android.text.TextUtils 9 | import android.util.AttributeSet 10 | import android.util.TypedValue 11 | import android.view.View 12 | import android.widget.LinearLayout 13 | import android.widget.TextView 14 | import com.llollox.androidtoggleswitch.R 15 | import java.util.* 16 | 17 | abstract class BaseToggleSwitch : LinearLayout, ToggleSwitchButton.Listener { 18 | 19 | 20 | /* 21 | Default Values 22 | */ 23 | 24 | companion object Default { 25 | 26 | @JvmStatic private val BORDER_RADIUS_DP = 4 27 | @JvmStatic private val BORDER_WIDTH = 0 28 | 29 | @JvmStatic private val CHECKED_BACKGROUND_COLOR = R.color.blue 30 | @JvmStatic private val CHECKED_BORDER_COLOR = R.color.blue 31 | @JvmStatic private val CHECKED_TEXT_COLOR = android.R.color.white 32 | 33 | @JvmStatic private val EMPTY_TOGGLE_DECORATOR = object: ToggleSwitchButton.ToggleSwitchButtonDecorator { 34 | override fun decorate(toggleSwitchButton: ToggleSwitchButton, view: View, position: Int) {} 35 | } 36 | 37 | @JvmStatic private val ENABLED = true 38 | 39 | @JvmStatic private val LAYOUT_ID = R.layout.toggle_switch_button_view 40 | @JvmStatic private val LAYOUT_HEIGHT = LinearLayout.LayoutParams.WRAP_CONTENT 41 | @JvmStatic private val LAYOUT_WIDTH = LinearLayout.LayoutParams.WRAP_CONTENT 42 | 43 | @JvmStatic private val NUM_ENTRIES = 0 44 | 45 | @JvmStatic private val SEPARATOR_COLOR = R.color.gray_very_light 46 | @JvmStatic private val SEPARATOR_VISIBLE = true 47 | 48 | @JvmStatic private val TEXT_SIZE = 16f 49 | 50 | @JvmStatic private val TOGGLE_DISTANCE = 0f 51 | @JvmStatic private val TOGGLE_ELEVATION = 0f 52 | @JvmStatic private val TOGGLE_HEIGHT = 38f 53 | @JvmStatic private val TOGGLE_WIDTH = 72f 54 | 55 | @JvmStatic private val UNCHECKED_BACKGROUND_COLOR = R.color.gray_light 56 | @JvmStatic private val UNCHECKED_BORDER_COLOR = R.color.gray_light 57 | @JvmStatic private val UNCHECKED_TEXT_COLOR = R.color.gray 58 | } 59 | 60 | 61 | 62 | 63 | /* 64 | Instance Variables 65 | */ 66 | 67 | 68 | var checkedBackgroundColor: Int 69 | var checkedBorderColor: Int 70 | var checkedTextColor: Int 71 | 72 | var borderRadius: Float 73 | var borderWidth: Float 74 | 75 | var uncheckedBackgroundColor: Int 76 | var uncheckedBorderColor: Int 77 | var uncheckedTextColor: Int 78 | 79 | var separatorColor: Int 80 | var separatorVisible = SEPARATOR_VISIBLE 81 | 82 | var textSize: Float 83 | 84 | var toggleElevation = TOGGLE_ELEVATION 85 | var toggleMargin: Float 86 | var toggleHeight: Float 87 | var toggleWidth: Float 88 | 89 | var layoutHeight = LAYOUT_HEIGHT 90 | var layoutWidth = LAYOUT_WIDTH 91 | 92 | var layoutId = LAYOUT_ID 93 | var numEntries = NUM_ENTRIES 94 | 95 | var prepareDecorator: ToggleSwitchButton.ToggleSwitchButtonDecorator = EMPTY_TOGGLE_DECORATOR 96 | var checkedDecorator: ToggleSwitchButton.ViewDecorator? = null 97 | var uncheckedDecorator: ToggleSwitchButton.ViewDecorator? = null 98 | 99 | var buttons = ArrayList() 100 | 101 | /* 102 | Constructors 103 | */ 104 | 105 | constructor(context: Context) : super(context) { 106 | 107 | // Setup View 108 | setUpView() 109 | 110 | // Set default params 111 | checkedBackgroundColor = ContextCompat.getColor(context, CHECKED_BACKGROUND_COLOR) 112 | checkedBorderColor = ContextCompat.getColor(context, CHECKED_BORDER_COLOR) 113 | checkedTextColor = ContextCompat.getColor(context, CHECKED_TEXT_COLOR) 114 | 115 | borderRadius = dp2px(context, BORDER_RADIUS_DP.toFloat()) 116 | borderWidth = dp2px(context, BORDER_WIDTH.toFloat()) 117 | 118 | uncheckedBackgroundColor = ContextCompat.getColor(context, UNCHECKED_BACKGROUND_COLOR) 119 | uncheckedBorderColor = ContextCompat.getColor(context, UNCHECKED_BORDER_COLOR) 120 | uncheckedTextColor = ContextCompat.getColor(context, UNCHECKED_TEXT_COLOR) 121 | 122 | separatorColor = ContextCompat.getColor(context, SEPARATOR_COLOR) 123 | 124 | textSize = dp2px(context, TEXT_SIZE) 125 | 126 | toggleMargin = dp2px(getContext(), TOGGLE_DISTANCE) 127 | toggleHeight = dp2px(getContext(), TOGGLE_HEIGHT) 128 | toggleWidth = dp2px(getContext(), TOGGLE_WIDTH) 129 | } 130 | 131 | constructor(context: Context, attrs: AttributeSet?) : super (context, attrs) { 132 | 133 | if (attrs != null) { 134 | setUpView() 135 | 136 | val attributes = context.obtainStyledAttributes(attrs, R.styleable.BaseToggleSwitch, 0, 0) 137 | 138 | try { 139 | 140 | checkedBackgroundColor = attributes.getColor( 141 | R.styleable.BaseToggleSwitch_checkedBackgroundColor, 142 | ContextCompat.getColor(context, CHECKED_BACKGROUND_COLOR)) 143 | 144 | checkedBorderColor = attributes.getColor( 145 | R.styleable.BaseToggleSwitch_checkedBorderColor, 146 | ContextCompat.getColor(context, CHECKED_BORDER_COLOR)) 147 | 148 | checkedTextColor = attributes.getColor( 149 | R.styleable.BaseToggleSwitch_checkedTextColor, 150 | ContextCompat.getColor(context, CHECKED_TEXT_COLOR)) 151 | 152 | borderRadius = attributes.getDimensionPixelSize( 153 | R.styleable.BaseToggleSwitch_borderRadius, 154 | dp2px(context, BORDER_RADIUS_DP.toFloat()).toInt()).toFloat() 155 | 156 | borderWidth = attributes.getDimensionPixelSize( 157 | R.styleable.BaseToggleSwitch_borderWidth, 158 | dp2px(context, BORDER_WIDTH.toFloat()).toInt()).toFloat() 159 | 160 | isEnabled = attributes.getBoolean( 161 | R.styleable.BaseToggleSwitch_android_enabled, 162 | ENABLED) 163 | 164 | uncheckedBackgroundColor = attributes.getColor( 165 | R.styleable.BaseToggleSwitch_uncheckedBackgroundColor, 166 | ContextCompat.getColor(context, UNCHECKED_BACKGROUND_COLOR)) 167 | 168 | uncheckedBorderColor = attributes.getColor( 169 | R.styleable.BaseToggleSwitch_uncheckedBorderColor, 170 | ContextCompat.getColor(context, UNCHECKED_BORDER_COLOR)) 171 | 172 | uncheckedTextColor = attributes.getColor( 173 | R.styleable.BaseToggleSwitch_uncheckedTextColor, 174 | ContextCompat.getColor(context, UNCHECKED_TEXT_COLOR)) 175 | 176 | separatorColor = attributes.getColor( 177 | R.styleable.BaseToggleSwitch_separatorColor, 178 | ContextCompat.getColor(context, SEPARATOR_COLOR)) 179 | 180 | separatorVisible = attributes.getBoolean( 181 | R.styleable.BaseToggleSwitch_separatorVisible, 182 | SEPARATOR_VISIBLE) 183 | 184 | textSize = attributes.getDimensionPixelSize( 185 | R.styleable.BaseToggleSwitch_android_textSize, 186 | dp2px(context, TEXT_SIZE).toInt()).toFloat() 187 | 188 | toggleElevation = attributes.getDimensionPixelSize( 189 | R.styleable.BaseToggleSwitch_elevation, 190 | dp2px(context, TOGGLE_ELEVATION).toInt()).toFloat() 191 | 192 | toggleMargin = attributes.getDimension( 193 | R.styleable.BaseToggleSwitch_toggleMargin, 194 | dp2px(getContext(), TOGGLE_DISTANCE)) 195 | 196 | toggleHeight = attributes.getDimension( 197 | R.styleable.BaseToggleSwitch_toggleHeight, 198 | dp2px(getContext(), TOGGLE_HEIGHT)) 199 | 200 | toggleWidth = attributes.getDimension( 201 | R.styleable.BaseToggleSwitch_toggleWidth, 202 | dp2px(getContext(), TOGGLE_WIDTH)) 203 | 204 | layoutHeight = attributes.getLayoutDimension( 205 | R.styleable.BaseToggleSwitch_android_layout_height, 206 | LAYOUT_HEIGHT) 207 | 208 | layoutWidth = attributes.getLayoutDimension( 209 | R.styleable.BaseToggleSwitch_android_layout_width, 210 | LAYOUT_WIDTH) 211 | 212 | val entries = attributes.getTextArray(R.styleable.BaseToggleSwitch_android_entries) 213 | if (entries == null || entries.isEmpty()) { 214 | 215 | val entriesList = ArrayList() 216 | 217 | val textToggleLeft = attributes.getString(R.styleable.BaseToggleSwitch_textToggleLeft) 218 | val textToggleRight = attributes.getString(R.styleable.BaseToggleSwitch_textToggleRight) 219 | 220 | if (!TextUtils.isEmpty(textToggleLeft) && !TextUtils.isEmpty(textToggleRight)) { 221 | entriesList.add(textToggleLeft) 222 | val textToggleCenter = attributes.getString(R.styleable.BaseToggleSwitch_textToggleCenter) 223 | if (!TextUtils.isEmpty(textToggleCenter)) { 224 | entriesList.add(textToggleCenter) 225 | } 226 | entriesList.add(textToggleRight) 227 | setEntries(entriesList) 228 | } 229 | } 230 | else { 231 | setEntries(entries) 232 | } 233 | } 234 | finally { 235 | attributes.recycle() 236 | } 237 | } 238 | else { 239 | throw RuntimeException("AttributeSet is null!") 240 | } 241 | } 242 | 243 | 244 | /* 245 | Overrode instance methods 246 | */ 247 | 248 | override fun onAttachedToWindow() { 249 | super.onAttachedToWindow() 250 | 251 | for (button in buttons) { 252 | if (!isFullWidth()) { 253 | button.layoutParams.width = toggleWidth.toInt() 254 | } 255 | 256 | if (!isFullHeight()) { 257 | button.layoutParams.height = toggleHeight.toInt() 258 | } 259 | } 260 | } 261 | 262 | final override fun setEnabled(enabled: Boolean) { 263 | super.setEnabled(enabled) 264 | alpha = if (enabled) 1f else .6f 265 | } 266 | 267 | @RequiresApi(Build.VERSION_CODES.LOLLIPOP) 268 | final override fun setElevation(elevation: Float) { 269 | super.setElevation(elevation) 270 | if (elevation > 0) { 271 | clipToPadding = false 272 | setPadding(elevation.toInt(), elevation.toInt(), elevation.toInt(), elevation.toInt()) 273 | } 274 | else { 275 | clipToPadding = true 276 | setPadding(0, 0, 0, 0) 277 | } 278 | for (button in buttons) { 279 | ViewCompat.setElevation(button, elevation) 280 | } 281 | } 282 | 283 | 284 | /* 285 | Public instance methods 286 | */ 287 | 288 | fun setEntries(stringArrayId: Int) { 289 | setEntries(resources.getStringArray(stringArrayId)) 290 | } 291 | 292 | fun setEntries(entries: Array) { 293 | setEntries(entries.toList()) 294 | } 295 | 296 | fun setEntries(entries : Array) { 297 | val entriesList = ArrayList() 298 | for (entry in entries) { 299 | entriesList.add(entry.toString()) 300 | } 301 | setEntries(entriesList) 302 | } 303 | 304 | fun setEntries(entries : List) { 305 | 306 | val prepareDecorator = object: ToggleSwitchButton.ToggleSwitchButtonDecorator { 307 | override fun decorate(toggleSwitchButton: ToggleSwitchButton, view: View, position: Int) { 308 | val textView = view.findViewById(R.id.text_view) as TextView 309 | textView.text = entries[position] 310 | textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize) 311 | } 312 | } 313 | 314 | val checkedDecorator = object: ToggleSwitchButton.ViewDecorator { 315 | override fun decorate(view: View, position: Int) { 316 | val textView = view.findViewById(R.id.text_view) as TextView 317 | textView.setTextColor(checkedTextColor) 318 | } 319 | } 320 | 321 | val uncheckedDecorator = object: ToggleSwitchButton.ViewDecorator { 322 | override fun decorate(view: View, position: Int) { 323 | val textView = view.findViewById(R.id.text_view) as TextView 324 | textView.setTextColor(uncheckedTextColor) 325 | } 326 | } 327 | 328 | setView(R.layout.toggle_switch_button_view, entries.size, 329 | prepareDecorator, checkedDecorator, uncheckedDecorator) 330 | } 331 | 332 | fun setView(layoutId: Int, numEntries: Int, 333 | prepareDecorator: ToggleSwitchButton.ToggleSwitchButtonDecorator) { 334 | 335 | setView(layoutId, numEntries, prepareDecorator, null, null) 336 | } 337 | 338 | fun setView(layoutId: Int, numEntries: Int, 339 | prepareDecorator: ToggleSwitchButton.ToggleSwitchButtonDecorator, 340 | checkedDecorator: ToggleSwitchButton.ViewDecorator?, 341 | uncheckedDecorator: ToggleSwitchButton.ViewDecorator?) { 342 | removeAllViews() 343 | buttons.clear() 344 | 345 | this.layoutId = layoutId 346 | this.numEntries = numEntries 347 | this.checkedDecorator = checkedDecorator 348 | this.uncheckedDecorator = uncheckedDecorator 349 | 350 | for (index in 0..numEntries - 1) { 351 | val positionType = getPosition(index, numEntries) 352 | val button = ToggleSwitchButton(context, index, positionType, this, 353 | layoutId, prepareDecorator, checkedDecorator, uncheckedDecorator, 354 | checkedBackgroundColor, checkedBorderColor, 355 | borderRadius, borderWidth, uncheckedBackgroundColor, 356 | uncheckedBorderColor, separatorColor, toggleMargin.toInt()) 357 | buttons.add(button) 358 | addView(button) 359 | } 360 | 361 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 362 | elevation = toggleElevation 363 | } 364 | 365 | manageSeparatorVisiblity() 366 | } 367 | 368 | fun reDraw() { 369 | setView(layoutId, numEntries, prepareDecorator, checkedDecorator, uncheckedDecorator) 370 | onRedrawn() 371 | } 372 | 373 | 374 | /* 375 | Protected instance methods 376 | */ 377 | 378 | protected abstract fun onRedrawn() 379 | 380 | protected fun manageSeparatorVisiblity() { 381 | for ((index, button) in buttons.withIndex()) { 382 | if (separatorVisible && index < buttons.size - 1 && !hasBorder() && !areToggleSeparated()) { 383 | button.setSeparatorVisibility(button.isChecked == buttons[index + 1].isChecked) 384 | } 385 | else { 386 | button.setSeparatorVisibility(false) 387 | } 388 | } 389 | } 390 | 391 | /* 392 | Private instance methods 393 | */ 394 | 395 | private fun areToggleSeparated() : Boolean { 396 | return toggleMargin > 0 397 | } 398 | 399 | private fun getPosition(index : Int, size: Int) : ToggleSwitchButton.PositionType { 400 | if (index == 0) { 401 | return ToggleSwitchButton.PositionType.LEFT 402 | } 403 | else if (index == size - 1) { 404 | return ToggleSwitchButton.PositionType.RIGHT 405 | } 406 | else { 407 | return ToggleSwitchButton.PositionType.CENTER 408 | } 409 | } 410 | 411 | private fun hasBorder() : Boolean { 412 | return borderWidth > 0f 413 | } 414 | 415 | private fun isFullHeight() : Boolean { 416 | return layoutHeight == LinearLayout.LayoutParams.MATCH_PARENT 417 | } 418 | 419 | private fun isFullWidth() : Boolean { 420 | return layoutWidth == LinearLayout.LayoutParams.MATCH_PARENT 421 | } 422 | 423 | private fun setUpView() { 424 | layoutParams = LinearLayout.LayoutParams( 425 | LinearLayout.LayoutParams.WRAP_CONTENT, 426 | LinearLayout.LayoutParams.MATCH_PARENT) 427 | orientation = HORIZONTAL 428 | } 429 | 430 | } -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/java/com/llollox/androidtoggleswitch/widgets/IRightToLeftProvider.kt: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch.widgets 2 | 3 | /** 4 | * Created by lorenzorigato on 24/06/2017. 5 | */ 6 | 7 | interface IRightToLeftProvider { 8 | fun isRightToLeft(): Boolean 9 | } -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/java/com/llollox/androidtoggleswitch/widgets/MultipleToggleSwitch.kt: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch.widgets 2 | 3 | import android.content.Context 4 | import android.util.AttributeSet 5 | import java.util.* 6 | 7 | class MultipleToggleSwitch(context: Context, attrs: AttributeSet?) : BaseToggleSwitch(context, attrs) { 8 | 9 | interface OnChangeListener { 10 | fun onMultipleToggleSwitchChanged(position: Int, checked: Boolean) 11 | } 12 | 13 | var onChangeListener: OnChangeListener? = null 14 | private var checkedPositions: ArrayList = ArrayList() 15 | 16 | fun getCheckedPositions() : List { 17 | return checkedPositions 18 | } 19 | 20 | fun setCheckedPositions(checkedPositions: List) { 21 | this.checkedPositions.clear() 22 | this.checkedPositions.addAll(checkedPositions) 23 | for ((index, toggleSwitchButton) in buttons.withIndex()) { 24 | if (checkedPositions.contains(index)) { 25 | toggleSwitchButton.check() 26 | } 27 | else { 28 | toggleSwitchButton.uncheck() 29 | } 30 | } 31 | manageSeparatorVisiblity() 32 | } 33 | 34 | override fun onToggleSwitchClicked(button: ToggleSwitchButton) { 35 | if (isEnabled) { 36 | val position = buttons.indexOf(button) 37 | if (button.isChecked) { 38 | button.uncheck() 39 | checkedPositions.remove(position) 40 | } 41 | else { 42 | button.check() 43 | checkedPositions.add(position) 44 | } 45 | 46 | Collections.sort(checkedPositions) 47 | 48 | manageSeparatorVisiblity() 49 | onChangeListener?.onMultipleToggleSwitchChanged(position, button.isChecked) 50 | } 51 | } 52 | 53 | override fun onRedrawn() { 54 | setCheckedPositions(ArrayList(checkedPositions)) 55 | manageSeparatorVisiblity() 56 | } 57 | } -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/java/com/llollox/androidtoggleswitch/widgets/ToggleSwitch.kt: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch.widgets 2 | 3 | import android.content.Context 4 | import android.util.AttributeSet 5 | 6 | class ToggleSwitch(context: Context, attrs: AttributeSet?) : BaseToggleSwitch(context, attrs) { 7 | 8 | interface OnChangeListener { 9 | fun onToggleSwitchChanged(position: Int) 10 | } 11 | 12 | var checkedPosition: Int? = null 13 | var onChangeListener : OnChangeListener? = null 14 | 15 | 16 | override fun onRedrawn() { 17 | if (checkedPosition != null) { 18 | val currentToggleSwitch = buttons[checkedPosition!!] 19 | currentToggleSwitch.check() 20 | currentToggleSwitch.isClickable = false 21 | } 22 | manageSeparatorVisiblity() 23 | } 24 | 25 | override fun onToggleSwitchClicked(button: ToggleSwitchButton) { 26 | 27 | if (!button.isChecked && isEnabled) { 28 | if (checkedPosition != null) { 29 | buttons[checkedPosition!!].uncheck() 30 | } 31 | 32 | checkedPosition = buttons.indexOf(button) 33 | 34 | button.check() 35 | 36 | manageSeparatorVisiblity() 37 | 38 | onChangeListener?.onToggleSwitchChanged(checkedPosition!!) 39 | } 40 | } 41 | 42 | fun getCheckedPosition() : Int { 43 | return checkedPosition ?: -1 44 | } 45 | 46 | fun setCheckedPosition(checkedPosition: Int) { 47 | this.checkedPosition = checkedPosition 48 | for ((index, toggleSwitchButton) in buttons.withIndex()) { 49 | if (checkedPosition == index) { 50 | toggleSwitchButton.check() 51 | } 52 | else { 53 | toggleSwitchButton.uncheck() 54 | } 55 | } 56 | manageSeparatorVisiblity() 57 | } 58 | } -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/java/com/llollox/androidtoggleswitch/widgets/ToggleSwitchButton.kt: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch.widgets 2 | 3 | import android.content.Context 4 | import android.graphics.drawable.GradientDrawable 5 | import android.view.LayoutInflater 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import android.widget.LinearLayout 9 | import android.widget.RelativeLayout 10 | import com.llollox.androidtoggleswitch.R 11 | 12 | 13 | class ToggleSwitchButton (context: Context, var position: Int, var positionType: PositionType, 14 | listener: Listener, layoutId: Int, var prepareDecorator: ToggleSwitchButtonDecorator, 15 | var checkedDecorator: ViewDecorator?, var uncheckedDecorator: ViewDecorator?, 16 | var checkedBackgroundColor: Int, var checkedBorderColor: Int, 17 | var borderRadius: Float, var borderWidth: Float, 18 | var uncheckedBackgroundColor: Int, var uncheckedBorderColor: Int, 19 | var separatorColor: Int, var toggleMargin: Int) : 20 | LinearLayout(context), IRightToLeftProvider { 21 | 22 | interface ToggleSwitchButtonDecorator { 23 | fun decorate(toggleSwitchButton: ToggleSwitchButton, view: View, position: Int) 24 | } 25 | 26 | interface ViewDecorator { 27 | fun decorate(view: View, position: Int) 28 | } 29 | 30 | interface Listener { 31 | fun onToggleSwitchClicked(button: ToggleSwitchButton) 32 | } 33 | 34 | enum class PositionType { 35 | LEFT, CENTER, RIGHT 36 | } 37 | 38 | var toggleWidth = 0 39 | var toggleHeight = LinearLayout.LayoutParams.MATCH_PARENT 40 | var isChecked = false 41 | var rightToLeftProvider: IRightToLeftProvider = this 42 | 43 | var separator: View 44 | var view: View 45 | 46 | init { 47 | 48 | // Inflate Layout 49 | val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater 50 | val layoutView = inflater.inflate(R.layout.toggle_switch_button, this, true) as LinearLayout 51 | val relativeLayoutContainer = layoutView.findViewById(R.id.relative_layout_container) as RelativeLayout 52 | 53 | // View 54 | val params = RelativeLayout.LayoutParams( 55 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) 56 | params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE) 57 | params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE) 58 | 59 | view = LayoutInflater.from(context).inflate(layoutId, relativeLayoutContainer, false) 60 | relativeLayoutContainer.addView(view, params) 61 | 62 | // Bind Views 63 | separator = layoutView.findViewById(R.id.separator) 64 | val clickableWrapper: LinearLayout = findViewById(R.id.clickable_wrapper) 65 | 66 | // Setup View 67 | val layoutParams = LinearLayout.LayoutParams(toggleWidth, toggleHeight, 1.0f) 68 | 69 | if (toggleMargin > 0 && !isFirst()) { 70 | layoutParams.setMargins(toggleMargin, 0, 0, 0) 71 | } 72 | 73 | this.layoutParams = layoutParams 74 | 75 | this.orientation = HORIZONTAL 76 | this.background = getBackgroundDrawable(uncheckedBackgroundColor, uncheckedBorderColor) 77 | 78 | separator.setBackgroundColor(separatorColor) 79 | 80 | clickableWrapper.setOnClickListener { 81 | listener.onToggleSwitchClicked(this) 82 | } 83 | 84 | // Decorate 85 | prepareDecorator.decorate(this, view, position) 86 | uncheckedDecorator?.decorate(view, position) 87 | } 88 | 89 | // Public instance methods 90 | 91 | fun check() { 92 | this.background = getBackgroundDrawable(checkedBackgroundColor, checkedBorderColor) 93 | this.isChecked = true 94 | checkedDecorator?.decorate(view, position) 95 | } 96 | 97 | fun uncheck() { 98 | this.background = getBackgroundDrawable(uncheckedBackgroundColor, uncheckedBorderColor) 99 | this.isChecked = false 100 | uncheckedDecorator?.decorate(view, position) 101 | } 102 | 103 | fun setSeparatorVisibility(isSeparatorVisible : Boolean) { 104 | separator.visibility = if (isSeparatorVisible) View.VISIBLE else View.GONE 105 | } 106 | 107 | // Private instance methods 108 | 109 | private fun getBackgroundDrawable(backgroundColor : Int, borderColor : Int) : GradientDrawable { 110 | 111 | val gradientDrawable = GradientDrawable() 112 | gradientDrawable.setColor(backgroundColor) 113 | 114 | gradientDrawable.cornerRadii = getCornerRadii() 115 | 116 | if (borderWidth > 0) { 117 | gradientDrawable.setStroke(borderWidth.toInt(), borderColor) 118 | } 119 | 120 | return gradientDrawable 121 | } 122 | 123 | private fun getCornerRadii(): FloatArray { 124 | if (toggleMargin > 0) { 125 | return floatArrayOf(borderRadius,borderRadius, 126 | borderRadius, borderRadius, 127 | borderRadius, borderRadius, 128 | borderRadius,borderRadius) 129 | } 130 | else { 131 | val isRightToLeft = rightToLeftProvider.isRightToLeft() 132 | when(positionType) { 133 | PositionType.LEFT -> 134 | return if (isRightToLeft) getRightCornerRadii() else getLeftCornerRadii() 135 | 136 | PositionType.RIGHT -> 137 | return if (isRightToLeft) getLeftCornerRadii() else getRightCornerRadii() 138 | 139 | else -> return floatArrayOf(0f,0f,0f, 0f, 0f, 0f, 0f,0f) 140 | } 141 | } 142 | } 143 | 144 | override fun isRightToLeft(): Boolean { 145 | return resources.getBoolean(R.bool.is_right_to_left) 146 | } 147 | 148 | private fun getRightCornerRadii(): FloatArray { 149 | return floatArrayOf(0f,0f,borderRadius, borderRadius, borderRadius, borderRadius, 0f,0f) 150 | } 151 | 152 | private fun getLeftCornerRadii(): FloatArray { 153 | return floatArrayOf(borderRadius, borderRadius, 0f,0f,0f,0f, borderRadius, borderRadius) 154 | } 155 | 156 | private fun isFirst() : Boolean { 157 | if (rightToLeftProvider.isRightToLeft()) { 158 | return positionType == PositionType.RIGHT 159 | } 160 | else { 161 | return positionType == PositionType.LEFT 162 | } 163 | } 164 | } -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/java/com/llollox/androidtoggleswitch/widgets/Util.kt: -------------------------------------------------------------------------------- 1 | package com.llollox.androidtoggleswitch.widgets 2 | 3 | import android.content.Context 4 | 5 | fun dp2px(context: Context, dp: Float): Float { 6 | val metrics = context.resources.displayMetrics 7 | return dp * (metrics.densityDpi / 160f) 8 | } -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/layout/toggle_switch_button.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/layout/toggle_switch_button_view.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/layout/widget_toggle_switch.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/androidtoggleswitch/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/androidtoggleswitch/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/androidtoggleswitch/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/androidtoggleswitch/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/values-ldrtl/booleans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | true 4 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/values/booleans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | false 4 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #42A5F5 4 | #444444 5 | #999999 6 | #BBBBBB 7 | #DDDDDD 8 | 9 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | 16dp 7 | 8dp 8 | 4dp 9 | 10 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidToggleSwitch 3 | 4 | Hello world! 5 | Settings 6 | 7 | -------------------------------------------------------------------------------- /androidtoggleswitch/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.3.21' 5 | 6 | repositories { 7 | google() 8 | jcenter() 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:3.3.2' 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 13 | 14 | // NOTE: Do not place your application dependencies here; they belong 15 | // in the individual module build.gradle files 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | google() 22 | jcenter() 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /docs/basic.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/docs/basic.gif -------------------------------------------------------------------------------- /docs/custom.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/docs/custom.gif -------------------------------------------------------------------------------- /docs/custom_views.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/docs/custom_views.gif -------------------------------------------------------------------------------- /docs/match_width_height.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/docs/match_width_height.gif -------------------------------------------------------------------------------- /docs/screen.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/docs/screen.jpg -------------------------------------------------------------------------------- /docs/separated.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/docs/separated.gif -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Mon Jun 26 12:20:00 CEST 2017 16 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/llollox/Android-Toggle-Switch/2bd8da1eaf4743cc7162dfd74dbf03b768cd7f1e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Mar 05 14:52:57 CET 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto prepareLayout 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto prepareLayout 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :prepareLayout 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':androidtoggleswitch', ':androidtoggleswitch-sample' 2 | --------------------------------------------------------------------------------