DraggableView is an Android library to make floating draggable view easy, currently it only extends from ImageView. Now you can make any view (or viewgroup) draggable using extensions on Kotlin & provided utils class on Java
On root / project build.gradle file add maven { url 'https://jitpack.io' }
Example:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
On app build.gradle add implementation 'com.github.hyuwah:DraggableView:LatestVersion'
Example:
dependencies {
...
implementation 'com.github.hyuwah:DraggableView:LatestVersion'
...
}
Note: check the number on Jitpack badge above for latest version
DraggableImageView
Attribute | Value (Default) | XML | Code |
---|---|---|---|
Animate | true, false (false) | animate | setAnimate(boolean isAnimate) |
Sticky Axis | NON_STICKY, STICKY_AXIS_X, STICKY_AXIS_Y, STICKY_AXIS_XY (NON_STICKY) | sticky | setStickyAxis(int axis) |
On Layout XML file
<io.github.hyuwah.draggableviewlib.DraggableImageView
android:src="@mipmap/ic_launcher_round"
android:id="@+id/draggableView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
On Activity / Fragment file
var dv = findViewById<DraggableImageView>(R.id.draggableView)
dv.setOnClickListener {
// TODO on click
}
You can extent any view or viewgroup to be draggable (i.e. Button, FrameLayout, Linearlayout, LottieView, etc)
Here's some example using TextView:
<TextView
android:id="@+id/tv_test_draggable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DRAG ME!" />
var tv = findViewById<TextView>(R.id.tv_test_draggable)
tv.makeDraggable(Draggable.STICKY.AXIS_X, true) // default is STICKY.NONE & animated true
// First param is the axis:
// - Draggable.STICKY.AXIS_X
// - Draggable.STICKY.AXIS_Y
// - Draggable.STICKY.AXIS_XY
// - Draggable.STICKY.NONE
// Second param is animation toggle
// - true or false
If you're on java class, you could do it with the help of DraggableUtils
Here's some example using Button:
<Button
android:id="@+id/tv_test_draggable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DRAGGABLE BUTTON!" />
Button button = findViewById(R.id.tv_test_draggable);
DraggableUtils.makeDraggable(button, Draggable.STICKY.AXIS_X, true); // default is STICKY.NONE & animated true
// First param is the view
// Second param is the axis:
// - Draggable.STICKY.AXIS_X
// - Draggable.STICKY.AXIS_Y
// - Draggable.STICKY.AXIS_XY
// - Draggable.STICKY.NONE
// Third param is animation toggle
// - true or false
Check example module kotlin, java for actual implementation
DraggableView
is available under the MIT license. See the LICENSE file for more info.