Advertisement
Guest User

Monodroid TextViewWithUrls

a guest
Apr 24th, 2013
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. using Android.App;
  8. using Android.Content;
  9. using Android.OS;
  10. using Android.Runtime;
  11. using Android.Views;
  12. using Android.Widget;
  13. using Android.Util;
  14. using Android.Text;
  15. using Android.Text.Method;
  16. using Android.Text.Style;
  17.  
  18. namespace Android.TweetBox.Views
  19. {
  20.     public class TextViewWithUrls : TextView
  21.     {
  22.         public bool DontConsumeNonUrlClicks = true;
  23.         public bool LinkHit;
  24.  
  25.         public TextViewWithUrls (Context context) : base(context)
  26.         {
  27.         }
  28.        
  29.         public TextViewWithUrls (Context context, IAttributeSet attrs) : base(context, attrs)
  30.         {
  31.         }
  32.        
  33.         public TextViewWithUrls (Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
  34.         {
  35.         }
  36.  
  37.         public override bool OnTouchEvent (MotionEvent e)
  38.         {
  39.             LinkHit = false;
  40.             bool res = base.OnTouchEvent (e);
  41.            
  42.             if (DontConsumeNonUrlClicks)
  43.                 return LinkHit;
  44.             return res;
  45.         }
  46.     }
  47.  
  48.     public class LocalLinkMovementMethod : LinkMovementMethod
  49.     {
  50.         public static LocalLinkMovementMethod sInstance;
  51.        
  52.        
  53.         public static LocalLinkMovementMethod GetInstance ()
  54.         {
  55.             if (sInstance == null)
  56.                 sInstance = new LocalLinkMovementMethod ();
  57.            
  58.             return sInstance;
  59.         }
  60.  
  61.         public override bool OnTouchEvent (TextView widget, Android.Text.ISpannable buffer, MotionEvent e)
  62.         {
  63.             MotionEventActions action = e.Action;
  64.  
  65.             if (action == MotionEventActions.Up || action == MotionEventActions.Down) {
  66.                 int x = (int)e.GetX ();
  67.                 int y = (int)e.GetY ();
  68.  
  69.                 x -= widget.PaddingLeft;
  70.                 y -= widget.PaddingTop;
  71.                
  72.                 x += widget.ScrollX;
  73.                 y += widget.ScrollY;
  74.                
  75.                 Layout layout = widget.Layout;
  76.                 int line = layout.GetLineForVertical (y);
  77.                 int off = layout.GetOffsetForHorizontal (line, x);
  78.                
  79.                 Object[] link = buffer.GetSpans (off, off, Java.Lang.Class.FromType (typeof(ClickableSpan)));
  80.  
  81.                 if (link.Length > 0) {
  82.                     Console.WriteLine ("ClickableSpan");
  83.                     if (action == MotionEventActions.Up) {
  84.                         ((ClickableSpan)link [0]).OnClick (widget);
  85.                     } else if (action == MotionEventActions.Down) {
  86.                         Selection.SetSelection (buffer, buffer.GetSpanStart ((ClickableSpan)link [0]), buffer.GetSpanEnd ((ClickableSpan)link [0]));
  87.                     }
  88.                    
  89.                     if (widget.GetType ().Equals (typeof(TextViewWithUrls))) {
  90.                         ((TextViewWithUrls)widget).LinkHit = true;
  91.                     }
  92.                     return true;
  93.                 } else {
  94.                     Selection.RemoveSelection (buffer);
  95.                     base.OnTouchEvent (widget, buffer, e);
  96.                     return false;
  97.                 }
  98.             }
  99.             return base.OnTouchEvent (widget, buffer, e);
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement