Search in sources :

Example 76 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project plaid by nickbutcher.

the class ImageSpanTarget method onResourceReady.

@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
    TextView tv = textView.get();
    if (tv != null) {
        BitmapDrawable bitmapDrawable = new BitmapDrawable(tv.getResources(), bitmap);
        // image span doesn't handle scaling so we manually set bounds
        if (bitmap.getWidth() > tv.getWidth()) {
            float aspectRatio = (float) bitmap.getHeight() / (float) bitmap.getWidth();
            bitmapDrawable.setBounds(0, 0, tv.getWidth(), (int) (aspectRatio * tv.getWidth()));
        } else {
            bitmapDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
        }
        ImageSpan span = new ImageSpan(bitmapDrawable);
        // add the image span and remove our marker
        SpannableStringBuilder ssb = new SpannableStringBuilder(tv.getText());
        int start = ssb.getSpanStart(loadingSpan);
        int end = ssb.getSpanEnd(loadingSpan);
        if (start >= 0 && end >= 0) {
            ssb.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        ssb.removeSpan(loadingSpan);
        // animate the change
        TransitionManager.beginDelayedTransition((ViewGroup) tv.getParent());
        tv.setText(ssb);
    }
}
Also used : TextView(android.widget.TextView) BitmapDrawable(android.graphics.drawable.BitmapDrawable) SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan)

Example 77 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project KeepScore by nolanlawson.

the class AutofitTextView method onMeasure.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int height = getHeight() - getCompoundPaddingBottom() - getCompoundPaddingTop();
    int maxNumLines = (height / getLineHeight());
    if (maxNumLines >= 2) {
        String oldText = getText().toString();
        int cutoffIndex = StringUtil.getNthIndexOf('\n', oldText, maxNumLines);
        if (cutoffIndex != -1) {
            // cut off the text
            int startOfLastLine = StringUtil.getNthIndexOf('\n', oldText, maxNumLines - 1);
            // make the last line semi-transparent, to indicate that there
            // are more results left
            // (similar to the ellipsize effect in normal text views, but
            // vertical)
            // get the original color (blue or red)
            ForegroundColorSpan[] foregroundColorSpans = ((SpannedString) getText()).getSpans(startOfLastLine + 1, cutoffIndex, ForegroundColorSpan.class);
            // make an alpha-ized gradient out of the original color
            int originalColor = foregroundColorSpans[0].getForegroundColor();
            int startColor = (START_ALPHA << 24) | (0x00FFFFFF & originalColor);
            int endColor = (END_ALPHA << 24) | (0x00FFFFFF & originalColor);
            int numLines = StringUtil.count(getText().subSequence(0, cutoffIndex).toString(), "\n");
            float startY = (numLines * getLineHeight());
            float endY = startY + getLineHeight();
            // build up a new spannable
            SpannableStringBuilder builder = new SpannableStringBuilder().append(getText().subSequence(0, startOfLastLine)).append(getText().subSequence(startOfLastLine, cutoffIndex).toString());
            builder.setSpan(new TopDownGradientSpan(startColor, endColor, startY, endY), startOfLastLine, cutoffIndex, 0);
            setText(builder);
        }
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) TopDownGradientSpan(com.nolanlawson.keepscore.android.TopDownGradientSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 78 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project k-9 by k9mail.

the class NotificationContentCreator method buildMessageSummary.

private CharSequence buildMessageSummary(String sender, String subject) {
    if (sender == null) {
        return subject;
    }
    SpannableStringBuilder summary = new SpannableStringBuilder();
    summary.append(sender);
    summary.append(" ");
    summary.append(subject);
    summary.setSpan(getEmphasizedSpan(), 0, sender.length(), 0);
    return summary;
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 79 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project k-9 by k9mail.

the class MessageHeader method populateAdditionalHeadersView.

/**
     * Set up the additional headers text view with the supplied header data.
     *
     * @param additionalHeaders List of header entries. Each entry consists of a header
     *                          name and a header value. Header names may appear multiple
     *                          times.
     *                          <p/>
     *                          This method is always called from within the UI thread by
     *                          {@link #showAdditionalHeaders()}.
     */
private void populateAdditionalHeadersView(final List<HeaderEntry> additionalHeaders) {
    SpannableStringBuilder sb = new SpannableStringBuilder();
    boolean first = true;
    for (HeaderEntry additionalHeader : additionalHeaders) {
        if (!first) {
            sb.append("\n");
        } else {
            first = false;
        }
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        SpannableString label = new SpannableString(additionalHeader.label + ": ");
        label.setSpan(boldSpan, 0, label.length(), 0);
        sb.append(label);
        sb.append(MimeUtility.unfoldAndDecode(additionalHeader.value));
    }
    mAdditionalHeadersView.setText(sb);
}
Also used : SpannableString(android.text.SpannableString) StyleSpan(android.text.style.StyleSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 80 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project plaid by nickbutcher.

the class HomeActivity method setNoFiltersEmptyTextVisibility.

private void setNoFiltersEmptyTextVisibility(int visibility) {
    if (visibility == View.VISIBLE) {
        if (noFiltersEmptyText == null) {
            // create the no filters empty text
            ViewStub stub = (ViewStub) findViewById(R.id.stub_no_filters);
            noFiltersEmptyText = (TextView) stub.inflate();
            String emptyText = getString(R.string.no_filters_selected);
            int filterPlaceholderStart = emptyText.indexOf('ࢴ');
            int altMethodStart = filterPlaceholderStart + 3;
            SpannableStringBuilder ssb = new SpannableStringBuilder(emptyText);
            // show an image of the filter icon
            ssb.setSpan(new ImageSpan(this, R.drawable.ic_filter_small, ImageSpan.ALIGN_BASELINE), filterPlaceholderStart, filterPlaceholderStart + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // make the alt method (swipe from right) less prominent and italic
            ssb.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.text_secondary_light)), altMethodStart, emptyText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ssb.setSpan(new StyleSpan(Typeface.ITALIC), altMethodStart, emptyText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            noFiltersEmptyText.setText(ssb);
            noFiltersEmptyText.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    drawer.openDrawer(GravityCompat.END);
                }
            });
        }
        noFiltersEmptyText.setVisibility(visibility);
    } else if (noFiltersEmptyText != null) {
        noFiltersEmptyText.setVisibility(visibility);
    }
}
Also used : ViewStub(android.view.ViewStub) ForegroundColorSpan(android.text.style.ForegroundColorSpan) StyleSpan(android.text.style.StyleSpan) ImageView(android.widget.ImageView) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan)

Aggregations

SpannableStringBuilder (android.text.SpannableStringBuilder)336 ForegroundColorSpan (android.text.style.ForegroundColorSpan)56 ImageSpan (android.text.style.ImageSpan)53 TextView (android.widget.TextView)40 View (android.view.View)38 StyleSpan (android.text.style.StyleSpan)35 Spanned (android.text.Spanned)28 Drawable (android.graphics.drawable.Drawable)27 Spannable (android.text.Spannable)25 SpannableString (android.text.SpannableString)21 Test (org.junit.Test)21 CharacterStyle (android.text.style.CharacterStyle)18 TextPaint (android.text.TextPaint)17 RelativeSizeSpan (android.text.style.RelativeSizeSpan)17 Paint (android.graphics.Paint)16 Context (android.content.Context)15 TextAppearanceSpan (android.text.style.TextAppearanceSpan)15 ImageView (android.widget.ImageView)15 Intent (android.content.Intent)13 Editable (android.text.Editable)13