Skip to content

Commit

Permalink
Make extensions usable in java via DraggableUtils
Browse files Browse the repository at this point in the history
- Update Readme
- Add new example in java
  • Loading branch information
hyuwah authored and Muhamad Wahyudin committed Oct 28, 2019
1 parent 7b93fcb commit 990f277
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 25 deletions.
49 changes: 41 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
[![](https://jitpack.io/v/hyuwah/DraggableView.svg)](https://jitpack.io/#hyuwah/DraggableView)

DraggableView is an Android library to make floating draggable view easy, currently it only extends from ImageView.
If you're using kotlin there's also an extension to make any view draggable.
Now you can make any view (or viewgroup) draggable using extensions on Kotlin & provided utils class on Java

![Preview](https://miro.medium.com/max/314/1*dMzIJlT12hmSTkVkzNnxEQ.gif)

## Setup

Expand All @@ -29,7 +31,7 @@ dependencies {
}
```

**Note:** check the number on jitpack badge above for latest version
**Note:** check the number on Jitpack badge above for latest version

## Usage

Expand Down Expand Up @@ -81,20 +83,51 @@ Here's some example using TextView:
```kotlin
var tv = findViewById<TextView>(R.id.tv_test_draggable)

tv.makeDraggable(Constants.STICKY.AXIS_X, true)
tv.makeDraggable(Draggable.STICKY.AXIS_X, true) // default is STICKY.NONE & animated true

// First param is the axis:
// - Constants.STICKY.AXIS_X
// - Constants.STICKY.AXIS_Y
// - Constants.STICKY.AXIS_XY
// - Constants.STICKY.NONE
// - Draggable.STICKY.AXIS_X
// - Draggable.STICKY.AXIS_Y
// - Draggable.STICKY.AXIS_XY
// - Draggable.STICKY.NONE

// Second param is animation toggle
// - true or false
```

#### Using DraggableUtils (on Java)

If you're on java class, you could do it with the help of DraggableUtils

Here's some example using Button:

```xml
<Button
android:id="@+id/tv_test_draggable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DRAGGABLE BUTTON!" />
```

```java
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](https://github.com/hyuwah/DraggableView/blob/master/example/src/main/java/io/github/hyuwah/draggableview/MainActivity.kt) module for actual implementation
Check example module [kotlin](https://github.com/hyuwah/DraggableView/blob/master/example/src/main/java/io/github/hyuwah/draggableview/MainActivity.kt), [java](https://github.com/hyuwah/DraggableView/blob/master/example/src/main/java/io/github/hyuwah/draggableview/JavaMainActivity.java) for actual implementation

## Accompanying Article

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.hyuwah.draggableviewlib

object Constants {
object Draggable {

enum class STICKY {
NONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import io.github.hyuwah.draggableviewlib.Constants.DRAG_TOLERANCE
import io.github.hyuwah.draggableviewlib.Draggable.DRAG_TOLERANCE
import kotlin.math.abs

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
@file:JvmName("DraggableUtils")
package io.github.hyuwah.draggableviewlib

import android.view.MotionEvent
import android.view.View
import io.github.hyuwah.draggableviewlib.Constants.DRAG_TOLERANCE
import io.github.hyuwah.draggableviewlib.Draggable.DRAG_TOLERANCE
import java.lang.Math.max
import kotlin.math.abs
import kotlin.math.min

@JvmOverloads
fun View.makeDraggable(
stickyAxis: Constants.STICKY = Constants.STICKY.NONE,
stickyAxis: Draggable.STICKY = Draggable.STICKY.NONE,
animated: Boolean = true
) {
var widgetInitialX = 0f
Expand Down Expand Up @@ -40,7 +42,7 @@ fun View.makeDraggable(
}
MotionEvent.ACTION_UP -> {
when (stickyAxis) {
Constants.STICKY.AXIS_X -> {
Draggable.STICKY.AXIS_X -> {
if (event.rawX >= PARENT_WIDTH / 2) {
if (animated)
v.animate().x((PARENT_WIDTH) - (v.width).toFloat()).setDuration(250).start()
Expand All @@ -53,7 +55,7 @@ fun View.makeDraggable(
v.x = 0F
}
}
Constants.STICKY.AXIS_Y -> {
Draggable.STICKY.AXIS_Y -> {
if (event.rawY >= PARENT_HEIGHT / 2) {
if (animated)
v.animate().y((PARENT_HEIGHT) - (v.height).toFloat()).setDuration(
Expand All @@ -72,7 +74,7 @@ fun View.makeDraggable(
}
}
}
Constants.STICKY.AXIS_XY -> {
Draggable.STICKY.AXIS_XY -> {
if (event.rawX >= PARENT_WIDTH / 2) {
if (animated)
v.animate().x((PARENT_WIDTH) - (v.width).toFloat()).setDuration(250).start()
Expand Down
2 changes: 1 addition & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
Expand Down
7 changes: 4 additions & 3 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.hyuwah.draggableview">
package="io.github.hyuwah.draggableview">

<application
android:allowBackup="true"
Expand All @@ -9,11 +9,12 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".JavaMainActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.hyuwah.draggableview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import io.github.hyuwah.draggableviewlib.Draggable;
import io.github.hyuwah.draggableviewlib.DraggableUtils;

public class JavaMainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_main);

Button button = findViewById(R.id.btn_java);

DraggableUtils.makeDraggable(button, Draggable.STICKY.AXIS_X, true);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(JavaMainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});

}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package io.github.hyuwah.draggableview

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.Switch
import android.widget.Toast
import io.github.hyuwah.draggableviewlib.Constants
import io.github.hyuwah.draggableviewlib.Draggable
import io.github.hyuwah.draggableviewlib.DraggableImageView
import io.github.hyuwah.draggableviewlib.makeDraggable
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

var currentStickyAxis = Constants.STICKY.AXIS_X
var currentStickyAxis = Draggable.STICKY.AXIS_X

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -48,7 +49,7 @@ class MainActivity : AppCompatActivity() {
// Set sticky axis to X axis
btnStickyX.setOnClickListener {
dvTest.setStickyAxis(DraggableImageView.STICKY_AXIS_X)
currentStickyAxis = Constants.STICKY.AXIS_X
currentStickyAxis = Draggable.STICKY.AXIS_X
ll_test_draggable.makeDraggable(currentStickyAxis, swAnimate.isChecked)
tv_test_draggable.makeDraggable(currentStickyAxis, swAnimate.isChecked)
Toast.makeText(this@MainActivity, "Sticky Axis set to X", Toast.LENGTH_SHORT).show()
Expand All @@ -57,7 +58,7 @@ class MainActivity : AppCompatActivity() {
// Set sticky axis to Y axis
btnStickyY.setOnClickListener {
dvTest.setStickyAxis(DraggableImageView.STICKY_AXIS_Y)
currentStickyAxis = Constants.STICKY.AXIS_Y
currentStickyAxis = Draggable.STICKY.AXIS_Y
ll_test_draggable.makeDraggable(currentStickyAxis, swAnimate.isChecked)
tv_test_draggable.makeDraggable(currentStickyAxis, swAnimate.isChecked)
Toast.makeText(this@MainActivity, "Sticky Axis set to Y", Toast.LENGTH_SHORT).show()
Expand All @@ -66,7 +67,7 @@ class MainActivity : AppCompatActivity() {
// Set sticky axis to XY axis
btnStickyXY.setOnClickListener {
dvTest.setStickyAxis(DraggableImageView.STICKY_AXIS_XY)
currentStickyAxis = Constants.STICKY.AXIS_XY
currentStickyAxis = Draggable.STICKY.AXIS_XY
ll_test_draggable.makeDraggable(currentStickyAxis, swAnimate.isChecked)
tv_test_draggable.makeDraggable(currentStickyAxis, swAnimate.isChecked)
Toast.makeText(this@MainActivity, "Sticky Axis set to XY", Toast.LENGTH_SHORT).show()
Expand All @@ -75,7 +76,7 @@ class MainActivity : AppCompatActivity() {
// Remove sticky axis / will float
btnNonSticky.setOnClickListener {
dvTest.setStickyAxis(DraggableImageView.NON_STICKY)
currentStickyAxis = Constants.STICKY.NONE
currentStickyAxis = Draggable.STICKY.NONE
ll_test_draggable.makeDraggable(currentStickyAxis, swAnimate.isChecked)
tv_test_draggable.makeDraggable(currentStickyAxis, swAnimate.isChecked)
Toast.makeText(this@MainActivity, "Sticky Axis set none", Toast.LENGTH_SHORT).show()
Expand All @@ -84,6 +85,7 @@ class MainActivity : AppCompatActivity() {
// Set click listener
dvTest.setOnClickListener {
Toast.makeText(this@MainActivity, "Clicked", Toast.LENGTH_SHORT).show()
startActivity(Intent(this,JavaMainActivity::class.java))
}

ll_test_draggable.setOnClickListener {
Expand Down
20 changes: 20 additions & 0 deletions example/src/main/res/layout/activity_java_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".JavaMainActivity">

<Button
android:id="@+id/btn_java"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</android.support.constraint.ConstraintLayout>

0 comments on commit 990f277

Please sign in to comment.