Skip to content

Commit f54f27c

Browse files
committed
added:
- vibration function - listeners
1 parent 67d0dc2 commit f54f27c

File tree

9 files changed

+120
-1
lines changed

9 files changed

+120
-1
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/button/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="cls.android.button">
44

5+
<uses-permission android:name="android.permission.VIBRATE" />
56
</manifest>

app/button/src/main/java/cls/android/button/Button.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33
import android.content.Context;
44
import android.util.AttributeSet;
55
import android.view.LayoutInflater;
6+
import android.view.View;
67
import android.widget.FrameLayout;
78

89
import androidx.annotation.NonNull;
910
import androidx.annotation.Nullable;
11+
import androidx.constraintlayout.motion.widget.MotionLayout;
1012

1113
public class Button extends FrameLayout {
14+
public static final int START = 0;
15+
public static final int END = 1;
16+
17+
private OnStateChangedListener onSwipeListener;
18+
19+
private MotionLayout motionLayout;
20+
private boolean isVibrationEnabled = false;
21+
1222
public Button(@NonNull Context context) {
1323
super(context);
1424
init();
@@ -31,5 +41,57 @@ public Button(@NonNull Context context, @Nullable AttributeSet attrs, int defSty
3141

3242
private void init() {
3343
LayoutInflater.from(getContext()).inflate(R.layout.layout_button_swipe,this);
44+
initViews();
45+
initListener();
46+
}
47+
48+
private void initListener() {
49+
motionLayout.setTransitionListener(new MotionLayout.TransitionListener() {
50+
@Override
51+
public void onTransitionStarted(MotionLayout motionLayout, int i, int i1) {
52+
53+
}
54+
55+
@Override
56+
public void onTransitionChange(MotionLayout motionLayout, int i, int i1, float v) {
57+
58+
}
59+
60+
@Override
61+
public void onTransitionCompleted(MotionLayout motionLayout, int i) {
62+
if (isVibrationEnabled)
63+
HardwareUtil.vibrate(getContext());
64+
if (onSwipeListener != null)
65+
onSwipeListener
66+
.onChange(
67+
motionLayout.getCurrentState() == motionLayout.getStartState() ?
68+
START : END
69+
);
70+
71+
}
72+
73+
@Override
74+
public void onTransitionTrigger(MotionLayout motionLayout, int i, boolean b, float v) {
75+
76+
}
77+
});
78+
79+
}
80+
81+
private void initViews() {
82+
motionLayout = findViewById(R.id.motionlayout);
83+
}
84+
85+
public void setOnSwipeListener(OnStateChangedListener onSwipeListener){
86+
this.onSwipeListener = onSwipeListener;
87+
}
88+
89+
public void isVibrationEnabled(boolean b) {
90+
isVibrationEnabled = b;
91+
}
92+
93+
public interface OnStateChangedListener{
94+
void onChange(int i);
95+
3496
}
3597
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cls.android.button;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.os.Build;
6+
import android.os.VibrationEffect;
7+
import android.os.Vibrator;
8+
9+
public class HardwareUtil {
10+
private static final long CONSTANT_VIBRATE_MILLIS = 50;
11+
12+
public static void vibrate(Context context){
13+
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
14+
// Vibrate for 500 milliseconds
15+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
16+
v.vibrate(VibrationEffect.createOneShot(CONSTANT_VIBRATE_MILLIS,
17+
VibrationEffect.DEFAULT_AMPLITUDE));
18+
} else {
19+
//deprecated in API 26
20+
v.vibrate(CONSTANT_VIBRATE_MILLIS);
21+
}
22+
}
23+
}

app/button/src/main/res/layout/layout_button_swipe.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
5+
android:id="@+id/motionlayout"
56
android:layout_height="@dimen/btn_height_swipe"
67
xmlns:app="http://schemas.android.com/apk/res-auto"
78
layoutDescription="@xml/layout_button_swipe_scene"

app/button/src/main/res/xml/layout_button_swipe_scene.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Constraint android:id="@+id/constraint_swipe_inner_btn"
2222
motion:layout_constraintStart_toStartOf="@+id/view"
2323
motion:layout_constraintTop_toTopOf="@+id/view"
24+
android:rotation="0"
2425
motion:layout_constraintBottom_toBottomOf="@+id/view"
2526
android:layout_width="50dp"
2627
android:layout_height="50dp"
@@ -61,6 +62,7 @@
6162
motion:layout_constraintBottom_toBottomOf="@+id/view"
6263
android:layout_width="50dp"
6364
android:layout_height="50dp"
65+
android:rotation="180"
6466
android:layout_marginEnd="@dimen/margin_large"
6567

6668
/>

app/src/main/java/cls/android/someuidesigns/MainActivity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
import androidx.navigation.ui.AppBarConfiguration;
1414
import androidx.navigation.ui.NavigationUI;
1515

16+
import cls.android.button.Button;
1617
import cls.android.someuidesigns.databinding.ActivityMainBinding;
1718

1819
import android.view.Menu;
1920
import android.view.MenuItem;
21+
import android.widget.Toast;
2022

2123
public class MainActivity extends AppCompatActivity {
2224

2325
private AppBarConfiguration appBarConfiguration;
2426
private ActivityMainBinding binding;
27+
private Button button;
2528

2629
@Override
2730
protected void onCreate(Bundle savedInstanceState) {
@@ -43,6 +46,26 @@ public void onClick(View view) {
4346
.setAction("Action", null).show();
4447
}
4548
});
49+
button = findViewById(R.id.button);
50+
button.setOnSwipeListener(new Button.OnStateChangedListener() {
51+
@Override
52+
public void onChange(int i) {
53+
if (i == Button.START){
54+
Toast.makeText(MainActivity.this, "Start", Toast.LENGTH_SHORT).show();
55+
56+
}
57+
else{
58+
Toast.makeText(MainActivity.this, "End", Toast.LENGTH_SHORT).show();
59+
}
60+
}
61+
});
62+
button.setOnClickListener(new View.OnClickListener() {
63+
@Override
64+
public void onClick(View view) {
65+
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
66+
}
67+
});
68+
button.isVibrationEnabled(true);
4669
}
4770

4871
@Override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
app:navGraph="@navigation/nav_graph" />
2121

2222
<cls.android.button.Button
23+
android:id="@+id/button"
2324
android:layout_margin="@dimen/margin_regular"
2425
android:layout_width="match_parent"
2526
android:layout_height="wrap_content"

0 commit comments

Comments
 (0)