Example usage for android.text.style BackgroundColorSpan BackgroundColorSpan

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

Introduction

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

Prototype

public BackgroundColorSpan(@NonNull Parcel src) 

Source Link

Document

Creates a BackgroundColorSpan from a parcel.

Usage

From source file:Main.java

public static SpannableString backgroundColor(SpannableString sequence, List<String> color) {
    int hex = Color.parseColor(color.get(0));

    sequence.setSpan(new BackgroundColorSpan(hex), 0, sequence.length(), 0);
    return sequence;
}

From source file:Main.java

public static SpannableString setTextBackground(String content, int startIndex, int endIndex,
        int backgroundColor) {
    if (TextUtils.isEmpty(content) || startIndex < 0 || endIndex >= content.length()
            || startIndex >= endIndex) {
        return null;
    }/*  w  ww . j ava 2 s.  c om*/

    SpannableString spannableString = new SpannableString(content);
    spannableString.setSpan(new BackgroundColorSpan(backgroundColor), startIndex, endIndex,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannableString;
}

From source file:com.none.tom.simplerssreader.utils.SearchUtils.java

public static void setSearchResultsSpan(final Activity activity, final SearchResults results, final Spannable s,
        final int colorHighlightCurrent, final int colorHighlight, final int colorFont, final int position) {
    final int index = results.columnIndexOf(position);

    int i = 0;//from   w w w .ja  v a2s. c o  m

    for (final Map.Entry<Integer, Integer> entry : results.getRowIndicesAt(index).entrySet()) {
        final int start = entry.getKey();
        final int end = entry.getValue();

        final BackgroundColorSpan span;

        if (results.isCurrent(index, i)) {
            span = new BackgroundColorSpan(colorHighlightCurrent);
        } else {
            span = new BackgroundColorSpan(colorHighlight);
        }

        s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        if (ThemeUtils.isDarkThemeSelected(activity)) {
            s.setSpan(new ForegroundColorSpan(colorFont), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        i++;
    }
}

From source file:im.zico.fancy.common.widget.HackyMovementMethod.java

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    if (mGray == null) {
        mGray = new BackgroundColorSpan(
                ContextCompat.getColor(widget.getContext(), R.color.alpha_spannable_pressed));
    }/*w  ww.  j  a v a  2 s  .c  o m*/

    mIsLinkHit = false;

    int action = event.getAction();

    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();
        x += widget.getScrollX();
        y += widget.getScrollY();

        int line = widget.getLayout().getLineForVertical(y);
        int offset = widget.getLayout().getOffsetForHorizontal(line, x);

        ClickableSpan[] spans = buffer.getSpans(offset, offset, ClickableSpan.class);

        if (spans.length != 0) {
            int start = buffer.getSpanStart(spans[0]);
            int end = buffer.getSpanEnd(spans[0]);
            mIsLinkHit = true;
            if (action == MotionEvent.ACTION_DOWN) {
                buffer.setSpan(mGray, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else if (action == MotionEvent.ACTION_UP) {
                spans[0].onClick(widget);
                buffer.removeSpan(mGray);
            }
            return true;
        }
    } else {
        buffer.removeSpan(mGray);
    }

    return Touch.onTouchEvent(widget, buffer, event);
}

From source file:com.zyz.mobile.book.UserSpan.java

public UserSpan(UserSpanType type, int color, int start, int end, String note, String description) {
    CharacterStyle object = null;//from w  ww  .j a v a 2  s .c  om

    switch (type) {
    case HIGHLGHT:
        object = new BackgroundColorSpan(color);
        break;
    case UNDERLINE:
        object = new UnderlineSpan();
    case BOOKMARK:
    case NOTE:
        break;
    default:
        break;
    }
    Initialize(object, type, color, start, end, note, description);
}

From source file:com.manning.androidhacks.hack010.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from   w  w w. ja v  a2 s  .  co  m

    final TextView textView1 = (TextView) findViewById(R.id.my_text_view_html);
    textView1.setText(Html.fromHtml(getString(R.string.text1)));
    textView1.setMovementMethod(LinkMovementMethod.getInstance());

    final Spannable text2 = new SpannableString(getString(R.string.text2));
    text2.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);
    text2.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 9, 0);

    ((TextView) findViewById(R.id.my_text_view_spannable)).setText(text2);
}

From source file:im.vector.adapters.VectorSearchMessagesListAdapter.java

public VectorSearchMessagesListAdapter(MXSession session, Context context, boolean displayRoomName,
        MXMediasCache mediasCache) {/*from w  w w  .j  a  v  a2s.c  om*/
    super(session, context, R.layout.adapter_item_vector_message_text_emote_notice,
            R.layout.adapter_item_vector_message_image_video,
            R.layout.adapter_item_vector_message_text_emote_notice,
            R.layout.adapter_item_vector_message_room_member,
            R.layout.adapter_item_vector_message_text_emote_notice, R.layout.adapter_item_vector_message_file,
            R.layout.adapter_item_vector_message_merge, R.layout.adapter_item_vector_message_image_video,
            R.layout.adapter_item_vector_message_emoji, R.layout.adapter_item_vector_message_code,
            R.layout.adapter_item_vector_message_image_video, R.layout.adapter_item_vector_hidden_message,
            mediasCache);

    setNotifyOnChange(true);
    mDisplayRoomName = displayRoomName;

    mBackgroundColorSpan = new BackgroundColorSpan(
            ContextCompat.getColor(mContext, R.color.vector_green_color));
}

From source file:org.kontalk.ui.view.TextContentView.java

private void init(Context context) {
    int color = ContextCompat.getColor(context, R.color.highlight_color);
    mHighlightColorSpan = new BackgroundColorSpan(color);
}

From source file:com.todoroo.astrid.tags.TagsControlSet.java

private Function<TagData, SpannableString> tagToString(final float maxLength) {
    return tagData -> {
        String tagName = tagData.getName();
        tagName = tagName.substring(0, Math.min(tagName.length(), (int) maxLength)).replace(' ',
                NO_BREAK_SPACE);/*from w ww .  j  ava2 s .  c  om*/
        SpannableString string = new SpannableString(NO_BREAK_SPACE + tagName + NO_BREAK_SPACE);
        int themeIndex = tagData.getColor();
        ThemeColor color = themeIndex >= 0 ? themeCache.getThemeColor(themeIndex)
                : themeCache.getUntaggedColor();
        string.setSpan(new BackgroundColorSpan(color.getPrimaryColor()), 0, string.length(),
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        string.setSpan(new ForegroundColorSpan(color.getActionBarTint()), 0, string.length(),
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return string;
    };
}

From source file:net.kourlas.voipms_sms.adapters.ConversationsRecyclerViewAdapter.java

@Override
public void onBindViewHolder(ConversationViewHolder conversationViewHolder, int position) {
    Message message = messages.get(position);

    ViewSwitcher viewSwitcher = conversationViewHolder.getViewSwitcher();
    viewSwitcher.setDisplayedChild(isItemChecked(position) ? 1 : 0);

    QuickContactBadge contactBadge = conversationViewHolder.getContactBadge();
    contactBadge.assignContactFromPhone(message.getContact(), true);

    String photoUri = Utils.getContactPhotoUri(applicationContext, message.getContact());
    if (photoUri != null) {
        contactBadge.setImageURI(Uri.parse(photoUri));
    } else {/*from w  ww  . j ava2  s  . co  m*/
        contactBadge.setImageToDefault();
    }

    TextView contactTextView = conversationViewHolder.getContactTextView();
    String contactName = Utils.getContactName(applicationContext, message.getContact());
    SpannableStringBuilder contactTextBuilder = new SpannableStringBuilder();
    if (contactName != null) {
        contactTextBuilder.append(contactName);
    } else {
        contactTextBuilder.append(Utils.getFormattedPhoneNumber(message.getContact()));
    }
    if (!filterConstraint.equals("")) {
        int index = contactTextBuilder.toString().toLowerCase().indexOf(filterConstraint.toLowerCase());
        if (index != -1) {
            contactTextBuilder.setSpan(
                    new BackgroundColorSpan(ContextCompat.getColor(applicationContext, R.color.highlight)),
                    index, index + filterConstraint.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    }
    contactTextView.setText(contactTextBuilder);

    final TextView messageTextView = conversationViewHolder.getMessageTextView();
    SpannableStringBuilder messageTextBuilder = new SpannableStringBuilder();

    int index = message.getText().toLowerCase().indexOf(filterConstraint.toLowerCase());
    if (!filterConstraint.equals("") && index != -1) {
        int nonMessageOffset = index;
        if (message.getType() == Message.Type.OUTGOING) {
            messageTextBuilder.insert(0,
                    applicationContext.getString(R.string.conversations_message_you) + " ");
            nonMessageOffset += 5;
        }

        int substringOffset = index - 20;
        if (substringOffset > 0) {
            messageTextBuilder.append("...");
            nonMessageOffset += 3;

            while (message.getText().charAt(substringOffset) != ' ' && substringOffset < index - 1) {
                substringOffset += 1;
            }
            substringOffset += 1;
        } else {
            substringOffset = 0;
        }

        messageTextBuilder.append(message.getText().substring(substringOffset));
        messageTextBuilder.setSpan(
                new BackgroundColorSpan(ContextCompat.getColor(applicationContext, R.color.highlight)),
                nonMessageOffset - substringOffset,
                nonMessageOffset - substringOffset + filterConstraint.length(),
                SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
    } else {
        if (message.getType() == Message.Type.OUTGOING) {
            messageTextBuilder.append(applicationContext.getString(R.string.conversations_message_you));
            messageTextBuilder.append(" ");
        }
        messageTextBuilder.append(message.getText());
    }
    messageTextView.setText(messageTextBuilder);

    if (message.isUnread()) {
        contactTextView.setTypeface(null, Typeface.BOLD);
        messageTextView.setTypeface(null, Typeface.BOLD);
    } else {
        contactTextView.setTypeface(null, Typeface.NORMAL);
        messageTextView.setTypeface(null, Typeface.NORMAL);
    }

    // Set date line
    TextView dateTextView = conversationViewHolder.getDateTextView();
    if (message.isDraft()) {
        SpannableStringBuilder dateTextBuilder = new SpannableStringBuilder();
        dateTextBuilder.append(applicationContext.getString(R.string.conversations_message_draft));
        dateTextBuilder.setSpan(new StyleSpan(Typeface.ITALIC), 0, dateTextBuilder.length(),
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        dateTextView.setText(dateTextBuilder);
    } else if (!message.isDelivered()) {
        if (!message.isDeliveryInProgress()) {
            SpannableStringBuilder dateTextBuilder = new SpannableStringBuilder();
            dateTextBuilder.append(applicationContext.getString(R.string.conversations_message_not_sent));
            dateTextBuilder.setSpan(
                    new ForegroundColorSpan(
                            ContextCompat.getColor(applicationContext, android.R.color.holo_red_dark)),
                    0, dateTextBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            dateTextView.setText(dateTextBuilder);
        } else {
            dateTextView.setText(applicationContext.getString(R.string.conversations_message_sending));
        }
    } else {
        dateTextView.setText(Utils.getFormattedDate(applicationContext, message.getDate(), true));
    }
}