Example usage for android.text Spannable setSpan

List of usage examples for android.text Spannable setSpan

Introduction

In this page you can find the example usage for android.text Spannable setSpan.

Prototype

public void setSpan(Object what, int start, int end, int flags);

Source Link

Document

Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

Usage

From source file:io.relayr.tellmewhen.gcm.GcmIntentService.java

private void sendNotification(Bundle msg) {
    Storage.startRuleScreen(false);/*from  w  w w . j a  va 2  s . com*/

    String ruleId = msg.getString("rule_id", "null");
    if (ruleId == null) {
        sendNotification(getBaseContext().getString(R.string.please_open_app));
        return;
    }

    TMWRule rule = new Select().from(TMWRule.class).where("dbId = ?", ruleId).executeSingle();
    if (rule == null) {
        sendNotification(getBaseContext().getString(R.string.please_open_app));
        return;
    }

    Float notificationValue = 0f;
    try {
        notificationValue = Float.parseFloat(msg.getString("val", "0"));
    } catch (NumberFormatException e) {
        Log.e(GcmIntentService.class.getSimpleName(), e.getMessage());
    }

    pushedRules.put(new Pair<>(rule.getSensorType(), rule.name), notificationValue);

    String title = pushedRules.size() + " "
            + (pushedRules.size() > 1 ? getString(R.string.push_notification_title)
                    : getString(R.string.push_notification_title_one_rule));

    Spannable spanTitle = new SpannableString(title);
    spanTitle.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    Notification notification = new Builder(getApplicationContext()).setContentIntent(createContentIntent())
            .setDeleteIntent(createDeleteIntent()).setSmallIcon(R.drawable.icon_notifications)
            .setContentTitle(spanTitle).setContentText(getString(R.string.push_notification_summary))
            .setAutoCancel(true).setStyle(prepareBigNotificationDetails(spanTitle)).build();

    showNotification(TMW_NOTIFICATION_ID, notification);

    playSound();
}

From source file:com.google.zxing.client.android.result.supplement.SupplementalInfoRetriever.java

final void append(String itemID, String source, String[] newTexts, String linkURL) throws InterruptedException {

    final TextView textView = textViewRef.get();
    if (textView == null) {
        throw new InterruptedException();
    }/*w w  w  .j  av a2  s.co  m*/

    StringBuilder newTextCombined = new StringBuilder();

    if (source != null) {
        newTextCombined.append(source).append(" : ");
    }

    int linkStart = newTextCombined.length();

    boolean first = true;
    for (String newText : newTexts) {
        if (first) {
            newTextCombined.append(newText);
            first = false;
        } else {
            newTextCombined.append(" [");
            newTextCombined.append(newText);
            newTextCombined.append(']');
        }
    }

    int linkEnd = newTextCombined.length();

    String newText = newTextCombined.toString();
    final Spannable content = new SpannableString(newText + "\n\n");
    if (linkURL != null) {
        content.setSpan(new URLSpan(linkURL), linkStart, linkEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    handler.post(new Runnable() {
        public void run() {
            textView.append(content);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
        }
    });

    // Add the text to the history.
    historyManager.addHistoryItemDetails(itemID, newText);
}

From source file:com.joyepay.qrcode.result.supplement.SupplementalInfoRetriever.java

final void append(String itemID, String source, String[] newTexts, String linkURL) throws InterruptedException {

    final TextView textView = textViewRef.get();
    if (textView == null) {
        throw new InterruptedException();
    }//from  w ww . ja  va2 s.  c om

    StringBuilder newTextCombined = new StringBuilder();

    if (source != null) {
        newTextCombined.append(source).append(" : ");
    }

    int linkStart = newTextCombined.length();

    boolean first = true;
    for (String newText : newTexts) {
        if (first) {
            newTextCombined.append(newText);
            first = false;
        } else {
            newTextCombined.append(" [");
            newTextCombined.append(newText);
            newTextCombined.append(']');
        }
    }

    int linkEnd = newTextCombined.length();

    String newText = newTextCombined.toString();
    final Spannable content = new SpannableString(newText + "\n\n");
    if (linkURL != null) {
        content.setSpan(new URLSpan(linkURL), linkStart, linkEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    handler.post(new Runnable() {
        public void run() {
            textView.append(content);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
        }
    });

    // Add the text to the history.
    //    historyManager.addHistoryItemDetails(itemID, newText);
}

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

/**
 * set this span to the specified spannable
 *
 * @param spannable the spannable to be styled
 *///from   ww w .  ja v  a2 s.c  om
public void setSpan(Spannable spannable) {
    if (spannable != null && isValid(spannable)) {
        spannable.setSpan(mSpanObject, mStart, mEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }
}

From source file:org.sufficientlysecure.keychain.ui.adapter.HighlightQueryCursorAdapter.java

protected Spannable highlightSearchQuery(String text) {
    Spannable highlight = Spannable.Factory.getInstance().newSpannable(text);

    if (mCurQuery != null) {
        Pattern pattern = Pattern.compile("(?i)" + mCurQuery);
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            highlight.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.emphasis)),
                    matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }//from   w  ww. j  a  v  a  2 s  . com
        return highlight;
    } else {
        return highlight;
    }
}

From source file:com.fowlcorp.homebank4android.gui.AccountRecyclerAdapter.java

private Spannable colorText(String fieldName, String value) {
    Spannable span = new SpannableString(fieldName + value + getCurrency());
    span.setSpan(
            new ForegroundColorSpan((value.charAt(0) == '-' ? Color.rgb(206, 92, 0) : Color.rgb(78, 154, 54))),
            fieldName.length(), fieldName.length() + value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return span;/*  w w w.  ja v  a  2s  .c o m*/
}

From source file:org.smssecure.smssecure.contacts.ContactSelectionListAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    final DataHolder contactData = (DataHolder) view.getTag(R.id.contact_info_tag);
    final ViewHolder holder = (ViewHolder) view.getTag(R.id.holder_tag);
    if (holder == null) {
        Log.w(TAG, "ViewHolder was null. This should not happen.");
        return;/*w ww  . java2 s .c o m*/
    }
    if (contactData == null) {
        Log.w(TAG, "DataHolder was null. This should not happen.");
        return;
    }
    if (ID_COLUMN < 0) {
        populateColumnIndices(cursor);
    }

    contactData.type = cursor.getInt(TYPE_COLUMN);
    contactData.name = cursor.getString(NAME_COLUMN);
    contactData.number = cursor.getString(NUMBER_COLUMN);
    contactData.numberType = cursor.getInt(NUMBER_TYPE_COLUMN);
    contactData.label = cursor.getString(LABEL_COLUMN);
    contactData.id = cursor.getLong(ID_COLUMN);

    if (contactData.type != ContactsDatabase.PUSH_TYPE) {
        holder.name.setTextColor(drawables.getColor(1, 0xff000000));
        holder.number.setTextColor(drawables.getColor(1, 0xff000000));
    } else {
        holder.name.setTextColor(drawables.getColor(0, 0xa0000000));
        holder.number.setTextColor(drawables.getColor(0, 0xa0000000));
    }

    if (selectedContacts.containsKey(contactData.id)) {
        holder.checkBox.setChecked(true);
    } else {
        holder.checkBox.setChecked(false);
    }

    holder.name.setText(contactData.name);

    if (contactData.number == null || contactData.number.isEmpty()) {
        holder.name.setEnabled(false);
        holder.number.setText("");
    } else if (contactData.type == ContactsDatabase.PUSH_TYPE) {
        holder.number.setText(contactData.number);
    } else {
        final CharSequence label = ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(),
                contactData.numberType, contactData.label);
        final CharSequence numberWithLabel = contactData.number + "  " + label;
        final Spannable numberLabelSpan = new SpannableString(numberWithLabel);
        numberLabelSpan.setSpan(new ForegroundColorSpan(drawables.getColor(2, 0xff444444)),
                contactData.number.length(), numberWithLabel.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        holder.number.setText(numberLabelSpan);
    }
    holder.contactPhoto.setImageDrawable(ContactPhotoFactory.getLoadingPhoto(context));
    if (contactData.id > -1)
        loadBitmap(contactData.number, holder.contactPhoto);
}

From source file:com.securecomcode.text.contacts.ContactSelectionListAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    final DataHolder contactData = (DataHolder) view.getTag(R.id.contact_info_tag);
    final ViewHolder holder = (ViewHolder) view.getTag(R.id.holder_tag);
    if (holder == null) {
        Log.w(TAG, "ViewHolder was null. This should not happen.");
        return;/*w w  w  . j  a v a2  s .  c o m*/
    }
    if (contactData == null) {
        Log.w(TAG, "DataHolder was null. This should not happen.");
        return;
    }
    if (ID_COLUMN < 0) {
        populateColumnIndices(cursor);
    }

    contactData.type = cursor.getInt(TYPE_COLUMN);
    contactData.name = cursor.getString(NAME_COLUMN);
    contactData.number = cursor.getString(NUMBER_COLUMN);
    contactData.numberType = cursor.getInt(NUMBER_TYPE_COLUMN);
    contactData.label = cursor.getString(LABEL_COLUMN);
    contactData.id = cursor.getLong(ID_COLUMN);

    if (contactData.type != ContactsDatabase.PUSH_TYPE) {
        holder.name.setTextColor(drawables.getColor(1, 0xff000000));
        holder.number.setTextColor(drawables.getColor(1, 0xff000000));
    } else {
        holder.name.setTextColor(drawables.getColor(0, 0xa0000000));
        holder.number.setTextColor(drawables.getColor(0, 0xa0000000));
    }

    if (selectedContacts.containsKey(contactData.id)) {
        holder.checkBox.setChecked(true);
    } else {
        holder.checkBox.setChecked(false);
    }

    holder.name.setText(contactData.name);

    if (contactData.number == null || contactData.number.isEmpty()) {
        holder.name.setEnabled(false);
        holder.number.setText("");
    } else if (contactData.type == ContactsDatabase.PUSH_TYPE) {
        holder.number.setText(contactData.number);
    } else {
        final CharSequence label = ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(),
                contactData.numberType, contactData.label);
        final CharSequence numberWithLabel = contactData.number + "  " + label;
        final Spannable numberLabelSpan = new SpannableString(numberWithLabel);
        numberLabelSpan.setSpan(new ForegroundColorSpan(drawables.getColor(2, 0xff444444)),
                contactData.number.length(), numberWithLabel.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        holder.number.setText(numberLabelSpan);
    }
    holder.contactPhoto.setImageBitmap(defaultCroppedPhoto);
    if (contactData.id > -1)
        loadBitmap(contactData.number, holder.contactPhoto);
}

From source file:com.ubergeek42.WeechatAndroid.ChatLinesAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;//w  w w. ja  va2 s.  c  o  m

    // If we don't have the view, or we were using a filteredView, inflate a new one
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.chatview_line, null);
        holder = new ViewHolder();
        holder.timestamp = (TextView) convertView.findViewById(R.id.chatline_timestamp);
        holder.prefix = (TextView) convertView.findViewById(R.id.chatline_prefix);
        holder.message = (TextView) convertView.findViewById(R.id.chatline_message);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Change the font sizes
    holder.timestamp.setTextSize(textSize);
    holder.prefix.setTextSize(textSize);
    holder.message.setTextSize(textSize);

    BufferLine chatLine = (BufferLine) getItem(position);

    // Render the timestamp
    if (enableTimestamp) {
        holder.timestamp.setText(timestampFormat.format(chatLine.getTimestamp()));
        holder.timestamp.setPadding(holder.timestamp.getPaddingLeft(), holder.timestamp.getPaddingTop(), 5,
                holder.timestamp.getPaddingBottom());
    } else {
        holder.timestamp.setText("");
        holder.timestamp.setPadding(holder.timestamp.getPaddingLeft(), holder.timestamp.getPaddingTop(), 0,
                holder.timestamp.getPaddingBottom());
    }

    // Recalculate the prefix width based on the size of one character(fixed width font)
    if (prefixWidth == 0) {
        holder.prefix.setMinimumWidth(0);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < maxPrefix; i++) {
            sb.append("m");
        }
        holder.prefix.setText(sb.toString());
        holder.prefix.measure(convertView.getWidth(), convertView.getHeight());
        prefixWidth = holder.prefix.getMeasuredWidth();
    }

    // Render the prefix
    if (chatLine.getHighlight()) {
        String prefixStr = chatLine.getPrefix();
        Spannable highlightText = new SpannableString(prefixStr);
        highlightText.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, prefixStr.length(),
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        highlightText.setSpan(new BackgroundColorSpan(Color.MAGENTA), 0, prefixStr.length(),
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        holder.prefix.setText(highlightText);
    } else {
        if (enableColor) {
            holder.prefix.setText(Html.fromHtml(chatLine.getPrefixHTML()), TextView.BufferType.SPANNABLE);
        } else {
            holder.prefix.setText(chatLine.getPrefix());
        }
    }
    if (prefix_align.equals("right")) {
        holder.prefix.setGravity(Gravity.RIGHT);
        holder.prefix.setMinimumWidth(prefixWidth);
    } else if (prefix_align.equals("left")) {
        holder.prefix.setGravity(Gravity.LEFT);
        holder.prefix.setMinimumWidth(prefixWidth);
    } else {
        holder.prefix.setGravity(Gravity.LEFT);
        holder.prefix.setMinimumWidth(0);
    }

    // Render the message

    if (enableColor) {
        holder.message.setText(Html.fromHtml(chatLine.getMessageHTML()), TextView.BufferType.SPANNABLE);
    } else {
        holder.message.setText(chatLine.getMessage());
    }

    return convertView;
}

From source file:com.arantius.tivocommander.Person.java

private void requestFinished() {
    if (--mOutstandingRequests > 0) {
        return;//  w ww.  jav  a2  s  .  com
    }
    setProgressBarIndeterminateVisibility(false);

    if (mPerson == null || mCredits == null) {
        setContentView(R.layout.no_results);
        return;
    }

    setContentView(R.layout.list_person);

    // Credits.
    JsonNode[] credits = new JsonNode[mCredits.size()];
    int i = 0;
    for (JsonNode credit : mCredits) {
        credits[i++] = credit;
    }

    ListView lv = getListView();
    CreditsAdapter adapter = new CreditsAdapter(Person.this, R.layout.item_person_credits, credits);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(mOnItemClickListener);

    // Name.
    ((TextView) findViewById(R.id.person_name)).setText(mName);

    // Role.
    JsonNode rolesNode = mPerson.path("roleForPersonId");
    String[] roles = new String[rolesNode.size()];
    for (i = 0; i < rolesNode.size(); i++) {
        roles[i] = rolesNode.path(i).asText();
        roles[i] = Utils.ucFirst(roles[i]);
    }
    ((TextView) findViewById(R.id.person_role)).setText(Utils.join(", ", roles));

    // Birth date.
    TextView birthdateView = ((TextView) findViewById(R.id.person_birthdate));
    if (mPerson.has("birthDate")) {
        Date birthdate = Utils.parseDateStr(mPerson.path("birthDate").asText());
        SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMMM d, yyyy", Locale.US);
        dateFormatter.setTimeZone(TimeZone.getDefault());
        Spannable birthdateStr = new SpannableString("Birthdate: " + dateFormatter.format(birthdate));
        birthdateStr.setSpan(new ForegroundColorSpan(Color.WHITE), 11, birthdateStr.length(), 0);
        birthdateView.setText(birthdateStr);
    } else {
        birthdateView.setVisibility(View.GONE);
    }

    // Birth place.
    TextView birthplaceView = ((TextView) findViewById(R.id.person_birthplace));
    if (mPerson.has("birthPlace")) {
        Spannable birthplaceStr = new SpannableString("Birthplace: " + mPerson.path("birthPlace").asText());
        birthplaceStr.setSpan(new ForegroundColorSpan(Color.WHITE), 12, birthplaceStr.length(), 0);
        birthplaceView.setText(birthplaceStr);
    } else {
        birthplaceView.setVisibility(View.GONE);
    }

    ImageView iv = (ImageView) findViewById(R.id.person_image);
    View pv = findViewById(R.id.person_image_progress);
    String imgUrl = Utils.findImageUrl(mPerson);
    new DownloadImageTask(this, iv, pv).execute(imgUrl);
}