Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <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=".MainActivity" >
- <ImageView
- android:id="@+id/jokeIcon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:contentDescription="@string/app_name"
- />
- <ScrollView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_below="@+id/jokeIcon" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/allJokesTxt"
- style="?android:textAppearanceMedium"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:lineSpacingMultiplier="1.2"
- android:padding="16dp" />
- </LinearLayout>
- </ScrollView>
- </RelativeLayout>
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- x1 = event.getX();
- break;
- case MotionEvent.ACTION_UP:
- x2 = event.getX();
- float deltaX = x2 - x1;
- if (Math.abs(deltaX) > MIN_DISTANCE) {
- changeText();
- } else {
- // consider as something else - a screen tap for example
- }
- break;
- }
- return super.onTouchEvent(event);
- }
- private void changeText(){
- // Left to Right swipe action
- if (x2 > x1) {
- if (jokesCounter > 0) {
- --jokesCounter;
- currentJoke.setVisibility(View.VISIBLE);
- currentJoke.setText(jokesCollector.get(jokesCounter));
- } else {
- Context context = getApplicationContext();
- CharSequence text = "xxx";
- int duration = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
- }
- }
- // Right to left swipe action
- else {
- if (jokesLengthCounter >= jokesCounter) {
- ++jokesCounter;
- currentJoke.setVisibility(View.VISIBLE);
- currentJoke.setText(jokesCollector.get(jokesCounter));
- } else {
- Context context = getApplicationContext();
- CharSequence text = "xxx";
- int duration = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement