Skip to content

Commit 57a8347

Browse files
committed
Migrated sample project to Kotlin
1 parent eb2c0a6 commit 57a8347

18 files changed

+1104
-600
lines changed

.idea/checkstyle-idea.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+49-58
Original file line numberDiff line numberDiff line change
@@ -59,79 +59,70 @@ Add the `SpeedDialView` to your layout:
5959

6060
#### Action items
6161
Add the items to the `SpeedDialView`:
62-
```java
63-
SpeedDialView speedDialView = findViewById(R.id.speedDial);
62+
```kotlin
63+
val speedDialView = findViewById<SpeedDialView>(R.id.speedDial)
6464
speedDialView.addActionItem(
65-
new SpeedDialActionItem.Builder(R.id.fab_link, R.drawable.ic_link_white_24dp)
66-
.create()
67-
);
68-
65+
SpeedDialActionItem.Builder(R.id.fab_no_label, R.drawable.ic_link_white_24dp)
66+
.create())
6967
```
7068

7169
If the color customization is not requested, it is also possible to inflate the Action items
7270
form a Menu Resource:
73-
```java
74-
speedDialView.inflate(R.menu.menu_speed_dial);
71+
```kotlin
72+
speedDialView.inflate(R.menu.menu_speed_dial)
7573
```
7674
Only the attributes `android:id`, `android:icon` and `android:title` are supported.
7775

7876
#### Click listeners
7977
Add the click listeners:
80-
```java
81-
speedDialView.setOnActionSelectedListener(new SpeedDialView.OnActionSelectedListener() {
82-
@Override
83-
public boolean onActionSelected(SpeedDialActionItem speedDialActionItem) {
84-
switch (speedDialActionItem.getId()) {
85-
case R.id.fab_link:
86-
showToast("Link action clicked!");
87-
return false; // true to keep the Speed Dial open
88-
default:
89-
return false;
78+
```kotlin
79+
speedDialView.setOnActionSelectedListener(SpeedDialView.OnActionSelectedListener { actionItem ->
80+
when (actionItem.id) {
81+
R.id.fab_no_label -> {
82+
showToast("No label action clicked!\nClosing with animation")
83+
speedDialView.close() // To close the Speed Dial with animation
84+
return@OnActionSelectedListener true // false will close it without animation
9085
}
9186
}
92-
});
87+
false
88+
})
9389
```
9490

9591
### Optional steps
9692
#### Add the main action click listener
97-
```java
98-
speedDialView.setOnChangeListener(new SpeedDialView.OnChangeListener() {
99-
@Override
100-
public void onMainActionSelected() {
101-
// Call your main action here
102-
return false; // true to keep the Speed Dial open
93+
```kotlin
94+
speedDialView.setOnChangeListener(object : SpeedDialView.OnChangeListener {
95+
override fun onMainActionSelected(): Boolean {
96+
showToast("Main action clicked!")
97+
return false // True to keep the Speed Dial open
10398
}
10499

105-
@Override
106-
public void onToggleChanged(boolean isOpen) {
107-
Log.d(TAG, "Speed dial toggle state changed. Open = " + isOpen);
100+
override fun onToggleChanged(isOpen: Boolean) {
101+
Log.d(TAG, "Speed dial toggle state changed. Open = $isOpen")
108102
}
109-
});
103+
})
110104
```
111105

112106
#### Customizing the items
113107
The `SpeedDialActionItem.Builder` provides several setters to customize the aspect of one item:
114108

115-
```java
116-
mSpeedDialView.addActionItem(
117-
new SpeedDialActionItem.Builder(R.id.fab_custom_color, R.drawable.ic_custom_color)
118-
.setFabBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.material_white_1000, getTheme()))
119-
.setFabImageTintColor(ResourcesCompat.getColor(getResources(), R.color.inbox_primary, getTheme()))
120-
.setLabel(getString(R.string.label_custom_color))
121-
.setLabelColor(Color.WHITE)
122-
.setLabelBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.inbox_primary, getTheme()))
123-
.setLabelClickable(false)
124-
.create()
125-
);
109+
```kotlin
110+
speedDialView.addActionItem(SpeedDialActionItem.Builder(R.id.fab_custom_color, drawable)
111+
.setFabBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.material_white_1000, getTheme()))
112+
.setFabImageTintColor(ResourcesCompat.getColor(getResources(), R.color.inbox_primary, getTheme()))
113+
.setLabel(getString(R.string.label_custom_color))
114+
.setLabelColor(Color.WHITE)
115+
.setLabelBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.inbox_primary, getTheme()))
116+
.setLabelClickable(false)
117+
.create())
126118
```
127119
Is is also possible to specify a theme to easily change the FAB background and ripple effect color:
128120

129-
```java
130-
mSpeedDialView.addActionItem(
131-
new SpeedDialActionItem.Builder(R.id.fab_custom_theme, R.drawable.ic_theme_white_24dp)
132-
.setLabel(getString(R.string.label_custom_theme))
133-
.setTheme(R.style.AppTheme_Purple)
134-
.create());
121+
```kotlin
122+
speedDialView.addActionItem(SpeedDialActionItem.Builder(R.id.fab_custom_theme, R.drawable.ic_theme_white_24dp)
123+
.setLabel(getString(R.string.label_custom_theme))
124+
.setTheme(R.style.AppTheme_Purple)
125+
.create())
135126
```
136127
```xml
137128
<style name="AppTheme.Purple" parent="AppTheme">
@@ -162,9 +153,9 @@ and then provide the instance of that layout to the `SpeedDialView`:
162153
app:sdOverlayLayout="@id/overlay" />
163154
```
164155
or
165-
```java
166-
SpeedDialOverlayLayout overlayLayout = findViewById(R.id.overlay);
167-
mSpeedDialView.setSpeedDialOverlayLayout(overlayLayout);
156+
```kotlin
157+
val overlayLayout = findViewById<SpeedDialOverlayLayout>(R.id.overlay)
158+
speedDialView.setSpeedDialOverlayLayout(overlayLayout)
168159
```
169160

170161
#### Hiding the FAB when scrolling a `RecyclerView` or a `NestedScrollView`
@@ -180,20 +171,20 @@ the convenience string resource `@string/speeddial_scrolling_view_snackbar_behav
180171
```
181172

182173
Or programmatically:
183-
```java
184-
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) speedDialView.getLayoutParams();
185-
params.setBehavior(new SpeedDialView.ScrollingViewSnackbarBehavior());
186-
speedDialView.requestLayout();
174+
```kotlin
175+
val params = speedDialView.layoutParams as CoordinatorLayout.LayoutParams
176+
params.behavior = SpeedDialView.ScrollingViewSnackbarBehavior()
177+
speedDialView.requestLayout()
187178
```
188179

189180
NB: for the behaviors to work, `SpeedDialView` needs to be a direct child of `CoordinatorLayout`
190181

191182
#### Disabling `SnackbarBehavior`
192183
Since the `SnackbarBehavior` is enabled by default and, afaik, it is not possible to remove a Behavior, simply use apply the `SpeedDialView.NoBehavior` instead:
193-
```java
194-
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) speedDialView.getLayoutParams();
195-
params.setBehavior(new SpeedDialView.NoBehavior());
196-
speedDialView.requestLayout();
184+
```kotlin
185+
val params = speedDialView.layoutParams as CoordinatorLayout.LayoutParams
186+
params.behavior = SpeedDialView.NoBehavior()
187+
speedDialView.requestLayout()
197188
```
198189

199190
### Sample project
@@ -250,7 +241,7 @@ This project is based on [floating-action-menu by ArthurGhazaryan](https://githu
250241

251242
## Licenses
252243
```
253-
Copyright 2018 Roberto Leinardi.
244+
Copyright 2019 Roberto Leinardi.
254245
255246
Licensed to the Apache Software Foundation (ASF) under one or more contributor
256247
license agreements. See the NOTICE file distributed with this work for

build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
apply from: rootProject.file('versions-plugin.gradle')
17+
apply from: rootProject.file('detekt.gradle')
1718
apply plugin: "se.bjurr.violations.violation-comments-to-github-gradle-plugin"
1819

1920
buildscript {
@@ -22,13 +23,15 @@ buildscript {
2223
addRepos(repositories)
2324
}
2425
dependencies {
26+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
2527
classpath "com.android.tools.build:gradle:$versions.android_gradle_plugin"
2628
classpath "com.leinardi.android:android-checkstyle-plugin:$versions.android_checkstyle_plugin"
2729
classpath "com.github.dcendents:android-maven-gradle-plugin:$versions.android_maven_gradle_plugin"
2830
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$versions.gradle_bintray_plugin"
2931
classpath "com.github.ben-manes:gradle-versions-plugin:$versions.gradle_versions_plugin"
3032
classpath "se.bjurr.violations:violation-comments-to-github-gradle-plugin:$versions.violations_plugin"
3133
classpath "net.ltgt.gradle:gradle-errorprone-plugin:$versions.errorprone_plugin"
34+
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$versions.detekt"
3235
// NOTE: Do not place your application dependencies here; they belong
3336
// in the individual module build.gradle files
3437
}

checkstyle.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ checkstyle {
2020
ignoreFailures = false // Whether this task will ignore failures and continue running the build.
2121
configFile rootProject.file('config/checkstyle/checkstyle.xml')
2222
// The Checkstyle configuration file to use.
23-
toolVersion = '8.16' // The version of Checkstyle you want to be used
23+
toolVersion = '8.23' // The version of Checkstyle you want to be used
2424
}

0 commit comments

Comments
 (0)