Example usage for android.text.style ClickableSpan ClickableSpan

List of usage examples for android.text.style ClickableSpan ClickableSpan

Introduction

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

Prototype

ClickableSpan

Source Link

Usage

From source file:Main.java

public static void makeAboutSpannable(SpannableStringBuilder span, String str_link, String replace,
        final Runnable on_click) {
    Pattern pattern = Pattern.compile(str_link);
    Matcher matcher = pattern.matcher(span);
    ForegroundColorSpan color_theme = new ForegroundColorSpan(Color.parseColor("#53b7bb"));
    if (matcher.find()) {
        span.setSpan(new ClickableSpan() {
            @Override/*from   ww w  . ja  v  a2  s  .  c o m*/
            public void onClick(View widget) {
                if (on_click != null)
                    on_click.run();
            }
        }, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(color_theme, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (replace != null)
            span.replace(matcher.start(), matcher.end(), replace);
    }
}

From source file:Main.java

public static void setClickable(final TextView textView) {
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    Spannable sp = (Spannable) textView.getText();
    ImageSpan[] images = sp.getSpans(0, textView.getText().length(), ImageSpan.class);

    for (ImageSpan span : images) {
        final String image_src = span.getSource();
        final int start = sp.getSpanStart(span);
        final int end = sp.getSpanEnd(span);

        ClickableSpan click_span = new ClickableSpan() {
            @Override//from   ww  w .  j  ava  2s  .co m
            public void onClick(View widget) {
                String[] strs = image_src.split("/");
                String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LilyClient/"
                        + strs[strs.length - 2] + "-" + strs[strs.length - 1];

                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setType("image/*");
                intent.setDataAndType(Uri.fromFile(new File(filePath)), "image/*");
                textView.getContext().startActivity(intent);

            }
        };
        ClickableSpan[] click_spans = sp.getSpans(start, end, ClickableSpan.class);
        if (click_spans.length != 0) {
            for (ClickableSpan c_span : click_spans) {
                sp.removeSpan(c_span);
            }
        }
        sp.setSpan(click_span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

From source file:Main.java

/**
 * Make UI TextView a html link./*from w ww  .  ja va2 s .  c  om*/
 * 
 * @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:de.dev.eth0.rssreader.app.util.html.rewriter.impl.ImageRewriterImpl.java

@Override
public Object rewrite(final String url) {
    Timber.d("Setup Overlay for Image %s", url);
    return new ClickableSpan() {
        @Override/*w w w  . j  ava2 s.c  o m*/
        public void onClick(View view) {
            if (context instanceof FragmentActivity) {
                FragmentActivity activity = (FragmentActivity) context;
                ImageOverlayDialog dialog = new ImageOverlayDialog();
                Bundle bundle = new Bundle();
                bundle.putString(FeedEntryTable.COLUMN_LINK, url);
                dialog.setArguments(bundle);
                dialog.show(activity.getSupportFragmentManager(), dialog.getClass().getSimpleName());
            }
        }
    };
}

From source file:com.andrada.sitracker.ui.fragment.AboutDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    PackageManager pm = getActivity().getPackageManager();
    String packageName = getActivity().getPackageName();
    String versionName;/*  w  w w  . j a  v  a  2 s. co m*/
    try {
        PackageInfo info = pm.getPackageInfo(packageName, 0);
        versionName = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        versionName = VERSION_UNAVAILABLE;
    }

    SpannableStringBuilder aboutBody = new SpannableStringBuilder();
    SpannableString licensesLink = new SpannableString(getString(R.string.about_licenses));
    licensesLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showOpenSourceLicenses(getActivity());
        }
    }, 0, licensesLink.length(), 0);
    SpannableString whatsNewLink = new SpannableString(getString(R.string.whats_new));
    whatsNewLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showWhatsNew(getActivity());
        }
    }, 0, whatsNewLink.length(), 0);
    aboutBody.append(licensesLink);
    aboutBody.append("\n\n");
    aboutBody.append(whatsNewLink);

    AboutDialogView aboutBodyView = AboutDialogView_.build(getActivity());
    aboutBodyView.bindData(getString(R.string.app_version_format, versionName), aboutBody);

    return new AlertDialog.Builder(getActivity()).setTitle(R.string.action_about).setView(aboutBodyView)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            }).create();
}

From source file:com.keylesspalace.tusky.util.LinkHelper.java

/**
 * Finds links, mentions, and hashtags in a piece of text and makes them clickable, associating
 * them with callbacks to notify when they're clicked.
 *
 * @param view the returned text will be put in
 * @param content containing text with mentions, links, or hashtags
 * @param mentions any '@' mentions which are known to be in the content
 * @param listener to notify about particular spans that are clicked
 *///w  w w.j a  va  2 s . co m
public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions,
        final LinkListener listener) {

    SpannableStringBuilder builder = new SpannableStringBuilder(content);
    URLSpan[] urlSpans = content.getSpans(0, content.length(), URLSpan.class);
    for (URLSpan span : urlSpans) {
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        int flags = builder.getSpanFlags(span);
        CharSequence text = builder.subSequence(start, end);
        if (text.charAt(0) == '#') {
            final String tag = text.subSequence(1, text.length()).toString();
            ClickableSpan newSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    listener.onViewTag(tag);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setUnderlineText(false);
                }
            };
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        } else if (text.charAt(0) == '@' && mentions != null && mentions.length > 0) {
            String accountUsername = text.subSequence(1, text.length()).toString();
            /* There may be multiple matches for users on different instances with the same
             * username. If a match has the same domain we know it's for sure the same, but if
             * that can't be found then just go with whichever one matched last. */
            String id = null;
            for (Status.Mention mention : mentions) {
                if (mention.localUsername.equalsIgnoreCase(accountUsername)) {
                    id = mention.id;
                    if (mention.url.contains(getDomain(span.getURL()))) {
                        break;
                    }
                }
            }
            if (id != null) {
                final String accountId = id;
                ClickableSpan newSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        listener.onViewAccount(accountId);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setUnderlineText(false);
                    }
                };
                builder.removeSpan(span);
                builder.setSpan(newSpan, start, end, flags);
            }
        } else {
            ClickableSpan newSpan = new CustomURLSpan(span.getURL());
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        }
    }
    view.setText(builder);
    view.setLinksClickable(true);
    view.setMovementMethod(LinkMovementMethod.getInstance());
}

From source file:com.yek.keyboard.ui.settings.AboutAnySoftKeyboardFragment.java

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    TextView additionalSoftware = (TextView) getView().findViewById(R.id.about_legal_stuff_link);
    SpannableStringBuilder sb = new SpannableStringBuilder(additionalSoftware.getText());
    sb.clearSpans();//removing any previously (from instance-state) set click spans.
    sb.setSpan(new ClickableSpan() {
        @Override//from w w w.j av a2 s.  c  om
        public void onClick(View widget) {
            FragmentChauffeurActivity activity = (FragmentChauffeurActivity) getActivity();
            activity.addFragmentToUi(new AdditionalSoftwareLicensesFragment(),
                    TransitionExperiences.DEEPER_EXPERIENCE_TRANSITION);
        }
    }, 0, additionalSoftware.getText().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    additionalSoftware.setMovementMethod(LinkMovementMethod.getInstance());
    additionalSoftware.setText(sb);
}

From source file:cw.kop.autobackground.tutorial.FinishFragment.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.tutorial_finish_fragment, container, false);
    int colorFilterInt = AppSettings.getColorFilterInt(appContext);

    TextView titleText = (TextView) view.findViewById(R.id.title_text);
    titleText.setTextColor(colorFilterInt);
    titleText.setText("That's it.");

    TextView tutorialText = (TextView) view.findViewById(R.id.tutorial_text);
    tutorialText.setTextColor(colorFilterInt);
    tutorialText.setText("Now you're ready to use AutoBackground. I'd suggest adding a new source first "
            + "and then hitting download." + "\n" + "\n"
            + "If you have any questions, concerns, suggestions, or whatever else, feel free to "
            + "email me at ");
    SpannableString emailString = new SpannableString("chiuwinson@gmail.com");
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override/*from   w w  w.ja  va  2s .  c  o  m*/
        public void onClick(View widget) {
            Log.i(TAG, "Clicked");
            Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
                    Uri.fromParts("mailto", "chiuwinson@gmail.com", null));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "AutoBackground Feedback");
            startActivity(Intent.createChooser(emailIntent, "Send email"));
        }
    };
    emailString.setSpan(clickableSpan, 0, emailString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tutorialText.append(emailString);
    tutorialText.append(".");
    tutorialText.setMovementMethod(LinkMovementMethod.getInstance());

    return view;
}

From source file:com.anysoftkeyboard.ui.settings.AboutAnySoftKeyboardFragment.java

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    TextView additionalSoftware = (TextView) getView().findViewById(R.id.about_legal_stuff_link);
    SpannableStringBuilder sb = new SpannableStringBuilder(additionalSoftware.getText());
    sb.clearSpans();//removing any previously (from instance-state) set click spans.
    sb.setSpan(new ClickableSpan() {
        @Override//from w ww . ja va2 s . co  m
        public void onClick(View widget) {
            FragmentChauffeurActivity activity = (FragmentChauffeurActivity) getActivity();
            activity.addFragmentToUi(new AdditionalSoftwareLicensesFragment(),
                    FragmentChauffeurActivity.FragmentUiContext.DeeperExperience);
        }
    }, 0, additionalSoftware.getText().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    additionalSoftware.setMovementMethod(LinkMovementMethod.getInstance());
    additionalSoftware.setText(sb);
}

From source file:com.hpush.app.fragments.AboutDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Get app version
    PackageManager pm = getActivity().getPackageManager();
    String packageName = getActivity().getPackageName();
    String versionName;/*w  ww.  j  a v a2 s .c  om*/
    try {
        PackageInfo info = pm.getPackageInfo(packageName, 0);
        versionName = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        versionName = VERSION_UNAVAILABLE;
    }

    // About.
    SpannableStringBuilder aboutBody = new SpannableStringBuilder();
    aboutBody.append(
            Html.fromHtml(getString(R.string.about_body, getString(R.string.application_name), versionName)));

    // Licenses.
    SpannableString licensesLink = new SpannableString(getString(R.string.about_licenses));
    licensesLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showOpenSourceLicenses(getActivity());
        }
    }, 0, licensesLink.length(), 0);
    aboutBody.append("\n\n");
    aboutBody.append(licensesLink);

    // End User License Agreement.
    SpannableString eulaLink = new SpannableString(getString(R.string.about_eula));
    eulaLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showEula(getActivity());
        }
    }, 0, eulaLink.length(), 0);
    aboutBody.append("\n\n");
    aboutBody.append(eulaLink);

    // Show "About" dialog.
    LayoutInflater layoutInflater = (LayoutInflater) getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView aboutBodyView = (TextView) layoutInflater.inflate(R.layout.fragment_dialog_about, null);
    aboutBodyView.setText(aboutBody);
    aboutBodyView.setMovementMethod(new LinkMovementMethod());

    return new AlertDialog.Builder(getActivity()).setTitle(R.string.lbl_about).setView(aboutBodyView)
            .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            }).create();
}