List of usage examples for android.text SpannableString setSpan
public void setSpan(Object what, int start, int end, int flags)
From source file:com.coinblesk.client.utils.UIUtils.java
public static SpannableString toLargeSpannable(Context context, String amount, String currency, float sizeSpan) { final int amountLength = amount.length(); SpannableString result = new SpannableString(new StringBuffer(amount + " " + currency)); result.setSpan(new RelativeSizeSpan(sizeSpan), 0, amountLength, 0); result.setSpan(new ForegroundColorSpan(Color.WHITE), 0, amountLength, 0); result.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorAccent)), amountLength, result.length(), 0);//from www .jav a2s.com return result; }
From source file:com.coinblesk.client.utils.UIUtils.java
private static SpannableString coinFiatSpannable(Context context, String amountFirst, String codeFirst, String amountSecond, String codeSecond, float secondaryRelativeSize) { if (amountFirst == null) amountFirst = ""; if (codeFirst == null) codeFirst = ""; if (amountSecond == null) amountSecond = ""; if (codeSecond == null) codeSecond = ""; StringBuffer resultBuffer = new StringBuffer(); resultBuffer.append(amountFirst).append(" "); int lenFirstAmount = resultBuffer.length(); resultBuffer.append(codeFirst);//from ww w .j a va2 s. c o m int lenFirstCode = resultBuffer.length(); resultBuffer.append(" ").append(amountSecond).append(" ").append(codeSecond); int lenSecond = resultBuffer.length(); SpannableString formattedString = new SpannableString(resultBuffer); formattedString.setSpan(new ForegroundColorSpan(Color.WHITE), 0, lenFirstAmount, 0); formattedString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorAccent)), lenFirstAmount, lenFirstCode, 0); formattedString.setSpan(new RelativeSizeSpan(secondaryRelativeSize), lenFirstAmount, lenFirstCode, 0); formattedString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.main_color_400)), lenFirstCode, lenSecond, 0); formattedString.setSpan(new RelativeSizeSpan(secondaryRelativeSize), lenFirstCode, lenSecond, 0); return formattedString; }
From source file:com.coinblesk.client.utils.UIUtils.java
public static SpannableString toFriendlyAmountString(Context context, TransactionWrapper transaction) { StringBuffer friendlyAmount = new StringBuffer(); MonetaryFormat formatter = getMoneyFormat(context); String btcCode = formatter.code(); String scaledAmount = formatter.noCode().format(transaction.getAmount()).toString(); friendlyAmount.append(scaledAmount).append(" "); final int coinLength = friendlyAmount.length(); friendlyAmount.append(btcCode).append(" "); final int codeLength = friendlyAmount.length(); ExchangeRate exchangeRate = transaction.getTransaction().getExchangeRate(); if (exchangeRate != null) { Fiat fiat = exchangeRate.coinToFiat(transaction.getAmount()); friendlyAmount.append("~ " + fiat.toFriendlyString()); friendlyAmount.append(System.getProperty("line.separator") + "(1 BTC = " + exchangeRate.fiat.toFriendlyString() + " as of now)"); }//from w w w . ja v a 2 s. c o m final int amountLength = friendlyAmount.length(); SpannableString friendlySpannable = new SpannableString(friendlyAmount); friendlySpannable.setSpan(new RelativeSizeSpan(2), 0, coinLength, 0); friendlySpannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorAccent)), coinLength, codeLength, 0); friendlySpannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.main_color_400)), codeLength, amountLength, 0); return friendlySpannable; }
From source file:com.google.android.gm.ay.java
public static void a(final Context context, final String s, final String s2, final long n, final int n2) { final List<String> b = com.google.android.gm.persistence.b.DD().b(context, true); int size;//from ww w . j av a 2 s .c om if (b != null && b.size() > 0) { size = b.size(); } else { size = 1; } final String quantityString = context.getResources().getQuantityString(n2, size); final ab ab = new ab(context); if (size > 1) { final String string = context.getResources().getString(2131297166, new Object[] { s }); final SpannableString spannableString = new SpannableString( (CharSequence) String.format(quantityString, string)); final int index = spannableString.toString().indexOf(string); spannableString.setSpan((Object) new TextAppearanceSpan(context, 2131231210), index, index + string.length(), 0); ab.b((CharSequence) spannableString); ab.e((CharSequence) spannableString); } else { ab.b(quantityString); ab.e((CharSequence) quantityString); } final int hashCode = Arrays.hashCode(new Object[] { n, s2 }); final Account x = x(context, s); final Folder h = h(context, s, "^^out"); Intent a; if (h == null || s == null) { E.f(ay.mW, "Null account or folder. account: %s folder: %s", x, h); a = null; } else { a = ag.a(context, h.azZ.aPj, x); } final NotificationManager notificationManager = (NotificationManager) context .getSystemService("notification"); ab.h(17301624); ab.a(System.currentTimeMillis()); ab.c(s2); ab.a(PendingIntent.getActivity(context, -1, a, 0)); ArrayList<Integer> list = ay.aZj.get(s); if (list == null) { list = new ArrayList<Integer>(); } list.add(hashCode); ay.aZj.put(s, list); notificationManager.notify(hashCode, ab.build()); }
From source file:im.vector.notifications.NotificationUtils.java
/** * Add a text style to a notification when there are several notified rooms. * * @param context the context * @param builder the notification builder * @param roomsNotifications the rooms notifications *///from w w w.ja v a2 s .c om private static void addTextStyleWithSeveralRooms(Context context, NotificationCompat.Builder builder, RoomsNotifications roomsNotifications) { NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); for (RoomNotifications roomNotifications : roomsNotifications.mRoomNotifications) { SpannableString notifiedLine = new SpannableString(roomNotifications.mMessagesSummary); notifiedLine.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, roomNotifications.mMessageHeader.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); inboxStyle.addLine(notifiedLine); } inboxStyle.setBigContentTitle(context.getString(R.string.riot_app_name)); inboxStyle.setSummaryText(roomsNotifications.mSummaryText); builder.setStyle(inboxStyle); TaskStackBuilder stackBuilderTap = TaskStackBuilder.create(context); Intent roomIntentTap; // add the home page the activity stack stackBuilderTap.addNextIntentWithParentStack(new Intent(context, VectorHomeActivity.class)); if (roomsNotifications.mIsInvitationEvent) { // for invitation the room preview must be displayed roomIntentTap = CommonActivityUtils.buildIntentPreviewRoom(roomsNotifications.mSessionId, roomsNotifications.mRoomId, context, VectorFakeRoomPreviewActivity.class); } else { roomIntentTap = new Intent(context, VectorRoomActivity.class); roomIntentTap.putExtra(VectorRoomActivity.EXTRA_ROOM_ID, roomsNotifications.mRoomId); } // the action must be unique else the parameters are ignored roomIntentTap.setAction(TAP_TO_VIEW_ACTION + ((int) (System.currentTimeMillis()))); stackBuilderTap.addNextIntent(roomIntentTap); builder.setContentIntent(stackBuilderTap.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)); // offer to open the rooms list { Intent openIntentTap = new Intent(context, VectorHomeActivity.class); // Recreate the back stack TaskStackBuilder viewAllTask = TaskStackBuilder.create(context).addNextIntent(openIntentTap); builder.addAction(R.drawable.ic_home_black_24dp, context.getString(R.string.bottom_action_home), viewAllTask.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)); } }
From source file:cn.dreamtobe.emoji.ellipsize.helper.SpanEllipsizeEndHelper.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static CharSequence matchMaxWidth(SpannableString targetText, TextView textView) { if (targetText.length() <= 0) { return targetText; }// w w w. jav a 2 s. c o m if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { return targetText; } if (textView == null) { return targetText; } final int maxWidth = textView.getMaxWidth(); if (maxWidth <= 0 || maxWidth >= Integer.MAX_VALUE) { return targetText; } if (textView.getEllipsize() != TextUtils.TruncateAt.END) { return targetText; } //TODO Multi-lines support if (textView.getMaxLines() != 1) { return targetText; } final String maxWidthKey = getMaxWidthKey(targetText, textView); SpannableString tmpText = SPAN_MAXWIDTH_CACHE.get(maxWidthKey); if (tmpText != null) { removeClickableSpan(tmpText); return tmpText; } TextPaint textPaint = textView.getPaint(); if (textPaint == null) { return targetText; } final int totalWidth = (int) textPaint.measureText(targetText, 0, targetText.length()); if (totalWidth <= maxWidth) { return targetText; } final long startTime = System.currentTimeMillis(); // deal maxwitdh final int dotWidth = (int) textPaint.measureText("..."); tmpText = targetText; int start = 0; int end = targetText.length(); // targetX is maxWidth - "...".length int targetX = maxWidth - dotWidth; //dichotomy: get x most touch targetX int middle = targetText.length(); int x = 0; while (start <= end) { // tx = targetX, tl = targetLength // width: 0 x // length: 0 middle end // -------------|------------- middle = (start + end) / 2; int emojiDraW = 0; int emojiStrW = 0; int emojiExcursion = 1; final Object[] tmpSpans = tmpText.getSpans(0, middle, Object.class); if (tmpSpans != null) { for (Object tmpSpan : tmpSpans) { final int tmpStart = tmpText.getSpanStart(tmpSpan); final int tmpEnd = tmpText.getSpanEnd(tmpSpan); //middle in (tmpStart, tmpEnd) if (tmpStart < middle && tmpEnd > middle) { middle = tmpEnd; emojiExcursion = tmpEnd - tmpStart; } } // TextPaint#measure do not attention span, so adjust by ourselves for (Object tmpSpan : tmpSpans) { final int tmpStart = tmpText.getSpanStart(tmpSpan); final int tmpEnd = tmpText.getSpanEnd(tmpSpan); // TODO support other span if (tmpStart < middle && tmpSpan instanceof ImageSpan) { emojiDraW += ((ImageSpan) tmpSpan).getDrawable().getBounds().width(); emojiStrW += textPaint.measureText(tmpText, tmpStart, tmpEnd); } } } x = (int) textPaint.measureText(tmpText, 0, middle); x = x - emojiStrW + emojiDraW; // x = (int) (textPaint.measureText(pureStr, 0, pureStr.length()) + emojiWidth); // Log.d(TAG, String.format("targetX: %d, currentX: %d, currentLength: %d, totalLength: %d, emojiStrW[%d], emojiDraW[%d]", targetX, x, middle, targetText.length(), emojiStrW, emojiDraW)); if (x > targetX) { // width: 0 tx x // length: start tl middle end // ----|---------|------------- // TO: start | *end // ----|--------|-------------- end = middle - emojiExcursion; } else if (x < targetX) { // width: 0 x tx // length: start middle tl end // --------------|-------|------ // TO: *start | end // ---------------|------|------ start = middle + 1; } else { break; } } // adjust x larger targetX while (x > targetX && middle > 0) { x = (int) textPaint.measureText(tmpText, 0, --middle); } // adjust x middle emoji span final Object[] ajustSpans = tmpText.getSpans(0, tmpText.length(), Object.class); for (Object adjustSpan : ajustSpans) { final int adjustStart = tmpText.getSpanStart(adjustSpan); final int adjustEnd = tmpText.getSpanEnd(adjustSpan); //[adjustStart, adjustEnd) if (middle >= adjustStart && middle < adjustEnd) { middle = adjustStart - 1; break; } } // finnal middle // sub sequence [0, middle + 1) & remove [middle +1, length] spans tmpText = (SpannableString) tmpText.subSequence(0, middle + 1); // Log.d(TAG, String.format("sub Sequence[0, %d), [%s] to [%s]", middle + 1, targetText, tmpText)); // add ... final SpannableString maxWidthSS = new SpannableString(tmpText + "..."); final Object[] maxWidthSpans = tmpText.getSpans(0, tmpText.length(), Object.class); if (maxWidthSpans != null) { for (Object maxWidthSpan : maxWidthSpans) { final int mwSpanStart = tmpText.getSpanStart(maxWidthSpan); final int mwSpanEnd = tmpText.getSpanEnd(maxWidthSpan); final int mwSpanFlag = tmpText.getSpanFlags(maxWidth); maxWidthSS.setSpan(maxWidthSpan, mwSpanStart, mwSpanEnd, mwSpanFlag); } } targetText = maxWidthSS; SPAN_MAXWIDTH_CACHE.put(maxWidthKey, targetText); Log.d(TAG, String.format("deal maxWidth %d", System.currentTimeMillis() - startTime)); return targetText; }
From source file:com.todoroo.astrid.adapter.UpdateAdapter.java
private static CharSequence getLinkSpan(final AstridActivity activity, UserActivity update, String targetName, String linkColor, String linkType) { if (TASK_LINK_TYPE.equals(linkType)) { final String taskId = update.getValue(UserActivity.TARGET_ID); if (RemoteModel.isValidUuid(taskId)) { SpannableString taskSpan = new SpannableString(targetName); taskSpan.setSpan(new ClickableSpan() { @Override//from www . j a v a 2s. co m public void onClick(View widget) { if (activity != null) // TODO: This shouldn't happen, but sometimes does activity.onTaskListItemClicked(taskId); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } }, 0, targetName.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); return taskSpan; } else { return Html.fromHtml(linkify(targetName, linkColor)); } } return null; }
From source file:Main.java
public static SpannableString toSpannableString(Context context, String text, SpannableString spannableString) { int start = 0; Pattern pattern = Pattern.compile("\\\\ue[a-z0-9]{3}", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String faceText = matcher.group(); String key = faceText.substring(1); BitmapFactory.Options options = new BitmapFactory.Options(); // options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), context.getResources().getIdentifier(key, "drawable", context.getPackageName()), options); ImageSpan imageSpan = new ImageSpan(context, bitmap); int startIndex = text.indexOf(faceText, start); int endIndex = startIndex + faceText.length(); if (startIndex >= 0) spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); start = (endIndex - 1);/* w w w. j av a 2 s .c o m*/ } return spannableString; }
From source file:com.securecomcode.text.notifications.MessageNotifier.java
private static NotificationState constructNotificationState(Context context, MasterSecret masterSecret, Cursor cursor) {/*from w ww . ja v a 2 s.c om*/ NotificationState notificationState = new NotificationState(); MessageRecord record; MmsSmsDatabase.Reader reader; if (masterSecret == null) reader = DatabaseFactory.getMmsSmsDatabase(context).readerFor(cursor); else reader = DatabaseFactory.getMmsSmsDatabase(context).readerFor(cursor, masterSecret); while ((record = reader.getNext()) != null) { Recipient recipient = record.getIndividualRecipient(); Recipients recipients = record.getRecipients(); long threadId = record.getThreadId(); SpannableString body = record.getDisplayBody(); Uri image = null; Recipients threadRecipients = null; if (threadId != -1) { threadRecipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId); } // XXXX This is so fucked up. FIX ME! if (body.toString().equals(context.getString(R.string.MessageDisplayHelper_decrypting_please_wait))) { body = new SpannableString(context.getString(R.string.MessageNotifier_encrypted_message)); body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } notificationState.addNotification( new NotificationItem(recipient, recipients, threadRecipients, threadId, body, image)); } reader.close(); return notificationState; }
From source file:Main.java
/** * toSpannableString/*from ww w . java 2 s . c o m*/ * @return SpannableString * @throws */ public static SpannableString toSpannableString(Context context, String text) { if (!TextUtils.isEmpty(text)) { SpannableString spannableString = new SpannableString(text); int start = 0; Pattern pattern = Pattern.compile("\\\\ue[a-z0-9]{3}", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String faceText = matcher.group(); String key = faceText.substring(1); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), context.getResources().getIdentifier(key, "drawable", context.getPackageName()), options); ImageSpan imageSpan = new ImageSpan(context, bitmap); int startIndex = text.indexOf(faceText, start); int endIndex = startIndex + faceText.length(); if (startIndex >= 0) spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); start = (endIndex - 1); } return spannableString; } else { return new SpannableString(""); } }