Java Code Examples for android.text.style.ForegroundColorSpan

The following examples show how to use android.text.style.ForegroundColorSpan. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
protected void convert(BaseViewHolder holder, RelevantTopicBean relevantTopicBean) {
    LocalDate date = relevantTopicBean.getCreatedAt().toLocalDate();
    int year = date.getYear();
    int month = date.getMonthValue();
    int day = date.getDayOfMonth();
    if (year == OffsetDateTime.now().getYear()) {
        holder.setText(R.id.txt_date, mContext.getString(R.string.month__day, month, day));
    } else {
        SpannableString spannableTitle = SpannableString.valueOf(mContext.getString(R.string.month__day__year, month, day, year));
        spannableTitle.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.text_topic_detail_news_author)),
                spannableTitle.toString().indexOf("\n") + 1,
                spannableTitle.toString().indexOf("\n") + 5,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.setText(R.id.txt_date, spannableTitle);
    }
    holder.setText(R.id.txt_topic_trace_content, relevantTopicBean.getTitle());
    holder.setVisible(R.id.view_top_line, holder.getItemViewType() == VIEW_TYPE_TOP || holder.getItemViewType() == VIEW_TYPE_ONLY_ONE ? false : true);
    holder.setVisible(R.id.view_bottom_line, holder.getItemViewType() == VIEW_TYPE_BOTTOM || holder.getItemViewType() == VIEW_TYPE_ONLY_ONE ? false : true);
}
 
Example 2
Source Project: QuranyApp   Source File: Util.java    License: Apache License 2.0 6 votes vote down vote up
public static Spannable getSpanOfText(String text, String word) {
        Spannable spannable = new SpannableString(text);
        String REGEX = word;
        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(text);

        //    Log.d(TAG, text + "getSpanOfText: word " + word);
        while (m.find()) {
//            Log.d(TAG, "getSpanOfText: start " + m.start());
//            Log.d(TAG, "getSpanOfText: end " + m.end());
            spannable.setSpan(new ForegroundColorSpan(Color.YELLOW), m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        }
        return spannable;

    }
 
Example 3
Source Project: FimiX8-RE   Source File: ForgetIphonePasswordFragment.java    License: MIT License 6 votes vote down vote up
private SpannableString getSpannableString() {
    String str1 = this.mContext.getString(R.string.login_send_email_hint1);
    String str2 = this.mContext.getString(R.string.login_send_email_hint2);
    String str3 = this.mContext.getString(R.string.login_send_email_hint3);
    SpannableString spannableString = new SpannableString(str1 + str2 + str3);
    spannableString.setSpan(new ForegroundColorSpan(this.mContext.getResources().getColor(R.color.register_agreement)), 0, str1.length(), 33);
    spannableString.setSpan(new ForegroundColorSpan(this.mContext.getResources().getColor(R.color.register_agreement)), str1.length() + str2.length(), (str1.length() + str2.length()) + str3.length(), 33);
    spannableString.setSpan(new ClickableSpan() {
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(ForgetIphonePasswordFragment.this.getResources().getColor(R.color.register_agreement_click));
            ds.setUnderlineText(false);
        }

        public void onClick(View widget) {
        }
    }, str1.length(), str1.length() + str2.length(), 33);
    return spannableString;
}
 
Example 4
public void setText(CharSequence text, int resId, int index, int searchLength) {
    if (text == null) {
        empty = true;
        textView.setText("");
        buttonView.setVisibility(INVISIBLE);
    } else {
        if (searchLength != 0) {
            SpannableStringBuilder builder = new SpannableStringBuilder(text);
            try {
                builder.setSpan(new ForegroundColorSpan(Theme.getColor(Theme.key_windowBackgroundWhiteBlueText4)), index, index + searchLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } catch (Exception ignore) {

            }
            textView.setText(builder);
        } else {
            textView.setText(Emoji.replaceEmoji(text, textView.getPaint().getFontMetricsInt(), AndroidUtilities.dp(14), false));
        }
        if (resId != 0) {
            buttonView.setImageResource(resId);
            buttonView.setVisibility(VISIBLE);
        } else {
            buttonView.setVisibility(INVISIBLE);
        }
    }
}
 
Example 5
Source Project: FimiX8-RE   Source File: ForgetPasswordFragment.java    License: MIT License 6 votes vote down vote up
private SpannableString getEmailVerficationSpannableString() {
    String str1 = this.mContext.getString(R.string.login_email_send_hint1);
    String str2 = this.mContext.getString(R.string.login_send_email_hint2);
    SpannableString spannableString = new SpannableString(str1 + str2);
    spannableString.setSpan(new ForegroundColorSpan(this.mContext.getResources().getColor(R.color.register_agreement)), 0, str1.length(), 33);
    spannableString.setSpan(new ForegroundColorSpan(this.mContext.getResources().getColor(R.color.register_agreement)), str1.length() + str2.length(), str1.length() + str2.length(), 33);
    spannableString.setSpan(new ClickableSpan() {
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(ForgetPasswordFragment.this.getResources().getColor(R.color.register_agreement_click));
            ds.setUnderlineText(false);
        }

        public void onClick(View widget) {
        }
    }, str1.length(), str1.length() + str2.length(), 33);
    return spannableString;
}
 
Example 6
public CharSequence terminateToken(CharSequence text) {

            int i = text.length();

            while (i > 0 && text.charAt(i - 1) == ' ') {
                i--;
            }

            if (i > 0 && text.charAt(i - 1) == ' ') {
                return text;
            } else {
                // Returns colored text for selected token
                SpannableString sp = new SpannableString(String.format(formattedOfString, text));
                int textColor = ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null);
                sp.setSpan(new ForegroundColorSpan(textColor), 0, text.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                return sp;
            }
        }
 
Example 7
Source Project: styT   Source File: HighLightKeyWordUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param color   关键字颜色
 * @param text    文本
 * @param keyword 多个关键字
 * @return
 */
public static SpannableString getHighLightKeyWord(int color, String text, String[] keyword) {
    SpannableString s = new SpannableString(text);
    for (int i = 0; i < keyword.length; i++) {
        Pattern p = Pattern.compile(keyword[i]);
        Matcher m = p.matcher(s);
        while (m.find()) {
            int start = m.start();
            int end = m.end();
            s.setSpan(new ForegroundColorSpan(color), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return s;

}
 
Example 8
Source Project: android_9.0.0_r45   Source File: Html.java    License: Apache License 2.0 6 votes vote down vote up
private static void endCssStyle(Editable text) {
    Strikethrough s = getLast(text, Strikethrough.class);
    if (s != null) {
        setSpanFromMark(text, s, new StrikethroughSpan());
    }

    Background b = getLast(text, Background.class);
    if (b != null) {
        setSpanFromMark(text, b, new BackgroundColorSpan(b.mBackgroundColor));
    }

    Foreground f = getLast(text, Foreground.class);
    if (f != null) {
        setSpanFromMark(text, f, new ForegroundColorSpan(f.mForegroundColor));
    }
}
 
Example 9
Source Project: mhzs   Source File: AboutActivity.java    License: MIT License 6 votes vote down vote up
private void initData() {
    isAboutActivity = true;
    ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(this, R.color.colorAccent));

    SpannableString spannableString = new SpannableString("麻花助手当前版本:" + HYHelper.getVerisonName(this));
    spannableString.setSpan(colorSpan, 9, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    versionTextView.setText(spannableString);

    SpannableString spannableString2 = new SpannableString("支持麻花影视版本:" + Config.SUPPORT_MHYS_VERISON);
    spannableString2.setSpan(colorSpan, 9, spannableString2.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    supportVersionText.setText(spannableString2);

    SpannableString spannableString3 = new SpannableString("支持贝贝影视版本:" + Config.SUPPORT_MHYS_VERISON);
    spannableString3.setSpan(colorSpan, 9, spannableString3.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    supportBBVersionText.setText(spannableString3);
}
 
Example 10
Source Project: lttrs-android   Source File: BindingAdapters.java    License: Apache License 2.0 6 votes vote down vote up
@BindingAdapter("android:text")
public static void setText(final TextView textView, final FullEmail.From from) {
    if (from instanceof FullEmail.NamedFrom) {
        final FullEmail.NamedFrom named = (FullEmail.NamedFrom) from;
        textView.setText(named.getName());
    } else if (from instanceof FullEmail.DraftFrom) {
        final Context context = textView.getContext();
        final SpannableString spannable = new SpannableString(context.getString(R.string.draft));
        spannable.setSpan(
                new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimary)),
                0,
                spannable.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        textView.setText(spannable);
    }
}
 
Example 11
Source Project: timecat   Source File: MeechaoDataUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 文字格式化,适用但不局限于心得内容,用于处理给定内容 str 中的标签
 *
 * @param str 给定内容
 *
 * @return s
 */
public static SpannableStringBuilder covArticleContent(String str) {
    if (TextUtils.isEmpty(str)) {
        return null;
    }
    SpannableStringBuilder builder = new SpannableStringBuilder(str);
    List<String> tagList = RegexUtil.getMathcherStr(str, RegexUtil.topicValReg);
    if (null != tagList && tagList.size() != 0) {
        int fromIndex = 0;
        for (String tagVal : tagList) {
            int _start = str.indexOf(tagVal, fromIndex);
            builder.setSpan(new ForegroundColorSpan(Color.parseColor("#ffaa31")),
                    _start, _start + tagVal.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            builder.setSpan(new AbsoluteSizeSpan(0, true), _start + tagVal.indexOf("["), _start + tagVal.indexOf("]") + 1,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            fromIndex = _start + tagVal.length();
        }
    }
    return builder;
}
 
Example 12
/**
 * 拼装红色字体
 */
private SpannableStringBuilder setTextColor(String content, String input) {
    int startIndex = content.indexOf(input);
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder(content);
    if (startIndex != -1) {

        ForegroundColorSpan colorSpan
                = new ForegroundColorSpan(context.getResources().getColor(R.color.search_color));
        stringBuilder.setSpan(colorSpan,
                startIndex,
                startIndex + input.length(),
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        Log.d("few", "fwe");
    }
    return stringBuilder;
}
 
Example 13
Source Project: tysq-android   Source File: RankTipActivity.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 组装红色字体
 */
private void AssemBleText(String score, String tip, boolean isGrade){

    int scoreStartIndex = tip.indexOf(score);
    int scoreEndIndex = scoreStartIndex + score.length();
    if (!isGrade){
        scoreEndIndex += 2;
    }

    SpannableString tipSpanBuilder = new SpannableString(tip);
    ForegroundColorSpan span = new ForegroundColorSpan(ContextCompat.getColor(this, R.color.red));
    tipSpanBuilder.setSpan(span, scoreStartIndex,
            scoreEndIndex,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    tvTip.setText(tipSpanBuilder);
}
 
Example 14
public void onClickRemove(View v) {
    int style = android.os.Build.VERSION.SDK_INT >= 21 ? R.style.AlertDialogCustom : android.R.style.Theme_Holo_Dialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), style);
    SpannableString title = new SpannableString(getString(R.string.contact_remove_btn));
    title.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.ltblue)), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    SpannableString positive = new SpannableString(getString(R.string.intro_new_wallet_backup_yes));
    positive.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.ltblue)), 0, positive.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    SpannableString negative = new SpannableString(getString(R.string.intro_new_wallet_backup_no));
    negative.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.ltblue)), 0, negative.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.setTitle(title)
            .setMessage(getString(R.string.contact_remove_sure, binding.contactName.getText().toString()))
            .setPositiveButton(positive, (dialog, which) -> {
                realm.executeTransaction(realm -> {
                    RealmResults<Contact> contact = realm.where(Contact.class).equalTo("name", binding.contactName.getText().toString()).findAll();
                    contact.deleteAllFromRealm();
                });
                RxBus.get().post(new ContactRemoved(binding.contactName.getText().toString(), binding.contactAddress.getText().toString()));
                dismiss();
            })
            .setNegativeButton(negative, (dialog, which) -> {
                // do nothing which dismisses the dialog
            })
            .show();
}
 
Example 15
private CharSequence generateFastForwardOrRewindTxt(long changingTime) {

        long duration = player == null ? 0 : player.getDuration();
        String result = stringForTime(changingTime) + " / " + stringForTime(duration);

        int index = result.indexOf("/");
        SpannableString spannableString = new SpannableString(result);

        TypedValue typedValue = new TypedValue();
        TypedArray a = getContext().obtainStyledAttributes(typedValue.data, new int[]{R.attr.colorAccent});
        int color = a.getColor(0, 0);
        a.recycle();
        spannableString.setSpan(new ForegroundColorSpan(color), 0, index, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        return spannableString;
    }
 
Example 16
private CharSequence getFormattedDebugString(){
    String in=VoIPService.getSharedInstance().getDebugString();
    SpannableString ss=new SpannableString(in);

    int offset=0;
    do{
        int lineEnd=in.indexOf('\n', offset+1);
        if(lineEnd==-1)
            lineEnd=in.length();
        String line=in.substring(offset, lineEnd);
        if(line.contains("IN_USE")){
            ss.setSpan(new ForegroundColorSpan(0xFF00FF00), offset, lineEnd, 0);
        }else{
            if(line.contains(": ")){
                ss.setSpan(new ForegroundColorSpan(0xAAFFFFFF), offset, offset+line.indexOf(':')+1, 0);
            }
        }
    }while((offset=in.indexOf('\n', offset+1))!=-1);

    return ss;
}
 
Example 17
public static void colorizeSpannableGreen(String prependString, Spannable s, Context context) {
    if (context == null) {
        return;
    }
    int offset = prependString.length();
    if (s.length() > 0) {
        s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.green_light)), 0, s.length() > 10 ? 11 + offset : s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (s.length() > 58) {
            s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.white_90)), 11 + offset, 58 + offset, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.green_light)), 58 + offset, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 18
public SpannableString buildSpannableString() {
  SpannableStringBuilder spannableStringBuilder =
      new SpannableStringBuilder(captionStringBuilder);
  int length = spannableStringBuilder.length();

  if (length > 0) {
    if (italicsStartPosition != C.POSITION_UNSET) {
      spannableStringBuilder.setSpan(new StyleSpan(Typeface.ITALIC), italicsStartPosition,
          length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (underlineStartPosition != C.POSITION_UNSET) {
      spannableStringBuilder.setSpan(new UnderlineSpan(), underlineStartPosition,
          length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (foregroundColorStartPosition != C.POSITION_UNSET) {
      spannableStringBuilder.setSpan(new ForegroundColorSpan(foregroundColor),
          foregroundColorStartPosition, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (backgroundColorStartPosition != C.POSITION_UNSET) {
      spannableStringBuilder.setSpan(new BackgroundColorSpan(backgroundColor),
          backgroundColorStartPosition, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
  }

  return new SpannableString(spannableStringBuilder);
}
 
Example 19
Source Project: MediaSDK   Source File: Tx3gDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private static void attachColor(SpannableStringBuilder cueText, int colorRgba,
    int defaultColorRgba, int start, int end, int spanPriority) {
  if (colorRgba != defaultColorRgba) {
    int colorArgb = ((colorRgba & 0xFF) << 24) | (colorRgba >>> 8);
    cueText.setSpan(new ForegroundColorSpan(colorArgb), start, end,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | spanPriority);
  }
}
 
Example 20
Source Project: MediaSDK   Source File: Cea708Decoder.java    License: Apache License 2.0 5 votes vote down vote up
public void setPenColor(int foregroundColor, int backgroundColor, int edgeColor) {
  if (foregroundColorStartPosition != C.POSITION_UNSET) {
    if (this.foregroundColor != foregroundColor) {
      captionStringBuilder.setSpan(new ForegroundColorSpan(this.foregroundColor),
          foregroundColorStartPosition, captionStringBuilder.length(),
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
  }
  if (foregroundColor != COLOR_SOLID_WHITE) {
    foregroundColorStartPosition = captionStringBuilder.length();
    this.foregroundColor = foregroundColor;
  }

  if (backgroundColorStartPosition != C.POSITION_UNSET) {
    if (this.backgroundColor != backgroundColor) {
      captionStringBuilder.setSpan(new BackgroundColorSpan(this.backgroundColor),
          backgroundColorStartPosition, captionStringBuilder.length(),
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
  }
  if (backgroundColor != COLOR_SOLID_BLACK) {
    backgroundColorStartPosition = captionStringBuilder.length();
    this.backgroundColor = backgroundColor;
  }

  // TODO: Add support for edge color.
}
 
Example 21
Source Project: MediaSDK   Source File: Cea708Decoder.java    License: Apache License 2.0 5 votes vote down vote up
public SpannableString buildSpannableString() {
  SpannableStringBuilder spannableStringBuilder =
      new SpannableStringBuilder(captionStringBuilder);
  int length = spannableStringBuilder.length();

  if (length > 0) {
    if (italicsStartPosition != C.POSITION_UNSET) {
      spannableStringBuilder.setSpan(new StyleSpan(Typeface.ITALIC), italicsStartPosition,
          length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (underlineStartPosition != C.POSITION_UNSET) {
      spannableStringBuilder.setSpan(new UnderlineSpan(), underlineStartPosition,
          length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (foregroundColorStartPosition != C.POSITION_UNSET) {
      spannableStringBuilder.setSpan(new ForegroundColorSpan(foregroundColor),
          foregroundColorStartPosition, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (backgroundColorStartPosition != C.POSITION_UNSET) {
      spannableStringBuilder.setSpan(new BackgroundColorSpan(backgroundColor),
          backgroundColorStartPosition, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
  }

  return new SpannableString(spannableStringBuilder);
}
 
Example 22
Source Project: MediaSDK   Source File: Cea608Decoder.java    License: Apache License 2.0 5 votes vote down vote up
private static void setColorSpan(
    SpannableStringBuilder builder, int start, int end, int color) {
  if (color == Color.WHITE) {
    // White is treated as the default color (i.e. no span is attached).
    return;
  }
  builder.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
 
Example 23
Source Project: DoubleDatePicker   Source File: DoubleDateSelectDialog.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 格式化显示的数据,必须返回SpannableString对象
 *
 * @param priFix  前缀
 * @param content 内容
 * @return 返回格式化的数据
 */
private SpannableString makeFormatContent(String priFix, String content) {
    SpannableString spannableString = new SpannableString(priFix + content);
    spannableString.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.black33)),
            priFix.length(), spannableString.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    spannableString.setSpan(new RelativeSizeSpan(1.33f),
            priFix.length(), spannableString.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    return spannableString;
}
 
Example 24
Source Project: Wifi-Connect   Source File: CustomDialog.java    License: Apache License 2.0 5 votes vote down vote up
public static SpannableString getSpannedText(Context context, float textSize, String text) {
    SpannableString spannableString = new SpannableString(text);
    if(text.indexOf("\n") != -1) {
        spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.activity_login_edittext_txt)),
                text.indexOf("\n"), text.length(), 0);
        spannableString.setSpan(new RelativeSizeSpan(textSize), text.indexOf("\n"), text.length(), 0);
    }

    return spannableString;
}
 
Example 25
Source Project: 1Rramp-Android   Source File: WinnerFeedItemView.java    License: MIT License 5 votes vote down vote up
private void setPrize() {
  String part1 = "Prize ";
  String part2 = String.format(Locale.US, "%s", mData.getPrize());
  int spanStart = part1.length();
  int spanEnd = spanStart + part2.length();
  Spannable wordtoSpan = new SpannableString(part1 + part2);
  wordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#3F72AF")),
    spanStart,
    spanEnd,
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  prizeWon.setText(wordtoSpan);
}
 
Example 26
Source Project: arcusandroid   Source File: RuleTemplateView.java    License: Apache License 2.0 5 votes vote down vote up
public void setRuleTemplate (@NonNull List<TemplateTextField> fields, OnTemplateFieldClickListener listener) {

        this.setText("");

        for (TemplateTextField thisField : fields) {

            // Capitalize proper nouns; lowercase all other editable fields.
            String displayText = thisField.getText();
            if (thisField.isEditable()) {
                displayText = thisField.isProperName() ? WordUtils.capitalizeFully(displayText) : displayText.toLowerCase();
            }

            SpannableStringBuilder span = SpannableStringBuilder.valueOf(displayText);

            if (thisField.isEditable()) {
                span.setSpan(new EditableSpan(thisField, listener), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            int textColor = thisField.isEditable() ? Color.BLACK : Color.GRAY;
            span.setSpan(new ForegroundColorSpan(textColor), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            this.append(span);
        }

        this.setHighlightColor(Color.TRANSPARENT);

        if (enabled) {
            this.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
 
Example 27
Source Project: mcumgr-android   Source File: StatsFragment.java    License: Apache License 2.0 5 votes vote down vote up
private void printError(@Nullable final String error) {
    if (error != null) {
        final SpannableString spannable = new SpannableString(error);
        TypedValue typedValue = new TypedValue();
        spannable.setSpan(new ForegroundColorSpan(
                        ContextCompat.getColor(requireContext(), R.color.colorError)),
                0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new StyleSpan(Typeface.BOLD),
                0, error.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mError.setText(spannable);
        mError.setVisibility(View.VISIBLE);
    } else {
        mError.setText(null);
    }
}
 
Example 28
Source Project: OpenWeatherPlus-Android   Source File: SearchAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder myViewHolder, @SuppressLint("RecyclerView") final int i) {
    MyViewHolder viewHolder = (MyViewHolder) myViewHolder;
    View itemView = viewHolder.itemView;
    String name = data.get(i).getCityName();
    int x = name.indexOf("-");
    String parentCity = name.substring(0, x);
    String location = name.substring(x + 1);

    String cityName = location + "," + parentCity + "," + data.get(i).getAdminArea() + "," + data.get(i).getCnty();
    if (TextUtils.isEmpty(data.get(i).getAdminArea())) {
        cityName = location + "," + parentCity + "," + data.get(i).getCnty();
    }
    if (!TextUtils.isEmpty(cityName)) {
        viewHolder.tvCity.setText(cityName);
        if (cityName.contains(searchText)) {
            int index = cityName.indexOf(searchText);
            //创建一个 SpannableString对象
            SpannableString sp = new SpannableString(cityName);
            //设置高亮样式一
            sp.setSpan(new ForegroundColorSpan(activity.getResources().getColor(R.color.light_text_color)), index, index + searchText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            viewHolder.tvCity.setText(sp);
        }
    }

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String cid = data.get(i).getCityId();
            if (lang.equals(Lang.CHINESE_SIMPLIFIED)) {
                saveData(Lang.ENGLISH, "cityBeanEn", cid);
                saveBean("cityBean", cid, i);
            } else {
                saveData(Lang.CHINESE_SIMPLIFIED, "cityBean", cid);
                saveBean("cityBeanEn", cid, i);
            }
            activity.onBackPressed();
        }
    });
}
 
Example 29
Source Project: SimpleBluetoothLeTerminal   Source File: TerminalFragment.java    License: MIT License 5 votes vote down vote up
private void send(String str) {
    if(connected != Connected.True) {
        Toast.makeText(getActivity(), "not connected", Toast.LENGTH_SHORT).show();
        return;
    }
    try {
        SpannableStringBuilder spn = new SpannableStringBuilder(str+'\n');
        spn.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorSendText)), 0, spn.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        receiveText.append(spn);
        byte[] data = (str + newline).getBytes();
        service.write(data);
    } catch (Exception e) {
        onSerialIoError(e);
    }
}
 
Example 30
/**
 * Replace occurences of NANO with blue text
 *
 * @param s       Spannable
 * @param context Context
 */
public static Spannable colorizeNano(String s, Context context) {
    Spannable sp = new SpannableString(s);
    if (context == null || !s.toUpperCase().contains("NANO")) {
        return sp;
    }
    int indexStart = s.toUpperCase().indexOf("NANO");
    int indexEnd = 4;
    if (indexStart < 0) {
        return sp;
    }
    sp.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.ltblue)), indexStart, indexStart + indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sp;
}