Example usage for android.text SpannableStringBuilder append

List of usage examples for android.text SpannableStringBuilder append

Introduction

In this page you can find the example usage for android.text SpannableStringBuilder append.

Prototype

public SpannableStringBuilder append(char text) 

Source Link

Usage

From source file:de.schildbach.litecoinwallet.ui.RequestCoinsFragment.java

private void updateView() {
    if (!isResumed())
        return;//from   w  ww .  j  a v  a2s  .  c om

    final String request = determineRequestStr(true);

    // update qr code
    final int size = (int) (256 * getResources().getDisplayMetrics().density);
    qrCodeBitmap = Qr.bitmap(request, size);
    qrView.setImageBitmap(qrCodeBitmap);

    // update ndef message
    final boolean nfcSuccess = Nfc.publishUri(nfcManager, getActivity(), request);

    // update initiate request message
    final SpannableStringBuilder initiateText = new SpannableStringBuilder(
            getString(R.string.request_coins_fragment_initiate_request_qr));
    if (nfcSuccess)
        initiateText.append(' ').append(getString(R.string.request_coins_fragment_initiate_request_nfc));
    initiateRequestView.setText(initiateText);

    // update bluetooth message
    final boolean serviceRunning = application.isServiceRunning(AcceptBluetoothService.class);
    bluetoothEnabledView.setVisibility(
            bluetoothAdapter != null && bluetoothAdapter.isEnabled() && serviceRunning ? View.VISIBLE
                    : View.GONE);
}

From source file:org.sufficientlysecure.keychain.ui.ViewKeyKeybaseFragment.java

@Override
public void onCryptoOperationError(KeybaseVerificationResult result) {

    result.createNotify(getActivity()).show();

    SpannableStringBuilder ssb = new SpannableStringBuilder();

    ssb.append(getString(R.string.keybase_proof_failure));
    String msg = getString(result.getLog().getLast().mType.mMsgId);
    if (msg == null) {
        msg = getString(R.string.keybase_unknown_proof_failure);
    }//from  w  ww.  j a  v a  2s.c om
    StyleSpan bold = new StyleSpan(Typeface.BOLD);
    ssb.setSpan(bold, 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.append("\n\n").append(msg);

    displaySpannableResult(ssb);
}

From source file:com.groksolutions.grok.mobile.annotation.AnnotationListFragment.java

/**
 * Format annotation message footer for list item display
 *
 * @param annotation/* w ww. j av  a  2  s.c o  m*/
 * @param showDate
 * @return
 */
SpannableStringBuilder formatAnnotationFooter(Annotation annotation, boolean showDate) {
    int pos, rightPos;

    Date created = new Date(annotation.getCreated());
    // Format text
    SpannableStringBuilder text = new SpannableStringBuilder();

    // <div align="right">
    rightPos = pos = text.length();

    //<small>6/30/14 3:25 PM - </small>

    // Show date if different annotation or if created date changed
    if (showDate) {
        text.append(_dateFormat.format(created)).append(" ");
    }
    text.append(_timeFormat.format(created)).append(" - ");
    text.setSpan(new RelativeSizeSpan(0.5f), pos, text.length(), 0);

    // User
    pos = text.length();
    text.append(annotation.getUser());
    text.setSpan(new RelativeSizeSpan(1.0f), pos, text.length(), 0);

    // </div align="right">
    text.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), rightPos, text.length(), 0);

    // Attach hidden annotation ID to the text.
    // This ID is used by the actions attached to the list
    text.setSpan(new android.text.Annotation("id", annotation.getId()), 0, text.length(), 0);

    return text;
}

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

@Override
public void onBindViewHolder(MessageViewHolder messageViewHolder, int i) {
    Message message = messages.get(i);/*from w  w  w. ja va  2  s.com*/
    int viewType = getItemViewType(i);

    if (viewType == ITEM_LEFT_PRIMARY || viewType == ITEM_RIGHT_PRIMARY) {
        QuickContactBadge contactBadge = messageViewHolder.getContactBadge();
        if (viewType == ITEM_LEFT_PRIMARY) {
            contactBadge.assignContactFromPhone(message.getContact(), true);
        } else {
            contactBadge.assignContactFromPhone(message.getDid(), true);
        }
        String photoUri;
        if (viewType == ITEM_LEFT_PRIMARY) {
            photoUri = Utils.getContactPhotoUri(applicationContext, message.getContact());

        } else {
            photoUri = Utils.getContactPhotoUri(applicationContext, ContactsContract.Profile.CONTENT_URI);
            if (photoUri == null) {
                photoUri = Utils.getContactPhotoUri(applicationContext, message.getDid());
            }
        }
        if (photoUri != null) {
            contactBadge.setImageURI(Uri.parse(photoUri));
        } else {
            contactBadge.setImageToDefault();
        }
    }

    View smsContainer = messageViewHolder.getSmsContainer();

    TextView messageText = messageViewHolder.getMessageText();
    SpannableStringBuilder messageTextBuilder = new SpannableStringBuilder();
    messageTextBuilder.append(message.getText());
    if (!filterConstraint.equals("")) {
        int index = message.getText().toLowerCase().indexOf(filterConstraint.toLowerCase());
        if (index != -1) {
            messageTextBuilder.setSpan(
                    new BackgroundColorSpan(ContextCompat.getColor(applicationContext, R.color.highlight)),
                    index, index + filterConstraint.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            messageTextBuilder.setSpan(
                    new ForegroundColorSpan(ContextCompat.getColor(applicationContext, R.color.dark_gray)),
                    index, index + filterConstraint.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    }
    messageText.setText(messageTextBuilder);

    TextView dateText = messageViewHolder.getDateText();
    if (!message.isDelivered()) {
        if (!message.isDeliveryInProgress()) {
            SpannableStringBuilder dateTextBuilder = new SpannableStringBuilder();
            if (isItemChecked(i)) {
                dateTextBuilder
                        .append(applicationContext.getString(R.string.conversation_message_not_sent_selected));
            } else {
                dateTextBuilder.append(applicationContext.getString(R.string.conversation_message_not_sent));
            }
            dateTextBuilder.setSpan(
                    new ForegroundColorSpan(
                            isItemChecked(i) ? ContextCompat.getColor(applicationContext, android.R.color.white)
                                    : ContextCompat.getColor(applicationContext,
                                            android.R.color.holo_red_dark)),
                    0, dateTextBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            dateText.setText(dateTextBuilder);
            dateText.setVisibility(View.VISIBLE);
        } else {
            dateText.setText(applicationContext.getString(R.string.conversation_message_sending));
            dateText.setVisibility(View.VISIBLE);
        }
    } else if (i == messages.size() - 1
            || ((viewType == ITEM_LEFT_PRIMARY || viewType == ITEM_LEFT_SECONDARY)
                    && getItemViewType(i + 1) != ITEM_LEFT_SECONDARY)
            || ((viewType == ITEM_RIGHT_PRIMARY || viewType == ITEM_RIGHT_SECONDARY)
                    && getItemViewType(i + 1) != ITEM_RIGHT_SECONDARY)) {
        dateText.setText(Utils.getFormattedDate(applicationContext, message.getDate(), false));
        dateText.setVisibility(View.VISIBLE);
    } else {
        dateText.setVisibility(View.GONE);
    }

    if (viewType == ITEM_LEFT_PRIMARY || viewType == ITEM_LEFT_SECONDARY) {
        smsContainer.setBackgroundResource(isItemChecked(i) ? android.R.color.holo_blue_dark : R.color.primary);
    } else {
        smsContainer.setBackgroundResource(
                isItemChecked(i) ? android.R.color.holo_blue_dark : android.R.color.white);
        messageText.setTextColor(
                isItemChecked(i) ? ContextCompat.getColor(applicationContext, android.R.color.white)
                        : ContextCompat.getColor(applicationContext, R.color.dark_gray));
        messageText.setLinkTextColor(
                isItemChecked(i) ? ContextCompat.getColor(applicationContext, android.R.color.white)
                        : ContextCompat.getColor(applicationContext, R.color.dark_gray));
        dateText.setTextColor(
                isItemChecked(i) ? ContextCompat.getColor(applicationContext, R.color.message_translucent_white)
                        : ContextCompat.getColor(applicationContext, R.color.message_translucent_dark_grey));
    }
}

From source file:de.langerhans.wallet.ui.RequestCoinsFragment.java

private void updateView() {
    if (!isResumed())
        return;//from www .  j a v  a2  s  .  c o  m

    final String bitcoinRequest = determineBitcoinRequestStr(true);
    byte[] paymentRequest;
    try {
        paymentRequest = determinePaymentRequest(true);
        oldRequest = paymentRequest;
    } catch (IllegalArgumentException e) {
        paymentRequest = oldRequest;
    }

    // update qr-code
    final int size = (int) (256 * getResources().getDisplayMetrics().density);
    final String qrContent;
    if (config.getQrPaymentRequestEnabled())
        qrContent = "DOGECOIN:-" + Qr.encodeBinary(paymentRequest);
    else
        qrContent = bitcoinRequest;
    qrCodeBitmap = Qr.bitmap(qrContent, size);
    qrView.setImageBitmap(qrCodeBitmap);

    // update nfc ndef message
    final boolean nfcSuccess = Nfc.publishMimeObject(nfcManager, activity,
            com.google.dogecoin.protocols.payments.PaymentProtocol.MIMETYPE_PAYMENTREQUEST, paymentRequest,
            false);

    // update initiate request message
    final SpannableStringBuilder initiateText = new SpannableStringBuilder(
            getString(R.string.request_coins_fragment_initiate_request_qr));
    if (nfcSuccess)
        initiateText.append(' ').append(getString(R.string.request_coins_fragment_initiate_request_nfc));
    initiateRequestView.setText(initiateText);

    // focus linking
    final int activeAmountViewId = amountCalculatorLink.activeTextView().getId();
    acceptBluetoothPaymentView.setNextFocusUpId(activeAmountViewId);
}

From source file:com.newcell.calltext.ui.messages.MessageFragment.java

/**
 * Format the string to show the Contact Name if they're a contact,
 * or just the phone number/*from www .  j  a  v  a  2s.  com*/
 * @param remoteContact The full SIP URI of the contact
 * @return The formatted string
 */
private CharSequence formatFrom(String remoteContact) {
    SpannableStringBuilder buf = new SpannableStringBuilder();

    CallerInfo callerInfo = CallerInfo.getCallerInfoFromSipUri(getActivity(), remoteContact);
    if (callerInfo != null && callerInfo.contactExists) {
        buf.append(callerInfo.name);
    } else {
        buf.append(SipUri.getDisplayedSimpleContact(remoteContact));
    }

    return buf;
}

From source file:org.telegram.ui.IdenticonActivity.java

@Override
public View createView(Context context) {
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setAllowOverlayTitle(true);
    actionBar.setTitle(LocaleController.getString("EncryptionKey", R.string.EncryptionKey));

    actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
        @Override/*from w  ww  .  java 2s.c o m*/
        public void onItemClick(int id) {
            if (id == -1) {
                finishFragment();
            }
        }
    });

    fragmentView = new LinearLayout(context);
    LinearLayout linearLayout = (LinearLayout) fragmentView;
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setWeightSum(100);
    linearLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.card_background));
    fragmentView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    frameLayout.setPadding(AndroidUtilities.dp(20), AndroidUtilities.dp(20), AndroidUtilities.dp(20),
            AndroidUtilities.dp(20));
    linearLayout.addView(frameLayout,
            LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, 50.0f));

    ImageView identiconView = new ImageView(context);
    identiconView.setScaleType(ImageView.ScaleType.FIT_XY);
    frameLayout.addView(identiconView,
            LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));

    frameLayout = new FrameLayout(context);
    frameLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.background));
    frameLayout.setPadding(AndroidUtilities.dp(10), 0, AndroidUtilities.dp(10), AndroidUtilities.dp(10));
    linearLayout.addView(frameLayout,
            LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, 50.0f));

    TextView textView = new TextView(context);
    textView.setTextColor(ContextCompat.getColor(context, R.color.secondary_text));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    textView.setLinksClickable(true);
    textView.setClickable(true);
    textView.setMovementMethod(new LinkMovementMethodMy());
    //textView.setAutoLinkMask(Linkify.WEB_URLS);
    textView.setLinkTextColor(Theme.MSG_LINK_TEXT_COLOR);
    textView.setGravity(Gravity.CENTER);
    frameLayout.addView(textView,
            LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));

    TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance().getEncryptedChat(chat_id);
    if (encryptedChat != null) {
        IdenticonDrawable drawable = new IdenticonDrawable();
        identiconView.setImageDrawable(drawable);
        drawable.setEncryptedChat(encryptedChat);
        TLRPC.User user = MessagesController.getInstance().getUser(encryptedChat.user_id);
        SpannableStringBuilder hash = new SpannableStringBuilder();
        if (encryptedChat.key_hash.length > 16) {
            String hex = Utilities.bytesToHex(encryptedChat.key_hash);
            for (int a = 0; a < 32; a++) {
                if (a != 0) {
                    if (a % 8 == 0) {
                        hash.append('\n');
                    } else if (a % 4 == 0) {
                        hash.append(' ');
                    }
                }
                hash.append(hex.substring(a * 2, a * 2 + 2));
                hash.append(' ');
            }
            hash.append("\n\n");
        }
        hash.append(AndroidUtilities.replaceTags(LocaleController.formatString("EncryptionKeyDescription",
                R.string.EncryptionKeyDescription, user.first_name, user.first_name)));
        final String url = "telegram.org";
        int index = hash.toString().indexOf(url);
        if (index != -1) {
            hash.setSpan(
                    new URLSpanReplacement(
                            LocaleController.getString("EncryptionKeyLink", R.string.EncryptionKeyLink)),
                    index, index + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.setText(hash);
    }

    return fragmentView;
}

From source file:com.abcvoipsip.ui.messages.ConverstationsAdapter.java

private CharSequence formatMessage(Cursor cursor) {
    SpannableStringBuilder buf = new SpannableStringBuilder();
    /*/*from w  w w .  ja  v a  2  s .c  om*/
    String remoteContact = cursor.getString(cursor.getColumnIndex(SipMessage.FIELD_FROM));
    if (remoteContact.equals("SELF")) {
    remoteContact = cursor.getString(cursor.getColumnIndex(SipMessage.FIELD_TO));
    buf.append("To: ");
    }
    */
    String remoteContactFull = cursor.getString(cursor.getColumnIndex(SipMessage.FIELD_FROM_FULL));
    buf.append(SipUri.getDisplayedSimpleContact(remoteContactFull));

    int counter = cursor.getInt(cursor.getColumnIndex("counter"));
    if (counter > 1) {
        buf.append(" (" + counter + ") ");
    }

    int read = cursor.getInt(cursor.getColumnIndex(SipMessage.FIELD_READ));
    // Unread messages are shown in bold
    if (read == 0) {
        buf.setSpan(STYLE_BOLD, 0, buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    }
    return buf;
}

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   www  .  ja va  2s  .  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));
    }
}

From source file:com.deliciousdroid.fragment.ViewBookmarkFragment.java

private void addTag(SpannableStringBuilder builder, Tag t, TagSpan.OnTagClickListener listener) {
    int flags = 0;

    if (builder.length() != 0) {
        builder.append("  ");
    }//from w  w w. j a va  2s  .c  o  m

    int start = builder.length();
    builder.append(t.getTagName());
    int end = builder.length();

    TagSpan span = new TagSpan(t.getTagName());
    span.setOnTagClickListener(listener);

    builder.setSpan(span, start, end, flags);
}