Skip to content

Commit b953164

Browse files
committed
Kotlin
1 parent 53e6e86 commit b953164

32 files changed

+456
-447
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Easy creation and management of toggle buttons from the Material Design [spec](h
88

99
[![Build Status](https://travis-ci.org/savvyapps/ToggleButtonLayout.svg?branch=master)](https://travis-ci.org/savvyapps/ToggleButtonLayout) [![](https://jitpack.io/v/savvyapps/ToggleButtonLayout.svg)](https://jitpack.io/#savvyapps/ToggleButtonLayout)
1010

11-
# Dependency
11+
## Dependency
1212

1313
Add this in your root `build.gradle` file (**not** your module `build.gradle` file):
1414

@@ -28,7 +28,7 @@ dependencies {
2828
}
2929
```
3030

31-
# Usage
31+
## Usage
3232
Add the ToggleButtonLayout to your layout:
3333
```xml
3434
<com.savvyapps.togglebuttonlayout.ToggleButtonLayout
@@ -60,12 +60,12 @@ where the toggles menu looks like:
6060
You can safely ignore lint warnings about needing a title on each item, unless you want a title to appear on each item.
6161

6262
Later, you can get the selected items via:
63-
```java
64-
List<Toggle> selectedToggles = toggleButtonLayout.getSelectedToggles();
63+
```kotlin
64+
val selectedToggles = toggleButtonLayout.getSelectedToggles()
6565
//do what you need to with these selected toggles
6666
```
6767

68-
# Customization
68+
## Customization
6969
You can customize the `ToggleButtonLayout` via XML attributes:
7070
```xml
7171
<com.savvyapps.togglebuttonlayout.ToggleButtonLayout
@@ -85,6 +85,9 @@ You can customize the `ToggleButtonLayout` via XML attributes:
8585

8686
If you use the `customLayout` attribute, the layout is expected to have a `TextView` with an ID of `android:id="@android:id/text1"` if you are using a title, and if you are using an icon, `android:id="@android:id/icon"`. You can omit either of these if you are only using a menu resource with a title or just an icon. See the sample for more.
8787

88+
## Notes
89+
- If you need to rely on a Java version of ToggleButtonLayout, you can use the `java` branch.
90+
8891
License
8992
--------
9093

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
24

35
android {
46
compileSdkVersion rootProject.ext.compileSdkVersion
@@ -26,6 +28,8 @@ android {
2628
}
2729

2830
dependencies {
31+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
32+
2933
implementation "com.android.support:appcompat-v7:$supportLibVersion"
3034
implementation "com.android.support:cardview-v7:$supportLibVersion"
3135
implementation "com.android.support:design:$supportLibVersion"

app/src/main/ic_launcher-web.png

15.6 KB
Loading

app/src/main/java/com/savvyapps/togglebuttonlayout/sample/ToggleButtonLayoutActivity.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.savvyapps.togglebuttonlayout.sample
2+
3+
import android.os.Bundle
4+
import android.support.design.widget.Snackbar
5+
import android.support.v7.app.AppCompatActivity
6+
import kotlinx.android.synthetic.main.activity_toggle_button.*
7+
8+
class ToggleButtonLayoutActivity : AppCompatActivity() {
9+
10+
override fun onCreate(savedInstanceState: Bundle?) {
11+
super.onCreate(savedInstanceState)
12+
setContentView(R.layout.activity_toggle_button)
13+
14+
toolbar.setTitle(R.string.app_name)
15+
16+
toggleButtonLayout.onToggledListener = { toggle, selected ->
17+
Snackbar.make(root, "Toggle " + toggle.id + " selected state " + selected, Snackbar.LENGTH_LONG)
18+
.show()
19+
}
20+
}
21+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector
3+
android:height="108dp"
4+
android:width="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108"
7+
xmlns:android="http://schemas.android.com/apk/res/android">
8+
<path android:fillColor="#26A69A"
9+
android:pathData="M0,0h108v108h-108z"/>
10+
<path android:fillColor="#00000000" android:pathData="M9,0L9,108"
11+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
12+
<path android:fillColor="#00000000" android:pathData="M19,0L19,108"
13+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
14+
<path android:fillColor="#00000000" android:pathData="M29,0L29,108"
15+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
16+
<path android:fillColor="#00000000" android:pathData="M39,0L39,108"
17+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
18+
<path android:fillColor="#00000000" android:pathData="M49,0L49,108"
19+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
20+
<path android:fillColor="#00000000" android:pathData="M59,0L59,108"
21+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
22+
<path android:fillColor="#00000000" android:pathData="M69,0L69,108"
23+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
24+
<path android:fillColor="#00000000" android:pathData="M79,0L79,108"
25+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
26+
<path android:fillColor="#00000000" android:pathData="M89,0L89,108"
27+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
28+
<path android:fillColor="#00000000" android:pathData="M99,0L99,108"
29+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
30+
<path android:fillColor="#00000000" android:pathData="M0,9L108,9"
31+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
32+
<path android:fillColor="#00000000" android:pathData="M0,19L108,19"
33+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
34+
<path android:fillColor="#00000000" android:pathData="M0,29L108,29"
35+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
36+
<path android:fillColor="#00000000" android:pathData="M0,39L108,39"
37+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
38+
<path android:fillColor="#00000000" android:pathData="M0,49L108,49"
39+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
40+
<path android:fillColor="#00000000" android:pathData="M0,59L108,59"
41+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
42+
<path android:fillColor="#00000000" android:pathData="M0,69L108,69"
43+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
44+
<path android:fillColor="#00000000" android:pathData="M0,79L108,79"
45+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
46+
<path android:fillColor="#00000000" android:pathData="M0,89L108,89"
47+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
48+
<path android:fillColor="#00000000" android:pathData="M0,99L108,99"
49+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
50+
<path android:fillColor="#00000000" android:pathData="M19,29L89,29"
51+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
52+
<path android:fillColor="#00000000" android:pathData="M19,39L89,39"
53+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
54+
<path android:fillColor="#00000000" android:pathData="M19,49L89,49"
55+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
56+
<path android:fillColor="#00000000" android:pathData="M19,59L89,59"
57+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
58+
<path android:fillColor="#00000000" android:pathData="M19,69L89,69"
59+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
60+
<path android:fillColor="#00000000" android:pathData="M19,79L89,79"
61+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
62+
<path android:fillColor="#00000000" android:pathData="M29,19L29,89"
63+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
64+
<path android:fillColor="#00000000" android:pathData="M39,19L39,89"
65+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
66+
<path android:fillColor="#00000000" android:pathData="M49,19L49,89"
67+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
68+
<path android:fillColor="#00000000" android:pathData="M59,19L59,89"
69+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
70+
<path android:fillColor="#00000000" android:pathData="M69,19L69,89"
71+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
72+
<path android:fillColor="#00000000" android:pathData="M79,19L79,89"
73+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
74+
</vector>

app/src/main/res/layout/activity_toggle_button.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
android:paddingTop="24dp">
3333

3434
<com.savvyapps.togglebuttonlayout.ToggleButtonLayout
35-
android:id="@+id/toggle"
35+
android:id="@+id/toggleButtonLayout"
3636
android:layout_width="wrap_content"
3737
android:layout_height="wrap_content"
3838
android:layout_gravity="center_horizontal"
3939
android:layout_marginBottom="16dp"
4040
app:menu="@menu/toggles"/>
4141

4242
<com.savvyapps.togglebuttonlayout.ToggleButtonLayout
43-
android:id="@+id/toggle_multiple"
43+
android:id="@+id/toggleMultiple"
4444
android:layout_width="wrap_content"
4545
android:layout_height="wrap_content"
4646
android:layout_gravity="center_horizontal"
@@ -51,7 +51,7 @@
5151
app:multipleSelection="true"/>
5252

5353
<com.savvyapps.togglebuttonlayout.ToggleButtonLayout
54-
android:id="@+id/toggle_text"
54+
android:id="@+id/toggleText"
5555
android:layout_width="match_parent"
5656
android:layout_height="wrap_content"
5757
android:layout_gravity="center_horizontal"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3+
<background android:drawable="@drawable/ic_launcher_background"/>
4+
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
5+
</adaptive-icon>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3+
<background android:drawable="@drawable/ic_launcher_background"/>
4+
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
5+
</adaptive-icon>
-872 Bytes
Loading
Loading
Loading
-414 Bytes
Loading
Loading
Loading
-1 KB
Loading
Loading
Loading
-2.5 KB
Loading
Loading
Loading
-3.6 KB
Loading
Loading
Loading

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
buildscript {
2+
ext.kotlin_version = '1.2.21'
23
repositories {
34
jcenter()
45
google()
56
}
67
dependencies {
78
classpath 'com.android.tools.build:gradle:3.0.1'
89
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
10+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
911
}
1012
}
1113

togglebuttonlayout/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
23

34
android {
45
compileSdkVersion rootProject.ext.compileSdkVersion
@@ -24,6 +25,8 @@ android {
2425
}
2526

2627
dependencies {
28+
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
29+
2730
api "com.android.support:appcompat-v7:$supportLibVersion"
2831
api "com.android.support:cardview-v7:$supportLibVersion"
2932
}

togglebuttonlayout/src/main/java/com/savvyapps/togglebuttonlayout/Toggle.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.savvyapps.togglebuttonlayout
2+
3+
import android.graphics.drawable.Drawable
4+
5+
/**
6+
* An item within a [ToggleButtonLayout]
7+
*/
8+
class Toggle(
9+
10+
/**
11+
* The ID provided within the Toggle
12+
*/
13+
val id: Int,
14+
15+
/**
16+
* The icon drawable inflated by the menu resource or passed during creation
17+
*/
18+
val icon: Drawable?,
19+
20+
/**
21+
* Optional title
22+
*/
23+
val title: CharSequence?) {
24+
25+
/**
26+
* The selection state of the toggle
27+
*/
28+
var isSelected: Boolean = false
29+
30+
init {
31+
if (id == 0) {
32+
throw IllegalArgumentException("Toggle must have a non-zero id")
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)