Search in sources :

Example 31 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project android_frameworks_base by ResurrectionRemix.

the class RestrictedLockUtils method setMenuItemAsDisabledByAdmin.

/**
     * Set the menu item as disabled by admin by adding a restricted padlock at the end of the
     * text and set the click listener which will send an intent to show the admin support details
     * dialog. If the admin is null, remove the padlock and disabled color span. When the admin is
     * null, we also set the OnMenuItemClickListener to null, so if you want to set a custom
     * OnMenuItemClickListener, set it after calling this method.
     */
public static void setMenuItemAsDisabledByAdmin(final Context context, final MenuItem item, final EnforcedAdmin admin) {
    SpannableStringBuilder sb = new SpannableStringBuilder(item.getTitle());
    removeExistingRestrictedSpans(sb);
    if (admin != null) {
        final int disabledColor = context.getColor(R.color.disabled_text_color);
        sb.setSpan(new ForegroundColorSpan(disabledColor), 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ImageSpan image = new RestrictedLockImageSpan(context);
        sb.append(" ", image, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                sendShowAdminSupportDetailsIntent(context, admin);
                return true;
            }
        });
    } else {
        item.setOnMenuItemClickListener(null);
    }
    item.setTitle(sb);
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) MenuItem(android.view.MenuItem) SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan)

Example 32 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project Tusky by Vavassor.

the class LinkHelper method setClickableText.

public static void setClickableText(TextView view, Spanned content, @Nullable Status.Mention[] mentions, boolean useCustomTabs, 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);
                }
            };
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        } else if (text.charAt(0) == '@' && mentions != null) {
            final String accountUsername = text.subSequence(1, text.length()).toString();
            String id = null;
            for (Status.Mention mention : mentions) {
                if (mention.localUsername.equals(accountUsername)) {
                    id = mention.id;
                }
            }
            if (id != null) {
                final String accountId = id;
                ClickableSpan newSpan = new ClickableSpan() {

                    @Override
                    public void onClick(View widget) {
                        listener.onViewAccount(accountId);
                    }
                };
                builder.removeSpan(span);
                builder.setSpan(newSpan, start, end, flags);
            }
        } else if (useCustomTabs) {
            ClickableSpan newSpan = new CustomTabURLSpan(span.getURL());
            builder.removeSpan(span);
            builder.setSpan(newSpan, start, end, flags);
        }
    }
    view.setText(builder);
    view.setLinksClickable(true);
    view.setMovementMethod(LinkMovementMethod.getInstance());
}
Also used : URLSpan(android.text.style.URLSpan) ClickableSpan(android.text.style.ClickableSpan) TextView(android.widget.TextView) View(android.view.View) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 33 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project android_frameworks_base by ResurrectionRemix.

the class NotificationBuilderTest method subst.

private static CharSequence subst(CharSequence in, char ch, CharSequence sub) {
    int i = 0;
    SpannableStringBuilder edit = new SpannableStringBuilder(in);
    while (i < edit.length()) {
        if (edit.charAt(i) == ch) {
            edit.replace(i, i + 1, sub);
            i += sub.length();
        } else {
            i++;
        }
    }
    return edit;
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 34 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project android_frameworks_base by ResurrectionRemix.

the class ViewPropertyAlphaActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_properties);
    getWindow().getDecorView().postDelayed(new Runnable() {

        @Override
        public void run() {
            startAnim(R.id.button);
            startAnim(R.id.textview);
            startAnim(R.id.spantext);
            startAnim(R.id.edittext);
            startAnim(R.id.selectedtext);
            startAnim(R.id.textviewbackground);
            startAnim(R.id.layout);
            startAnim(R.id.imageview);
            startAnim(myViewAlphaDefault);
            startAnim(myViewAlphaHandled);
            EditText selectedText = (EditText) findViewById(R.id.selectedtext);
            selectedText.setSelection(3, 8);
        }
    }, 2000);
    Button invalidator = (Button) findViewById(R.id.invalidateButton);
    invalidator.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            findViewById(R.id.textview).invalidate();
            findViewById(R.id.spantext).invalidate();
        }
    });
    TextView textView = (TextView) findViewById(R.id.spantext);
    if (textView != null) {
        SpannableStringBuilder text = new SpannableStringBuilder("Now this is a short text message with spans");
        text.setSpan(new BackgroundColorSpan(Color.RED), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new SuggestionSpan(this, new String[] { "longer" }, 3), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new UnderlineSpan(), 17, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ImageSpan(this, R.drawable.icon), 21, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(text);
    }
    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    myViewAlphaDefault = new MyView(this, false);
    myViewAlphaDefault.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    container.addView(myViewAlphaDefault);
    myViewAlphaHandled = new MyView(this, true);
    myViewAlphaHandled.setLayoutParams(new LinearLayout.LayoutParams(75, 75));
    container.addView(myViewAlphaHandled);
}
Also used : EditText(android.widget.EditText) ForegroundColorSpan(android.text.style.ForegroundColorSpan) TextView(android.widget.TextView) View(android.view.View) UnderlineSpan(android.text.style.UnderlineSpan) Button(android.widget.Button) TextView(android.widget.TextView) SuggestionSpan(android.text.style.SuggestionSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan) LinearLayout(android.widget.LinearLayout) ImageSpan(android.text.style.ImageSpan)

Example 35 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project android_frameworks_base by ResurrectionRemix.

the class NotificationTests method BOLD.

static SpannableStringBuilder BOLD(CharSequence str) {
    final SpannableStringBuilder ssb = new SpannableStringBuilder(str);
    ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, ssb.length(), 0);
    return ssb;
}
Also used : StyleSpan(android.text.style.StyleSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

SpannableStringBuilder (android.text.SpannableStringBuilder)705 TextView (android.widget.TextView)114 ForegroundColorSpan (android.text.style.ForegroundColorSpan)112 View (android.view.View)100 StyleSpan (android.text.style.StyleSpan)85 ImageSpan (android.text.style.ImageSpan)77 Test (org.junit.Test)65 SpannableString (android.text.SpannableString)55 Drawable (android.graphics.drawable.Drawable)44 TextPaint (android.text.TextPaint)43 Intent (android.content.Intent)42 Spanned (android.text.Spanned)38 RelativeSizeSpan (android.text.style.RelativeSizeSpan)38 Paint (android.graphics.Paint)34 Spannable (android.text.Spannable)33 ImageView (android.widget.ImageView)31 ArrayList (java.util.ArrayList)28 Typeface (android.graphics.Typeface)26 Context (android.content.Context)25 SuppressLint (android.annotation.SuppressLint)24