List of usage examples for android.text Spannable length
int length();
From source file:org.onebusaway.android.report.ui.Open311ProblemFragment.java
/** * Dynamically creates radio buttons/*from w ww. j a va 2 s .c o m*/ * * @param open311Attribute contains the open311 attributes */ private void createSingleValueList(Open311Attribute open311Attribute) { ArrayList<Object> values = (ArrayList<Object>) open311Attribute.getValues(); if (values != null && values.size() > 0) { LayoutInflater inflater = LayoutInflater.from(getActivity()); RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.report_issue_single_value_list_item, null, false); layout.setSaveEnabled(true); ((ImageView) layout.findViewById(R.id.ri_ic_radio)) .setColorFilter(getResources().getColor(R.color.material_gray)); Spannable word = new SpannableString(open311Attribute.getDescription()); ((TextView) layout.findViewById(R.id.risvli_textView)).setText(word); if (open311Attribute.getRequired()) { Spannable wordTwo = new SpannableString(" *Required"); wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); ((TextView) layout.findViewById(R.id.risvli_textView)).append(wordTwo); } RadioGroup rg = (RadioGroup) layout.findViewById(R.id.risvli_radioGroup); rg.setOrientation(RadioGroup.VERTICAL); // Restore view state from attribute result hash map AttributeValue av = mAttributeValueHashMap.get(open311Attribute.getCode()); String entryValue = null; if (av != null) { entryValue = av.getSingleValue(); } for (int i = 0; i < values.size(); i++) { LinkedHashMap<String, String> value = (LinkedHashMap<String, String>) values.get(i); RadioButton rb = new RadioButton(getActivity()); rg.addView(rb); //the RadioButtons are added to the radioGroup instead of the layout String attributeKey = ""; String attributeValue = ""; for (LinkedHashMap.Entry<String, String> entry : value.entrySet()) { if (Open311Attribute.NAME.equals(entry.getKey())) { rb.setText(entry.getValue()); if (entryValue != null && entryValue.equalsIgnoreCase(entry.getValue())) { rb.setChecked(true); } attributeKey = open311Attribute.getCode() + entry.getValue(); } else if (Open311Attribute.KEY.equals(entry.getKey())) { attributeValue = entry.getValue(); } } mOpen311AttributeKeyNameMap.put(attributeKey, attributeValue); } mInfoLayout.addView(layout); mDynamicAttributeUIMap.put(open311Attribute.getCode(), rg); } }
From source file:org.chromium.chrome.browser.omnibox.SuggestionView.java
private boolean applyHighlightToMatchRegions(Spannable str, List<MatchClassification> classifications) { boolean hasMatch = false; for (int i = 0; i < classifications.size(); i++) { MatchClassification classification = classifications.get(i); if ((classification.style & MatchClassificationStyle.MATCH) == MatchClassificationStyle.MATCH) { int matchStartIndex = classification.offset; int matchEndIndex; if (i == classifications.size() - 1) { matchEndIndex = str.length(); } else { matchEndIndex = classifications.get(i + 1).offset; }/*from w ww .j av a 2 s. co m*/ matchStartIndex = Math.min(matchStartIndex, str.length()); matchEndIndex = Math.min(matchEndIndex, str.length()); hasMatch = true; // Bold the part of the URL that matches the user query. str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), matchStartIndex, matchEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return hasMatch; }
From source file:org.proninyaroslav.libretorrent.fragments.DetailTorrentFragment.java
@Override public void onShow(final AlertDialog dialog) { if (dialog == null) { return;//w w w .jav a2s. co m } if (getFragmentManager().findFragmentByTag(TAG_ADD_TRACKERS_DIALOG) != null) { final TextInputEditText field = (TextInputEditText) dialog .findViewById(R.id.multiline_text_input_dialog); final TextInputLayout fieldLayout = (TextInputLayout) dialog .findViewById(R.id.layout_multiline_text_input_dialog); /* Dismiss error label if user has changed the text */ if (field != null && fieldLayout != null) { field.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* Nothing */ } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { fieldLayout.setErrorEnabled(false); fieldLayout.setError(null); /* Clear selection of invalid url */ Spannable text = field.getText(); ForegroundColorSpan[] errorSpans = text.getSpans(0, text.length(), ForegroundColorSpan.class); for (ForegroundColorSpan span : errorSpans) { text.removeSpan(span); } } @Override public void afterTextChanged(Editable s) { /* Nothing */ } }); } /* * It is necessary in order to the dialog is not closed by * pressing add/replace button if the text checker gave a false result */ Button addButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); Button replaceButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE); addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (field != null && fieldLayout != null) { String text = field.getText().toString(); List<String> urls = Arrays.asList(text.split(Utils.getLineSeparator())); if (checkEditTextField(urls, fieldLayout, field)) { addTrackersRequest(new ArrayList<>(urls), false); dialog.dismiss(); } } } }); replaceButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (field != null && fieldLayout != null) { String text = field.getText().toString(); List<String> urls = Arrays.asList(text.split(Utils.getLineSeparator())); if (checkEditTextField(urls, fieldLayout, field)) { addTrackersRequest(new ArrayList<>(urls), true); dialog.dismiss(); } } } }); /* Inserting links from the clipboard */ String clipboard = Utils.getClipboard(activity.getApplicationContext()); if (clipboard != null && field != null) { List<String> urls = Arrays.asList(clipboard.split(Utils.getLineSeparator())); ArrayList<String> validUrls = new ArrayList<>(); for (String url : urls) { if (Utils.isValidTrackerUrl(url)) { validUrls.add(url); } } field.setText(TextUtils.join(Utils.getLineSeparator(), validUrls)); } } else if (getFragmentManager().findFragmentByTag(TAG_SPEED_LIMIT_DIALOG) != null) { TextInputEditText upload = (TextInputEditText) dialog.findViewById(R.id.upload_limit); TextInputEditText download = (TextInputEditText) dialog.findViewById(R.id.download_limit); if (upload != null && download != null) { int minSpeedLimit = 0; int maxSpeedLimit = Integer.MAX_VALUE; InputFilter[] filter = new InputFilter[] { new InputFilterMinMax(minSpeedLimit, maxSpeedLimit) }; upload.setFilters(filter); if (TextUtils.isEmpty(upload.getText())) { upload.setText((uploadSpeedLimit != -1 ? Integer.toString(uploadSpeedLimit / 1024) : Integer.toString(minSpeedLimit))); } download.setFilters(filter); if (TextUtils.isEmpty(download.getText())) { download.setText((downloadSpeedLimit != -1 ? Integer.toString(downloadSpeedLimit / 1024) : Integer.toString(minSpeedLimit))); } } } }
From source file:com.msopentech.applicationgateway.EnterpriseBrowserActivity.java
public void showSettingsPopup(View v) { try {//from w w w . j a v a2s . c o m PopupMenu popup = new PopupMenu(this, v); MenuInflater inflater = popup.getMenuInflater(); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.menu_connection: { showAgentsList(); return true; } case R.id.menu_sign_in_or_out: { if (mIsSigninRequired) { // Menu item in sign in mode. //TODO: showSignIn(null, true); return true; } // Clean up the session. mTraits.sessionID = null; mTraits.token = null; mTraits.agent = null; // Indicate the condition. mIsSigninRequired = true; //Remove all tabs except one. mCustomTabHost.resetTabs(); mUrlEditTextView.setText(""); mProgressBarView.setProgress(0); // Disable the reload button and set the right image for it. mReloadButtonView.setEnabled(false); mReloadButtonView.setImageResource(R.drawable.reload_button); mStatusButtonView.setImageResource(R.drawable.connection_red); // Since it's the user's intention to sign out, his // history must be dropped. PersistenceManager.dropContent(PersistenceManager.ContentType.HISTORY); return true; } // Removed from RELEASE version. Should be active for development ONLY. // case R.id.menu_advanced_router_settings: { // showAdvancedRouterSettings(null); // } default: { return false; } } } }); inflater.inflate(R.menu.settings_menu, popup.getMenu()); if (mIsSigninRequired) { MenuItem signItem = popup.getMenu().findItem(R.id.menu_sign_in_or_out); signItem.setTitle(getResources().getString(R.string.browser_menu_item_sign_in)); } // Assumes that agent always has a display name. If display name is not present agent is considered to be not connected. MenuItem agentItem = popup.getMenu().findItem(R.id.menu_connection); String status = getResources().getString(R.string.browser_menu_item_choose_connection_not_connected); if (mTraits != null && mTraits.agent != null && !TextUtils.isEmpty(mTraits.agent.getDisplayName())) { status = mTraits.agent.getDisplayName(); } Spannable span = new SpannableString( getResources().getString(R.string.browser_menu_item_choose_connection, status)); span.setSpan(new RelativeSizeSpan(0.8f), 18, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(new ForegroundColorSpan(Color.GRAY), 18, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); agentItem.setTitle(span); popup.show(); } catch (final Exception e) { Utility.showAlertDialog(EnterpriseBrowserActivity.class.getSimpleName() + ".showSettingsPopup(): Failed. " + e.toString(), EnterpriseBrowserActivity.this); } }
From source file:cgeo.geocaching.CacheDetailActivity.java
private static void fixTextColor(final Spannable spannable, final int backgroundColor) { final ForegroundColorSpan[] spans = spannable.getSpans(0, spannable.length(), ForegroundColorSpan.class); for (final ForegroundColorSpan span : spans) { if (ColorUtils.getContrastRatio(span.getForegroundColor(), backgroundColor) < CONTRAST_THRESHOLD) { final int start = spannable.getSpanStart(span); final int end = spannable.getSpanEnd(span); // Assuming that backgroundColor can be either white or black, // this will set opposite background color (white for black and black for white) spannable.setSpan(new BackgroundColorSpan(backgroundColor ^ 0x00ffffff), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }/*from w ww . jav a 2s.co m*/ } }
From source file:com.orange.ocara.ui.activity.BaseActivityManagingAudit.java
public void showAuditObjectProgress(AuditObject auditObject, final boolean terminateActivityWhenDone) { CharSequence info = getText(com.orange.ocara.R.string.auditing_progress_info); Spannable auditingStatus = new SpannableString(""); int color = getResources().getColor(com.orange.ocara.R.color.black); switch (auditObject.getResponse()) { case OK://ww w. ja v a2s . co m auditingStatus = new SpannableString(getText(com.orange.ocara.R.string.auditing_progress_status_ok)); color = getResources().getColor(com.orange.ocara.R.color.green); break; case NOK: if (auditObject.hasAtLeastOneBlockingRule()) { auditingStatus = new SpannableString( getText(com.orange.ocara.R.string.auditing_progress_status_nok)); color = getResources().getColor(com.orange.ocara.R.color.red); } else { auditingStatus = new SpannableString( getText(com.orange.ocara.R.string.auditing_progress_status_anoying)); color = getResources().getColor(com.orange.ocara.R.color.orange); } break; case DOUBT: auditingStatus = new SpannableString(getText(com.orange.ocara.R.string.auditing_progress_status_doubt)); color = getResources().getColor(com.orange.ocara.R.color.yellow); break; case NoAnswer: auditingStatus = new SpannableString( getText(com.orange.ocara.R.string.auditing_progress_status_no_answer)); color = getResources().getColor(com.orange.ocara.R.color.blue); break; } auditingStatus.setSpan(new ForegroundColorSpan(color), info.length(), auditingStatus.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); StringBuffer stringBuffer = new StringBuffer(info); stringBuffer.append("<br>").append(auditingStatus); // get application preference to know if he wants to audit object now SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); int displayAuditingProgessDialog = Integer.parseInt(sharedPreferences .getString(getString(com.orange.ocara.R.string.setting_display_auditing_progress_key), "1")); switch (displayAuditingProgessDialog) { case 1: final NotificationDialogBuilder dialogBuilder = new NotificationDialogBuilder(this); final AlertDialog dialog = dialogBuilder.setInfo(auditingStatus) .setOption(getString(com.orange.ocara.R.string.auditing_progress_option)).setCancelable(false) .setTitle(com.orange.ocara.R.string.auditing_progress_title) .setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { auditingProgressDismiss(dialogBuilder.getOptionValue()); if (terminateActivityWhenDone) { BaseActivityManagingAudit.this.finish(); } } }).setPositiveButton(com.orange.ocara.R.string.action_close, null).create(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { dialog.dismiss(); } }, 3500); dialog.show(); break; default: if (terminateActivityWhenDone) { BaseActivityManagingAudit.this.finish(); } break; } }
From source file:com.android.mail.browse.ConversationItemView.java
private void createSubject(final boolean isUnread) { final String badgeText = mHeader.badgeText == null ? "" : mHeader.badgeText; String subject = filterTag(getContext(), mHeader.conversation.subject); subject = mAdapter.getBidiFormatter().unicodeWrap(subject); subject = Conversation.getSubjectForDisplay(mContext, badgeText, subject); final Spannable displayedStringBuilder = new SpannableString(subject); // since spans affect text metrics, add spans to the string before measure/layout or eliding final int badgeTextLength = formatBadgeText(displayedStringBuilder, badgeText); if (!TextUtils.isEmpty(subject)) { displayedStringBuilder.setSpan(/*from w w w . j av a 2 s . co m*/ TextAppearanceSpan.wrap(isUnread ? sSubjectTextUnreadSpan : sSubjectTextReadSpan), badgeTextLength, subject.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (isActivated() && showActivatedText()) { displayedStringBuilder.setSpan(sActivatedTextSpan, badgeTextLength, displayedStringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); } final int subjectWidth = mCoordinates.subjectWidth; final int subjectHeight = mCoordinates.subjectHeight; mSubjectTextView.setLayoutParams(new ViewGroup.LayoutParams(subjectWidth, subjectHeight)); mSubjectTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mCoordinates.subjectFontSize); layoutViewExactly(mSubjectTextView, subjectWidth, subjectHeight); mSubjectTextView.setText(displayedStringBuilder); }
From source file:android.support.text.emoji.EmojiProcessor.java
/** * Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found. * <p>// w w w. j a v a 2 s.c om * <ul> * <li>If no emojis are found, {@code charSequence} given as the input is returned without * any changes. i.e. charSequence is a String, and no emojis are found, the same String is * returned.</li> * <li>If the given input is not a Spannable (such as String), and at least one emoji is found * a new {@link android.text.Spannable} instance is returned. </li> * <li>If the given input is a Spannable, the same instance is returned. </li> * </ul> * * @param charSequence CharSequence to add the EmojiSpans, cannot be {@code null} * @param start start index in the charSequence to look for emojis, should be greater than or * equal to {@code 0}, also less than {@code charSequence.length()} * @param end end index in the charSequence to look for emojis, should be greater than or * equal to {@code start} parameter, also less than {@code charSequence.length()} * @param maxEmojiCount maximum number of emojis in the {@code charSequence}, should be greater * than or equal to {@code 0} * @param replaceAll whether to replace all emoji with {@link EmojiSpan}s */ CharSequence process(@NonNull final CharSequence charSequence, @IntRange(from = 0) int start, @IntRange(from = 0) int end, @IntRange(from = 0) int maxEmojiCount, final boolean replaceAll) { final boolean isSpannableBuilder = charSequence instanceof SpannableBuilder; if (isSpannableBuilder) { ((SpannableBuilder) charSequence).beginBatchEdit(); } try { Spannable spannable = null; // if it is a spannable already, use the same instance to add/remove EmojiSpans. // otherwise wait until the the first EmojiSpan found in order to change the result // into a Spannable. if (isSpannableBuilder || charSequence instanceof Spannable) { spannable = (Spannable) charSequence; } if (spannable != null) { final EmojiSpan[] spans = spannable.getSpans(start, end, EmojiSpan.class); if (spans != null && spans.length > 0) { // remove existing spans, and realign the start, end according to spans // if start or end is in the middle of an emoji they should be aligned final int length = spans.length; for (int index = 0; index < length; index++) { final EmojiSpan span = spans[index]; final int spanStart = spannable.getSpanStart(span); final int spanEnd = spannable.getSpanEnd(span); // Remove span only when its spanStart is NOT equal to current end. // During add operation an emoji at index 0 is added with 0-1 as start and // end indices. Therefore if there are emoji spans at [0-1] and [1-2] // and end is 1, the span between 0-1 should be deleted, not 1-2. if (spanStart != end) { spannable.removeSpan(span); } start = Math.min(spanStart, start); end = Math.max(spanEnd, end); } } } if (start == end || start >= charSequence.length()) { return charSequence; } // calculate max number of emojis that can be added. since getSpans call is a relatively // expensive operation, do it only when maxEmojiCount is not unlimited. if (maxEmojiCount != EmojiCompat.EMOJI_COUNT_UNLIMITED && spannable != null) { maxEmojiCount -= spannable.getSpans(0, spannable.length(), EmojiSpan.class).length; } // add new ones int addedCount = 0; final ProcessorSm sm = new ProcessorSm(mMetadataRepo.getRootNode()); int currentOffset = start; int codePoint = Character.codePointAt(charSequence, currentOffset); while (currentOffset < end && addedCount < maxEmojiCount) { final int action = sm.check(codePoint); switch (action) { case ACTION_ADVANCE_BOTH: start += Character.charCount(Character.codePointAt(charSequence, start)); currentOffset = start; if (currentOffset < end) { codePoint = Character.codePointAt(charSequence, currentOffset); } break; case ACTION_ADVANCE_END: currentOffset += Character.charCount(codePoint); if (currentOffset < end) { codePoint = Character.codePointAt(charSequence, currentOffset); } break; case ACTION_FLUSH: if (replaceAll || !hasGlyph(charSequence, start, currentOffset, sm.getFlushMetadata())) { if (spannable == null) { spannable = new SpannableString(charSequence); } addEmoji(spannable, sm.getFlushMetadata(), start, currentOffset); addedCount++; } start = currentOffset; break; } } // After the last codepoint is consumed the state machine might be in a state where it // identified an emoji before. i.e. abc[women-emoji] when the last codepoint is consumed // state machine is waiting to see if there is an emoji sequence (i.e. ZWJ). // Need to check if it is in such a state. if (sm.isInFlushableState() && addedCount < maxEmojiCount) { if (replaceAll || !hasGlyph(charSequence, start, currentOffset, sm.getCurrentMetadata())) { if (spannable == null) { spannable = new SpannableString(charSequence); } addEmoji(spannable, sm.getCurrentMetadata(), start, currentOffset); addedCount++; } } return spannable == null ? charSequence : spannable; } finally { if (isSpannableBuilder) { ((SpannableBuilder) charSequence).endBatchEdit(); } } }
From source file:com.tct.mail.browse.ConversationItemView.java
private void createSubject(final boolean isUnread) { final String badgeText = mHeader.badgeText == null ? "" : mHeader.badgeText; String subject = filterTag(getContext(), mHeader.conversation.subject); subject = Conversation.getSubjectForDisplay(mContext, badgeText, subject); /// TCT: add for search term highlight // process subject and snippet respectively @{ SpannableStringBuilder subjectToHighlight = new SpannableStringBuilder(subject); boolean hasFilter = (mSearchParams != null && !TextUtils.isEmpty(mSearchParams.mFilter)); if (hasFilter) { boolean fieldMatchedSubject = (mSearchParams != null && (SearchParams.SEARCH_FIELD_SUBJECT.equals(mSearchParams.mField) || SearchParams.SEARCH_FIELD_ALL.equals(mSearchParams.mField))); /// TCT: Only highlight un-empty subject if (fieldMatchedSubject && !TextUtils.isEmpty(subject)) { CharSequence subjectChars = TextUtilities.highlightTermsInText(subject, mSearchParams.mFilter); subjectToHighlight.replace(0, subject.length(), subjectChars); }// w ww . j av a 2 s. c o m } /// @} final Spannable displayedStringBuilder = new SpannableString(subjectToHighlight); // since spans affect text metrics, add spans to the string before measure/layout or fancy // ellipsizing final int badgeTextLength = formatBadgeText(displayedStringBuilder, badgeText); if (!TextUtils.isEmpty(subject)) { displayedStringBuilder.setSpan( TextAppearanceSpan.wrap(isUnread ? sSubjectTextUnreadSpan : sSubjectTextReadSpan), badgeTextLength, subject.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } if (isActivated() && showActivatedText()) { displayedStringBuilder.setSpan(sActivatedTextSpan, badgeTextLength, displayedStringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); } final int subjectWidth = mSubjectWidth;//TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD final int subjectHeight = mCoordinates.subjectHeight; mSubjectTextView.setLayoutParams(new ViewGroup.LayoutParams(subjectWidth, subjectHeight)); mSubjectTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mCoordinates.subjectFontSize); layoutViewExactly(mSubjectTextView, subjectWidth, subjectHeight); //[FEATURE]-Mod-BEGIN by CDTS.zhonghua.tuo,05/29/2014,FR 670064 SpannableStringBuilder builder = new SpannableStringBuilder(); boolean filterSubject = false; if (mField == UIProvider.LOCAL_SEARCH_ALL || mField == UIProvider.LOCAL_SEARCH_SUBJECT) { filterSubject = true; } if (mQueryText != null && filterSubject) { CharSequence formatSubject = displayedStringBuilder; formatSubject = TextUtilities.highlightTermsInText(subject, mQueryText); builder.append(formatSubject); mSubjectTextView.setText(builder); // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_S //store the displayed subject for calculate the statusView's X and width mHeader.subjectText = builder.toString(); // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_E } else { mSubjectTextView.setText(displayedStringBuilder); // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_S mHeader.subjectText = displayedStringBuilder.toString(); // TS: chao.zhang 2015-09-14 EMAIL FEATURE-585337 ADD_E } //[FEATURE]-Mod-END by CDTS.zhonghua.tuo }