Example usage for android.text.style URLSpan getURL

List of usage examples for android.text.style URLSpan getURL

Introduction

In this page you can find the example usage for android.text.style URLSpan getURL.

Prototype

public String getURL() 

Source Link

Document

Get the url string for this span.

Usage

From source file:com.microsoft.mimickeralarm.utilities.GeneralUtilities.java

public static void stripUnderlines(TextView textView) {
    Spannable s = (Spannable) textView.getText();
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);//from www.  j  a va 2 s  .com
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

From source file:Main.java

/**
 * Make UI TextView a html link./*from  w  ww.j a v a  2  s  .  com*/
 * 
 * @param context the context
 * @param textView the text view
 * @param html the html containing link info
 */
public static void makeTextViewAHTMLLink(final Context context, TextView textView, String html) {
    textView.setLinksClickable(true);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = spannableStringBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (final URLSpan urlSpan : urls) {
        int start = spannableStringBuilder.getSpanStart(urlSpan);
        int end = spannableStringBuilder.getSpanEnd(urlSpan);
        int flags = spannableStringBuilder.getSpanFlags(urlSpan);
        ClickableSpan clickable = new ClickableSpan() {
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlSpan.getURL()));
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint textPaint) {
                super.updateDrawState(textPaint);
                textPaint.setUnderlineText(false);
            }
        };
        spannableStringBuilder.removeSpan(urlSpan);
        spannableStringBuilder.setSpan(clickable, start, end, flags);
    }
    textView.setText(spannableStringBuilder);
}

From source file:com.silentcircle.common.util.ViewUtil.java

public static Intent createIntentForLinks(TextView view) {

    CharSequence text = view.getText();

    if (text instanceof Spannable) {

        Spannable stext = (Spannable) text;
        URLSpan[] spans = stext.getSpans(0, stext.length(), URLSpan.class);

        if (spans != null && spans.length > 0) {
            ChooserBuilder chooser = new ChooserBuilder(view.getContext());
            chooser.label(R.string.view);
            for (URLSpan span : spans) {
                String url = span.getURL();
                CharSequence label = stext.subSequence(stext.getSpanStart(span), stext.getSpanEnd(span));
                chooser.intent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), label);
            }/*from ww w .j  ava 2s.  co  m*/
            return chooser.build();

        }
    }
    return null;
}

From source file:org.catnut.util.CatnutUtils.java

/**
 * textview//from w  w w .j  av a2  s  .  com
 *
 * @param textView
 */
public static void removeLinkUnderline(TextView textView) {
    Spannable s = Spannable.Factory.getInstance().newSpannable(textView.getText());
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);
        span = new TweetURLSpan(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

From source file:com.android.screenspeak.menurules.RuleSpannables.java

@Override
public List<ContextMenuItem> getMenuItemsForNode(ScreenSpeakService service,
        ContextMenuItemBuilder menuItemBuilder, AccessibilityNodeInfoCompat node) {
    final SpannableString spannable = (SpannableString) node.getText();
    final URLSpan[] urlSpans = spannable.getSpans(0, spannable.length(), URLSpan.class);
    final LinkedList<ContextMenuItem> result = new LinkedList<>();

    if ((urlSpans == null) || (urlSpans.length == 0)) {
        return result;
    }//  w ww.  j  a  va2 s .c  o m

    for (int i = 0; i < urlSpans.length; i++) {
        final URLSpan urlSpan = urlSpans[i];
        final String url = urlSpan.getURL();
        final int start = spannable.getSpanStart(urlSpan);
        final int end = spannable.getSpanEnd(urlSpan);
        final CharSequence label = spannable.subSequence(start, end);
        if (TextUtils.isEmpty(url) || TextUtils.isEmpty(label)) {
            continue;
        }

        final Uri uri = Uri.parse(url);
        if (uri.isRelative()) {
            // Generally, only absolute URIs are resolvable to an activity
            continue;
        }

        final ContextMenuItem item = menuItemBuilder.createMenuItem(service, Menu.NONE, i, Menu.NONE, label);
        item.setOnMenuItemClickListener(new SpannableMenuClickListener(service, uri));
        result.add(item);
    }

    return result;
}

From source file:com.android.talkback.menurules.RuleSpannables.java

@Override
public List<ContextMenuItem> getMenuItemsForNode(TalkBackService service,
        ContextMenuItemBuilder menuItemBuilder, AccessibilityNodeInfoCompat node) {
    final LinkedList<ContextMenuItem> result = new LinkedList<>();

    final SpannableString spannable = getStringWithUrlSpan(node);
    if (spannable == null) {
        return result;
    }//from  w w  w  .jav a  2 s.co  m

    final URLSpan[] urlSpans = spannable.getSpans(0, spannable.length(), URLSpan.class);
    if ((urlSpans == null) || (urlSpans.length == 0)) {
        return result;
    }

    for (int i = 0; i < urlSpans.length; i++) {
        final URLSpan urlSpan = urlSpans[i];
        final String url = urlSpan.getURL();
        final int start = spannable.getSpanStart(urlSpan);
        final int end = spannable.getSpanEnd(urlSpan);
        final CharSequence label = spannable.subSequence(start, end);
        if (TextUtils.isEmpty(url) || TextUtils.isEmpty(label)) {
            continue;
        }

        final Uri uri = Uri.parse(url);
        if (uri.isRelative()) {
            // Generally, only absolute URIs are resolvable to an activity
            continue;
        }

        final ContextMenuItem item = menuItemBuilder.createMenuItem(service, Menu.NONE, i, Menu.NONE, label);
        item.setOnMenuItemClickListener(new SpannableMenuClickListener(service, uri));
        result.add(item);
    }

    return result;
}

From source file:com.example.activity.ProfileActivity.java

private void setPlayer() {
    ImageLoader.getInstance().displayImage(playerModel.player.avatar_url, head, BeeFrameworkApp.options_head);
    name.setText(playerModel.player.name);
    location.setText(playerModel.player.location);

    shots_count.setText(playerModel.player.shots_count + "");
    likes_count.setText(playerModel.player.likes_count + "");
    following_count.setText(playerModel.player.following_count + "");
    followers_count.setText(playerModel.player.followers_count + "");

    net.setText(playerModel.player.website_url);

    CharSequence text = net.getText();
    if (text instanceof Spannable) {
        int end = text.length();
        Spannable sp = (Spannable) net.getText();
        URLSpan[] spans = sp.getSpans(0, end, URLSpan.class);
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.clearSpans();// should clear old spans
        for (URLSpan span : spans) {
            JayceSpan mySpan = new JayceSpan(span.getURL());
            style.setSpan(mySpan, sp.getSpanStart(span), sp.getSpanEnd(span),
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }/*w  w w  .j  a  v  a2  s  . c o  m*/
        net.setText(style);
    }
}

From source file:com.zagapps.eventblank.fragments.MoreCreditsDetailFragment.java

@Nullable
@Override//  w ww  .ja  v a2 s . c  o  m
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.more_credits_fragment, container, false);
    ButterKnife.bind(this, view);
    setupToolbar();

    mainColor = ModelManager.get(getActivity()).getEvent().getMainColor();
    view.findViewById(R.id.more_detail_header).setBackgroundColor(mainColor);

    Utils.SpanConverter<URLSpan, CharacterStyle> spanConverter = new Utils.SpanConverter<URLSpan, CharacterStyle>() {
        @Override
        public CharacterStyle convert(final URLSpan span) {
            return (new ClickableLinkSpan(ModelManager.get(getActivity()).getEvent().getMainColor(), false) {
                @Override
                public void onClick(View widget) {
                    String url = span.getURL();
                    if (url.contains("mailto:")) {
                        //hape si email, 7 i bjen pozita ku ia nis emaila, pasi "mailto:" osht ne positions 0-6
                        Utils.goToMailClient(getActivity(), url.substring(7));
                    } else {
                        Utils.openWebsiteInsideApp(url, getFragmentManager());
                    }
                }
            });
        }
    };

    for (int i = 0; i < mContents.size(); i++) {
        HtmlTextView textView = mContents.get(i);
        textView.setHtmlFromString(generateCredits(i), new HtmlTextView.LocalImageGetter());
        textView.setText(Utils.replaceAllSpans((Spanned) textView.getText(), URLSpan.class, spanConverter));
    }

    return view;
}

From source file:com.slx.funstream.adapters.ChatAdapter.java

private void linksToImages(Spannable spannable, TextView textView) {
    //      Matcher matcher = Linkify.WEB_URLS.matcher(spannable);
    //      URLSpan[] spans = textView.getUrls();
    URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
    Log.d(TAG, "URLSpan[] size=" + spans.length);
    for (URLSpan span : spans) {
        String url = span.getURL();
        Log.d(TAG, "START: " + spannable.getSpanStart(span) + "END: " + spannable.getSpanEnd(span) + " " + url);
        ImageTarget t = new ImageTarget(spannable.getSpanStart(span), spannable.getSpanEnd(span), textView,
                spannable);// www.  j a va 2s .c o m
        picasso.load(url).resize(300, 300).centerInside().onlyScaleDown().into(t);
        targets.add(t);
    }
}

From source file:com.lloydtorres.stately.helpers.SparkleHelper.java

/**
 * Stylify text view to primary colour and no underline
 * @param c App context//from w w  w.j  av  a  2  s.c  o m
 * @param t TextView
 */
public static void styleLinkifiedTextView(Context c, TextView t) {
    // Get individual spans and replace them with clickable ones.
    Spannable s = new SpannableString(t.getText());
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);
        span = new URLSpanNoUnderline(c, span.getURL());
        s.setSpan(span, start, end, 0);
    }

    t.setText(s);
    // Need to set this to allow for clickable TextView links.
    if (!(t instanceof HtmlTextView)) {
        t.setMovementMethod(LinkMovementMethod.getInstance());
    }
}