Search in sources :

Example 91 with StyleSpan

use of android.text.style.StyleSpan in project platform_frameworks_base by android.

the class ProgressDialog method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    TypedArray a = mContext.obtainStyledAttributes(null, com.android.internal.R.styleable.AlertDialog, com.android.internal.R.attr.alertDialogStyle, 0);
    if (mProgressStyle == STYLE_HORIZONTAL) {
        /* Use a separate handler to update the text views as they
             * must be updated on the same thread that created them.
             */
        mViewUpdateHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                /* Update the number and percent */
                int progress = mProgress.getProgress();
                int max = mProgress.getMax();
                if (mProgressNumberFormat != null) {
                    String format = mProgressNumberFormat;
                    mProgressNumber.setText(String.format(format, progress, max));
                } else {
                    mProgressNumber.setText("");
                }
                if (mProgressPercentFormat != null) {
                    double percent = (double) progress / (double) max;
                    SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
                    tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mProgressPercent.setText(tmp);
                } else {
                    mProgressPercent.setText("");
                }
            }
        };
        View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout, R.layout.alert_dialog_progress), null);
        mProgress = (ProgressBar) view.findViewById(R.id.progress);
        mProgressNumber = (TextView) view.findViewById(R.id.progress_number);
        mProgressPercent = (TextView) view.findViewById(R.id.progress_percent);
        setView(view);
    } else {
        View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_progressLayout, R.layout.progress_dialog), null);
        mProgress = (ProgressBar) view.findViewById(R.id.progress);
        mMessageView = (TextView) view.findViewById(R.id.message);
        setView(view);
    }
    a.recycle();
    if (mMax > 0) {
        setMax(mMax);
    }
    if (mProgressVal > 0) {
        setProgress(mProgressVal);
    }
    if (mSecondaryProgressVal > 0) {
        setSecondaryProgress(mSecondaryProgressVal);
    }
    if (mIncrementBy > 0) {
        incrementProgressBy(mIncrementBy);
    }
    if (mIncrementSecondaryBy > 0) {
        incrementSecondaryProgressBy(mIncrementSecondaryBy);
    }
    if (mProgressDrawable != null) {
        setProgressDrawable(mProgressDrawable);
    }
    if (mIndeterminateDrawable != null) {
        setIndeterminateDrawable(mIndeterminateDrawable);
    }
    if (mMessage != null) {
        setMessage(mMessage);
    }
    setIndeterminate(mIndeterminate);
    onProgressChanged();
    super.onCreate(savedInstanceState);
}
Also used : SpannableString(android.text.SpannableString) Message(android.os.Message) LayoutInflater(android.view.LayoutInflater) TypedArray(android.content.res.TypedArray) StyleSpan(android.text.style.StyleSpan) Handler(android.os.Handler) SpannableString(android.text.SpannableString) TextView(android.widget.TextView) View(android.view.View)

Example 92 with StyleSpan

use of android.text.style.StyleSpan in project platform_frameworks_base by android.

the class HtmlToSpannedConverter method endHeading.

private static void endHeading(Editable text) {
    // RelativeSizeSpan and StyleSpan are CharacterStyles
    // Their ranges should not include the newlines at the end
    Heading h = getLast(text, Heading.class);
    if (h != null) {
        setSpanFromMark(text, h, new RelativeSizeSpan(HEADING_SIZES[h.mLevel]), new StyleSpan(Typeface.BOLD));
    }
    endBlockElement(text);
}
Also used : StyleSpan(android.text.style.StyleSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan)

Example 93 with StyleSpan

use of android.text.style.StyleSpan in project platform_frameworks_base by android.

the class HtmlToSpannedConverter method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    Application application = ActivityThread.currentApplication();
                    sizeDip /= application.getResources().getDisplayMetrics().density;
                }
                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }
        withinStyle(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) Application(android.app.Application) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 94 with StyleSpan

use of android.text.style.StyleSpan in project Android-Week-View by alamkanak.

the class WeekView method drawEventTitle.

/**
     * Draw the name of the event on top of the event rectangle.
     * @param event The event of which the title (and location) should be drawn.
     * @param rect The rectangle on which the text is to be drawn.
     * @param canvas The canvas to draw upon.
     * @param originalTop The original top position of the rectangle. The rectangle may have some of its portion outside of the visible area.
     * @param originalLeft The original left position of the rectangle. The rectangle may have some of its portion outside of the visible area.
     */
private void drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, float originalTop, float originalLeft) {
    if (rect.right - rect.left - mEventPadding * 2 < 0)
        return;
    if (rect.bottom - rect.top - mEventPadding * 2 < 0)
        return;
    // Prepare the name of the event.
    SpannableStringBuilder bob = new SpannableStringBuilder();
    if (event.getName() != null) {
        bob.append(event.getName());
        bob.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, bob.length(), 0);
        bob.append(' ');
    }
    // Prepare the location of the event.
    if (event.getLocation() != null) {
        bob.append(event.getLocation());
    }
    int availableHeight = (int) (rect.bottom - originalTop - mEventPadding * 2);
    int availableWidth = (int) (rect.right - originalLeft - mEventPadding * 2);
    // Get text dimensions.
    StaticLayout textLayout = new StaticLayout(bob, mEventTextPaint, availableWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    int lineHeight = textLayout.getHeight() / textLayout.getLineCount();
    if (availableHeight >= lineHeight) {
        // Calculate available number of line counts.
        int availableLineCount = availableHeight / lineHeight;
        do {
            // Ellipsize text to fit into event rect.
            textLayout = new StaticLayout(TextUtils.ellipsize(bob, mEventTextPaint, availableLineCount * availableWidth, TextUtils.TruncateAt.END), mEventTextPaint, (int) (rect.right - originalLeft - mEventPadding * 2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            // Reduce line count.
            availableLineCount--;
        // Repeat until text is short enough.
        } while (textLayout.getHeight() > availableHeight);
        // Draw text.
        canvas.save();
        canvas.translate(originalLeft + mEventPadding, originalTop + mEventPadding);
        textLayout.draw(canvas);
        canvas.restore();
    }
}
Also used : StyleSpan(android.text.style.StyleSpan) StaticLayout(android.text.StaticLayout) SpannableStringBuilder(android.text.SpannableStringBuilder) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 95 with StyleSpan

use of android.text.style.StyleSpan in project Signal-Android by WhisperSystems.

the class FromTextView method setText.

public void setText(Recipients recipients, boolean read) {
    int[] attributes = new int[] { R.attr.conversation_list_item_count_color };
    TypedArray colors = getContext().obtainStyledAttributes(attributes);
    String fromString = recipients.toShortString();
    int typeface;
    if (!read) {
        typeface = Typeface.BOLD;
    } else {
        typeface = Typeface.NORMAL;
    }
    SpannableStringBuilder builder = new SpannableStringBuilder(fromString);
    builder.setSpan(new StyleSpan(typeface), 0, builder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    colors.recycle();
    setText(builder);
    if (recipients.isBlocked())
        setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_block_grey600_18dp, 0, 0, 0);
    else if (recipients.isMuted())
        setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_volume_off_grey600_18dp, 0, 0, 0);
    else
        setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
Also used : TypedArray(android.content.res.TypedArray) StyleSpan(android.text.style.StyleSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

StyleSpan (android.text.style.StyleSpan)143 SpannableString (android.text.SpannableString)56 SpannableStringBuilder (android.text.SpannableStringBuilder)34 RelativeSizeSpan (android.text.style.RelativeSizeSpan)32 ForegroundColorSpan (android.text.style.ForegroundColorSpan)25 View (android.view.View)18 TextView (android.widget.TextView)17 UnderlineSpan (android.text.style.UnderlineSpan)16 Spannable (android.text.Spannable)14 StrikethroughSpan (android.text.style.StrikethroughSpan)13 TypefaceSpan (android.text.style.TypefaceSpan)12 TypedArray (android.content.res.TypedArray)10 TextPaint (android.text.TextPaint)10 SubscriptSpan (android.text.style.SubscriptSpan)10 SuperscriptSpan (android.text.style.SuperscriptSpan)10 Handler (android.os.Handler)9 Message (android.os.Message)9 URLSpan (android.text.style.URLSpan)8 JustifiedSpan (com.bluejamesbond.text.style.JustifiedSpan)8 ArticleBuilder (com.bluejamesbond.text.util.ArticleBuilder)8