Example usage for android.text Html fromHtml

List of usage examples for android.text Html fromHtml

Introduction

In this page you can find the example usage for android.text Html fromHtml.

Prototype

@Deprecated
public static Spanned fromHtml(String source) 

Source Link

Document

Returns displayable styled text from the provided HTML string with the legacy flags #FROM_HTML_MODE_LEGACY .

Usage

From source file:Main.java

private static void createLink(View v, int id, String html) {
    TextView textView = (TextView) v.findViewById(id);
    textView.setClickable(true);/* w  w w  . ja v a 2 s.  com*/
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(Html.fromHtml(html));
}

From source file:Main.java

/**
 * Populate the given {@link TextView} with the requested text, formatting
 * through {@link Html#fromHtml(String)} when applicable. Also sets
 * {@link TextView#setMovementMethod} so inline links are handled.
 *//*w  w  w  .j a  va 2s  .  co m*/
public static void setTextMaybeHtml(TextView view, String text) {
    if (TextUtils.isEmpty(text)) {
        view.setText("");
        return;
    }
    if (text.contains("<") && text.contains(">")) {
        view.setText(Html.fromHtml(text));
        view.setMovementMethod(LinkMovementMethod.getInstance());
    } else {
        view.setText(text);
    }
}

From source file:Main.java

private static CharSequence extract(StringBuilder data, boolean isSpanned) {
    CharSequence len = cut(data, 0, NUM_LEN);
    int l = intFromHexString(len);
    CharSequence ret = null;//w ww .ja  v a  2 s .c o  m
    switch (l) {
    case FLAG_NULL:
        ret = null;
        break;
    case FLAG_SPANNED:
        ret = extract(data, true);
        break;
    default:
        ret = cut(data, 0, l);
        if (isSpanned) {
            ret = Html.fromHtml(ret.toString());
        }
    }
    return ret;
}

From source file:Main.java

public static SpannableString linkifyGid(String gid) {
    if (gid == null) {
        return null;
    }/*from   w w  w.j a  va  2  s.  com*/
    SpannableString ret = new SpannableString(gid);
    String name = gid.substring(0, gid.lastIndexOf("@"));
    String authority = gid.substring(gid.lastIndexOf("@") + 1);
    String mainAuth = authority.contains("/") ? authority.substring(0, authority.indexOf("/")) : authority;

    if (auths.containsKey(mainAuth)) {

        String link;
        try {
            if (auths.get(mainAuth).length == 1) {
                link = auths.get(mainAuth)[0];
            } else if (auths.get(mainAuth).length == 2) {
                link = auths.get(mainAuth)[0] + URLEncoder.encode(name, "UTF-8") + auths.get(mainAuth)[1];
            } else {
                return ret;
            }
        } catch (UnsupportedEncodingException e) {
            return ret;
        }

        ret = new SpannableString(Html.fromHtml("<a href=\"" + link + "\">" + gid + "</a>"));

    } else if (mainAuth.matches(".*\\..*")) { // if it contains a dot

        // we try a link to the supposed webpage of this auth
        ret = new SpannableString(Html.fromHtml("<a href=\"http://" + mainAuth + "\">" + gid + "</a>"));

    }

    return ret;
}

From source file:Main.java

/**
 * Make UI TextView a html link.//from   w w  w  .j av  a2 s  .c  o  m
 * 
 * @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:Main.java

/**
 * Populate the given {@link TextView} with the requested text, formatting
 * through {@link Html#fromHtml(String)} when applicable. Also sets
 * {@link TextView#setMovementMethod} so inline links are handled.
 *///from w  w  w .j a  v  a2  s.co m
public static void setTextMaybeHtml(TextView view, String text) {
    if (TextUtils.isEmpty(text)) {
        view.setText("");
        return;
    }
    if ((text.contains("<") && text.contains(">")) || REGEX_HTML_ESCAPE.matcher(text).find()) {
        view.setText(Html.fromHtml(text));
        view.setMovementMethod(LinkMovementMethod.getInstance());
    } else {
        view.setText(text);
    }
}

From source file:Main.java

public static void showMessage(Context _context, String title, String message, int icon,
        DialogInterface.OnClickListener ackHandler) {
    AlertDialog.Builder builder = new AlertDialog.Builder(_context);
    builder.setTitle(title);//from   w w  w .  j a  v a2 s .  c o m
    builder.setMessage(Html.fromHtml(message));
    builder.setCancelable(false);
    builder.setPositiveButton("Acknowledged", ackHandler);
    builder.setIcon(icon);
    boolean show = true;
    if (_context instanceof Activity) {
        Activity activity = (Activity) _context;
        if (activity.isFinishing()) {
            show = false;
        }
    }
    if (show)
        builder.show();
}

From source file:at.bitfire.davdroid.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    setTitle("CardDroid " + Constants.APP_VERSION);

    TextView tv = (TextView) findViewById(R.id.text_info);
    tv.setText(Html.fromHtml(getString(R.string.html_info)));
    tv.setMovementMethod(LinkMovementMethod.getInstance());
}

From source file:com.tigerpenguin.places.deserializer.HtmlStringDeserializer.java

@Override
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {
    String value = jsonParser.getValueAsString();
    value = Html.fromHtml(value).toString();
    return value;
}

From source file:Main.java

/**
 * Returns the bulleted list based on the given {@code lines}.
 * If one of lines starts with {@link BulletListUtil#BAD_FIRST_SYMBOLS}, then such symbol is
 * removed from there (sometimes bad lines are received from the Food2Work).
 * Also if line consists of the upper case words, then this line is used like a header and is
 * underlined./*  www . j a  v a2s .c o m*/
 *
 * @param leadingMargin In pixels, the space between the left edge of the bullet and the left
 *                         edge of the text
 * @param lines List of strings. Each string will be a separate item in the bulleted list
 * @return The bulleted list based on the given {@code lines}
 */
public static CharSequence makeBulletList(List<String> lines, int leadingMargin) {
    List<Spanned> spanned = new ArrayList<>(lines.size());
    for (String line : lines) {
        if (!line.trim().isEmpty()) {
            Spanned spannedLine = Html.fromHtml(removeBadFirstCharacters(line.trim()));
            spanned.add(spannedLine);
        }
    }

    SpannableStringBuilder sb = new SpannableStringBuilder();
    for (int i = 0; i < spanned.size(); i++) {
        CharSequence line = spanned.get(i) + (i < spanned.size() - 1 ? "\n" : "");
        boolean underlineNeeded = isUpperCase(line);
        Spannable spannable = new SpannableString(line);
        if (underlineNeeded) {
            spannable.setSpan(new UnderlineSpan(), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {
            spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(),
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        sb.append(spannable);
    }
    return sb;
}