-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_21-22_gestures.txt
168 lines (135 loc) · 5.47 KB
/
code_21-22_gestures.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
[source code] Android Development Tutorials 21 and 22 - Gestures
***** Tutorial Notes
** 21 – Gestures
Touch Gestures include: Scroll, Fling, Double-Tap, and Swipe.
//Layout//
1. Start with a EmptyActivity and delete Hello World. Add a Large Text with the id buckysMessage. TextView text will change to reflect the state the gesture you perform.
//Java//
1. Get your imports including android.support.v4.view.GestureDetectorCompat
2. Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener
NOTE: Don't change the Activity to ActionBarActivity like Bucky. You will get rendering problems
3. Add variables for TextView and gestureDetector
4. Use Alt-Ins to implement all the required methods (below on Create()
5. In onCreate(), get a ref to TextView and create the gesture object and implement the listener.
6. Set the actions for the call back methods e.g. buckysMessage.setText("onSingleTapConfirmed");
NOTE: Be sure to return true in event overrides to consume the touch event.
** 22 - Running the Gesture App
NOTE: V7. ActionBarActivity is a child of support.v4.FragmentActivity, which is a child of Activity
We need one last statement to check touch events for gesture events.
1. Override Activity onTouchEvent (after Gestures)
2. Add a line to check for gestures i.e. this.gestureDetector.onTouchEvent(event);
Run and test the app.
***** manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenewboston.swiperdiaper">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
***** MainActivity.java
package com.thenewboston.swiperdiaper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private TextView buckysMessage;
private GestureDetectorCompat gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buckysMessage = (TextView)findViewById(R.id.buckysMessage);
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);
}
//////////////// Begin Gestures ////////////////
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
buckysMessage.setText("onSingleTapConfirmed");
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
buckysMessage.setText("onDoubleTap");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
buckysMessage.setText("onDoubleTapEvent");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
buckysMessage.setText("onDown");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
buckysMessage.setText("onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
buckysMessage.setText("onSingleTapUp");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
buckysMessage.setText("onScroll");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
buckysMessage.setText("onLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
buckysMessage.setText("onFling");
return true;
}
//////////////// End Gestures ////////////////
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
***** activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.thenewboston.swiperdiaper.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/buckys_text"
android:id="@+id/buckysMessage"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
***** strings.xml
<resources>
<string name="app_name">SwiperDiaper</string>
<string name="buckys_text">Gestures</string>
</resources>