Option to have different icons for open/close state
Optional overlay/touch guard layout
Support for bottom, left and right menu expansion (left and right have no labels)
Out-of-the box support for Snackbar behavior
Optional support for RecyclerView and NestedScrollView behavior
Support for VectorDrawable
Easy to use
Compose ready!
How to use
Gradle setup
Official releases
The library is available on Jcenter so no additional repository is required.
Dependencies entry (latest version on Maven Central ):
// Compose only
implementation "com.leinardi.android:speed-dial.compose:1.0.0-alpha04"// Classic View only
implementation "com.leinardi.android:speed-dial:3.3.0"
Snapshots (development branch)
You can use JitPack to test the latest master (remember that master is the development branch and can be unstable or completely broken).
val speedDialView = findViewById<SpeedDialView>(R.id.speedDial)
speedDialView.addActionItem(
SpeedDialActionItem.Builder(R.id.fab_no_label, R.drawable.ic_link_white_24dp)
.create()
)
If the color customization is not requested, it is also possible to inflate the Action items form a Menu Resource:
speedDialView.inflate(R.menu.menu_speed_dial)
Only the attributes android:id, android:icon and android:title are supported.
Click listeners
Add the click listeners:
speedDialView.setOnActionSelectedListener(SpeedDialView.OnActionSelectedListener { actionItem ->when (actionItem.id) {
R.id.fab_no_label -> {
showToast("No label action clicked!\nClosing with animation")
speedDialView.close() // To close the Speed Dial with animationreturn@OnActionSelectedListener true// false will close it without animation
}
}
false
})
Optional steps
Add the main action click listener
speedDialView.setOnChangeListener(object:SpeedDialView.OnChangeListener {
overridefunonMainActionSelected(): Boolean {
showToast("Main action clicked!")
returnfalse// True to keep the Speed Dial open
}
overridefunonToggleChanged(isOpen:Boolean) {
Log.d(TAG, "Speed dial toggle state changed. Open = $isOpen")
}
})
Customizing the items
The SpeedDialActionItem.Builder provides several setters to customize the aspect of one item:
val overlayLayout = findViewById<SpeedDialOverlayLayout>(R.id.overlay)
speedDialView.setSpeedDialOverlayLayout(overlayLayout)
Hiding the FAB when scrolling a RecyclerView or a NestedScrollView
Just apply the ScrollingViewSnackbarBehavior to the SpeedDialView. This can be done via XML using the convenience string
resource @string/speeddial_scrolling_view_snackbar_behavior:
val params = speedDialView.layoutParams asCoordinatorLayout.LayoutParams
params.behavior =SpeedDialView.ScrollingViewSnackbarBehavior()
speedDialView.requestLayout()
NB: for the behaviors to work, SpeedDialView needs to be a direct child of CoordinatorLayout
Disabling SnackbarBehavior
Since the SnackbarBehavior is enabled by default and, afaik, it is not possible to remove a Behavior, simply use apply
the SpeedDialView.NoBehavior instead:
val params = speedDialView.layoutParams asCoordinatorLayout.LayoutParams
params.behavior =SpeedDialView.NoBehavior()
speedDialView.requestLayout()
How can I change the color of the overlay / touch guard layout?
The color of the SpeedDialOverlayLayout can be changed simply using the android:background attribute or, programmatically, using the equivalent
setter like any other view.
How can I prevent the overlay / touch guard layout from going over the Toolbar inside a CoordinatorLayout?
It can be done using the attribute app:layout_behavior="@string/appbar_scrolling_view_behavior":
Copyright 2021 Roberto Leinardi.
Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
additional information regarding copyright ownership. The ASF licenses this
file to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
请发表评论