Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10.  
  11. <ImageView
  12. android:id="@+id/jokeIcon"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentTop="true"
  16. android:layout_centerHorizontal="true"
  17. android:contentDescription="@string/app_name"
  18. />
  19.  
  20. <ScrollView
  21. android:layout_width="fill_parent"
  22. android:layout_height="fill_parent"
  23. android:layout_below="@+id/jokeIcon" >
  24.  
  25. <LinearLayout
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:orientation="vertical" >
  29.  
  30. <TextView
  31. android:id="@+id/allJokesTxt"
  32. style="?android:textAppearanceMedium"
  33. android:layout_width="fill_parent"
  34. android:layout_height="fill_parent"
  35. android:gravity="center"
  36. android:lineSpacingMultiplier="1.2"
  37. android:padding="16dp" />
  38.  
  39. </LinearLayout>
  40. </ScrollView>
  41.  
  42. </RelativeLayout>
  43.  
  44. public boolean onTouchEvent(MotionEvent event) {
  45.  
  46. switch (event.getAction()) {
  47. case MotionEvent.ACTION_DOWN:
  48. x1 = event.getX();
  49. break;
  50. case MotionEvent.ACTION_UP:
  51. x2 = event.getX();
  52. float deltaX = x2 - x1;
  53.  
  54. if (Math.abs(deltaX) > MIN_DISTANCE) {
  55. changeText();
  56.  
  57. } else {
  58. // consider as something else - a screen tap for example
  59. }
  60. break;
  61. }
  62. return super.onTouchEvent(event);
  63. }
  64.  
  65.  
  66. private void changeText(){
  67. // Left to Right swipe action
  68. if (x2 > x1) {
  69. if (jokesCounter > 0) {
  70. --jokesCounter;
  71. currentJoke.setVisibility(View.VISIBLE);
  72. currentJoke.setText(jokesCollector.get(jokesCounter));
  73. } else {
  74. Context context = getApplicationContext();
  75. CharSequence text = "xxx";
  76. int duration = Toast.LENGTH_SHORT;
  77.  
  78. Toast toast = Toast.makeText(context, text, duration);
  79. toast.show();
  80. }
  81.  
  82. }
  83.  
  84. // Right to left swipe action
  85. else {
  86. if (jokesLengthCounter >= jokesCounter) {
  87. ++jokesCounter;
  88.  
  89. currentJoke.setVisibility(View.VISIBLE);
  90. currentJoke.setText(jokesCollector.get(jokesCounter));
  91. } else {
  92. Context context = getApplicationContext();
  93. CharSequence text = "xxx";
  94. int duration = Toast.LENGTH_SHORT;
  95.  
  96. Toast toast = Toast.makeText(context, text, duration);
  97. toast.show();
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement