use of android.text.style.ForegroundColorSpan in project Mvp-Rxjava-Retrofit-dagger2 by pengMaster.
the class StringUtils method getHighLightText.
// public final static String UTF_8 = "utf-8";
/**
* 返回一个高亮spannable
*
* @param content 文本内容
* @param color 高亮颜色
* @param start 起始位置
* @param end 结束位置
* @return 高亮spannable
*/
public static CharSequence getHighLightText(String content, int color, int start, int end) {
if (TextUtils.isEmpty(content)) {
return "";
}
start = start >= 0 ? start : 0;
end = end <= content.length() ? end : content.length();
SpannableString spannable = new SpannableString(content);
CharacterStyle span = new ForegroundColorSpan(color);
spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable;
}
use of android.text.style.ForegroundColorSpan in project Shuttle by timusus.
the class PrefixHighlighter method apply.
/**
* Returns a {@link CharSequence} which highlights the given prefix if found
* in the given text
*
* @param text the text to which to apply the highlight
* @param prefix the prefix to look for
*/
private CharSequence apply(CharSequence text, char[] prefix) {
final int index = indexOfWordPrefix(text, prefix);
if (index != -1) {
if (mPrefixColorSpan == null) {
mPrefixColorSpan = new ForegroundColorSpan(mPrefixHighlightColor);
}
final SpannableString result = new SpannableString(text);
result.setSpan(mPrefixColorSpan, index, index + prefix.length, 0);
return result;
} else {
return text;
}
}
use of android.text.style.ForegroundColorSpan in project TinyKeePass by sorz.
the class EntryRecyclerViewAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Entry entry = entries.get(position);
holder.view.setSelected(selectedItem == position);
holder.imageIcon.setImageBitmap(getIcon(entry));
holder.textTitle.setText(parse(entry.getTitle()));
holder.textUsername.setText(parse(entry.getUsername()));
String url = parse(entry.getUrl()).replaceFirst("https?://(www\\.)?", "");
String[] hostnamePath = url.split("/", 2);
holder.textUrlHostname.setText(hostnamePath[0]);
holder.textUrlPath.setText(hostnamePath.length > 1 && !hostnamePath[1].isEmpty() ? "/" + hostnamePath[1] : "");
holder.textPassword.setText("");
if (position == passwordShownItem && entry.getPassword() != null && !entry.getPassword().isEmpty()) {
SpannableStringBuilder builder = new SpannableStringBuilder();
int[] textColors = { context.getColor(R.color.password1), context.getColor(R.color.password2) };
int[] backgroundColors = { context.getColor(R.color.passwordBackground1), context.getColor(R.color.passwordBackground2) };
int colorIndex = 0;
for (char c : entry.getPassword().toCharArray()) {
builder.append(c);
if (builder.length() >= PASSWORD_NUM_OF_CHARS_IN_GROUP || holder.textPassword.length() + builder.length() >= entry.getPassword().length()) {
builder.setSpan(new BackgroundColorSpan(backgroundColors[colorIndex]), 0, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(new ForegroundColorSpan(textColors[colorIndex]), 0, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.textPassword.append(builder);
builder.clear();
colorIndex = (colorIndex + 1) % textColors.length;
}
}
}
showTextViewOnlyIfNotEmpty(holder.textUsername);
showTextViewOnlyIfNotEmpty(holder.textUrlHostname);
showTextViewOnlyIfNotEmpty(holder.textUrlPath);
showTextViewOnlyIfNotEmpty(holder.textPassword);
if (onClickHandler != null)
holder.view.setOnClickListener(v -> onClickHandler.accept(v, entry));
if (onLongClickHandler != null)
holder.view.setOnLongClickListener(v -> {
setSelectedItem(position);
return onLongClickHandler.test(v, entry);
});
}
use of android.text.style.ForegroundColorSpan in project bitcoin-wallet by bitcoin-wallet.
the class CurrencyTextView method setPrefixColor.
public void setPrefixColor(final int prefixColor) {
this.prefixColorSpan = new ForegroundColorSpan(prefixColor);
updateView();
}
use of android.text.style.ForegroundColorSpan in project bitcoin-wallet by bitcoin-wallet.
the class WalletBalanceWidgetProvider method updateWidget.
private static void updateWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId, final Bundle appWidgetOptions, final Coin balance) {
final WalletApplication application = (WalletApplication) context.getApplicationContext();
final Configuration config = application.getConfiguration();
final MonetaryFormat btcFormat = config.getFormat();
final Spannable balanceStr = new MonetarySpannable(btcFormat.noCode(), balance).applyMarkup(null, MonetarySpannable.STANDARD_INSIGNIFICANT_SPANS);
final Cursor data = context.getContentResolver().query(ExchangeRatesProvider.contentUri(context.getPackageName(), true), null, ExchangeRatesProvider.KEY_CURRENCY_CODE, new String[] { config.getExchangeCurrencyCode() }, null);
final Spannable localBalanceStr;
if (data != null) {
if (data.moveToFirst()) {
final ExchangeRate exchangeRate = ExchangeRatesProvider.getExchangeRate(data);
final Fiat localBalance = exchangeRate.rate.coinToFiat(balance);
final MonetaryFormat localFormat = Constants.LOCAL_FORMAT.code(0, Constants.PREFIX_ALMOST_EQUAL_TO + GenericUtils.currencySymbol(exchangeRate.getCurrencyCode()));
final Object[] prefixSpans = new Object[] { MonetarySpannable.SMALLER_SPAN, new ForegroundColorSpan(context.getResources().getColor(R.color.fg_less_significant)) };
localBalanceStr = new MonetarySpannable(localFormat, localBalance).applyMarkup(prefixSpans, MonetarySpannable.STANDARD_INSIGNIFICANT_SPANS);
} else {
localBalanceStr = null;
}
data.close();
} else {
localBalanceStr = null;
}
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wallet_balance_widget_content);
final String currencyCode = btcFormat.code();
if (MonetaryFormat.CODE_BTC.equals(currencyCode))
views.setImageViewResource(R.id.widget_wallet_prefix, R.drawable.currency_symbol_btc);
else if (MonetaryFormat.CODE_MBTC.equals(currencyCode))
views.setImageViewResource(R.id.widget_wallet_prefix, R.drawable.currency_symbol_mbtc);
else if (MonetaryFormat.CODE_UBTC.equals(currencyCode))
views.setImageViewResource(R.id.widget_wallet_prefix, R.drawable.currency_symbol_ubtc);
views.setTextViewText(R.id.widget_wallet_balance_btc, balanceStr);
views.setViewVisibility(R.id.widget_wallet_balance_local, localBalanceStr != null ? View.VISIBLE : View.GONE);
views.setTextViewText(R.id.widget_wallet_balance_local, localBalanceStr);
if (appWidgetOptions != null) {
final int minWidth = appWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
views.setViewVisibility(R.id.widget_app_icon, minWidth > 400 ? View.VISIBLE : View.GONE);
views.setViewVisibility(R.id.widget_button_request, minWidth > 300 ? View.VISIBLE : View.GONE);
views.setViewVisibility(R.id.widget_button_send, minWidth > 300 ? View.VISIBLE : View.GONE);
views.setViewVisibility(R.id.widget_button_send_qr, minWidth > 200 ? View.VISIBLE : View.GONE);
}
views.setOnClickPendingIntent(R.id.widget_button_balance, PendingIntent.getActivity(context, 0, new Intent(context, WalletActivity.class), 0));
views.setOnClickPendingIntent(R.id.widget_button_request, PendingIntent.getActivity(context, 0, new Intent(context, RequestCoinsActivity.class), 0));
views.setOnClickPendingIntent(R.id.widget_button_send, PendingIntent.getActivity(context, 0, new Intent(context, SendCoinsActivity.class), 0));
views.setOnClickPendingIntent(R.id.widget_button_send_qr, PendingIntent.getActivity(context, 0, new Intent(context, SendCoinsQrActivity.class), 0));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Aggregations