Guest User

Untitled

a guest
Mar 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import android.text.style.ClickableSpan;
  2. import android.view.View;
  3.  
  4. /**
  5. * Created by nilesh on 22/3/18.
  6. */
  7.  
  8. public abstract class CustomClickableSpan extends ClickableSpan {
  9.  
  10. abstract public void onLongClick(View view);
  11.  
  12. @Override
  13. public void onClick(View view) {
  14.  
  15. }
  16. }
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19. TextView editemail;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26.  
  27. editemail = findViewById(R.id.Amount);
  28. Spannable span = Spannable.Factory.getInstance().newSpannable("Nilesh Rathod");
  29.  
  30. CustomClickableSpan customClickableSpan = new CustomClickableSpan() {
  31. @Override
  32. public void onLongClick(View view) {
  33.  
  34. Toast.makeText(MainActivity.this, "Long Clicked", Toast.LENGTH_SHORT).show();
  35. }
  36. };
  37.  
  38. span.setSpan(customClickableSpan, 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  39. SpannableStringBuilder builder = new SpannableStringBuilder();
  40. builder.append(span);
  41. builder.append(" : " + "Othet text");
  42. editemail.setText(span);
  43. editemail.setMovementMethod(LongClickSpan.getInstance());
  44.  
  45.  
  46. }
  47.  
  48.  
  49. }
  50.  
  51. package com.example.nilesh.testapp;
  52.  
  53. /**
  54. * Created by nilesh on 22/3/18.
  55. */
  56. import android.text.Layout;
  57. import android.text.Selection;
  58. import android.text.Spannable;
  59. import android.text.method.LinkMovementMethod;
  60. import android.text.method.MovementMethod;
  61. import android.view.MotionEvent;
  62. import android.widget.TextView;
  63.  
  64. public class LongClickSpan extends LinkMovementMethod {
  65.  
  66. private Long lastClickTime = 0l;
  67. private int lastX = 0;
  68. private int lastY = 0;
  69. @Override
  70. public boolean onTouchEvent(TextView widget, Spannable buffer,
  71. MotionEvent event) {
  72. int action = event.getAction();
  73.  
  74. if (action == MotionEvent.ACTION_UP ||
  75. action == MotionEvent.ACTION_DOWN) {
  76. int x = (int) event.getX();
  77. int y = (int) event.getY();
  78. lastX = x;
  79. lastY = y;
  80. int deltaX = Math.abs(x-lastX);
  81. int deltaY = Math.abs(y-lastY);
  82.  
  83. x -= widget.getTotalPaddingLeft();
  84. y -= widget.getTotalPaddingTop();
  85.  
  86. x += widget.getScrollX();
  87. y += widget.getScrollY();
  88.  
  89. Layout layout = widget.getLayout();
  90. int line = layout.getLineForVertical(y);
  91. int off = layout.getOffsetForHorizontal(line, x);
  92.  
  93. CustomClickableSpan[] link = buffer.getSpans(off, off, CustomClickableSpan.class);
  94.  
  95. if (link.length != 0) {
  96. if (action == MotionEvent.ACTION_UP) {
  97. if (System.currentTimeMillis() - lastClickTime < 1000)
  98. link[0].onClick(widget);
  99. else if (deltaX < 10 && deltaY < 10)
  100. link[0].onLongClick(widget);
  101. } else if (action == MotionEvent.ACTION_DOWN) {
  102. Selection.setSelection(buffer,
  103. buffer.getSpanStart(link[0]),
  104. buffer.getSpanEnd(link[0]));
  105. lastClickTime = System.currentTimeMillis();
  106. }
  107. return true;
  108. }
  109. }
  110.  
  111. return super.onTouchEvent(widget, buffer, event);
  112. }
  113.  
  114.  
  115. public static MovementMethod getInstance() {
  116. if (sInstance == null)
  117. sInstance = new LongClickSpan();
  118.  
  119. return sInstance;
  120. }
  121.  
  122. private static LongClickSpan sInstance;
  123. }
Add Comment
Please, Sign In to add comment