Search in sources :

Example 76 with ForegroundColorSpan

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;
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) CharacterStyle(android.text.style.CharacterStyle)

Example 77 with ForegroundColorSpan

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;
    }
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan)

Example 78 with ForegroundColorSpan

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);
        });
}
Also used : Context(android.content.Context) LayoutInflater(android.view.LayoutInflater) EntryQueryRelevance(org.sorz.lab.tinykeepass.search.EntryQueryRelevance) KeePassFile(de.slackspace.openkeepass.domain.KeePassFile) Spannable(android.text.Spannable) ImageView(android.widget.ImageView) BackgroundColorSpan(android.text.style.BackgroundColorSpan) Collectors(java.util.stream.Collectors) ViewGroup(android.view.ViewGroup) ArrayList(java.util.ArrayList) RecyclerView(android.support.v7.widget.RecyclerView) BiPredicate(java.util.function.BiPredicate) SpannableStringBuilder(android.text.SpannableStringBuilder) List(java.util.List) TextView(android.widget.TextView) KeePassStorage(org.sorz.lab.tinykeepass.keepass.KeePassStorage) BiConsumer(java.util.function.BiConsumer) KeePassHelper.getIcon(org.sorz.lab.tinykeepass.keepass.KeePassHelper.getIcon) View(android.view.View) KeePassHelper(org.sorz.lab.tinykeepass.keepass.KeePassHelper) Nullable(android.support.annotation.Nullable) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Log(android.util.Log) Entry(de.slackspace.openkeepass.domain.Entry) Entry(de.slackspace.openkeepass.domain.Entry) ForegroundColorSpan(android.text.style.ForegroundColorSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan)

Example 79 with ForegroundColorSpan

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();
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan)

Example 80 with ForegroundColorSpan

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);
}
Also used : ExchangeRate(de.schildbach.wallet.data.ExchangeRate) MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Cursor(android.database.Cursor) MonetarySpannable(de.schildbach.wallet.util.MonetarySpannable) RemoteViews(android.widget.RemoteViews) Fiat(org.bitcoinj.utils.Fiat) MonetarySpannable(de.schildbach.wallet.util.MonetarySpannable) Spannable(android.text.Spannable)

Aggregations

ForegroundColorSpan (android.text.style.ForegroundColorSpan)157 SpannableStringBuilder (android.text.SpannableStringBuilder)57 SpannableString (android.text.SpannableString)50 StyleSpan (android.text.style.StyleSpan)25 TextView (android.widget.TextView)25 ImageSpan (android.text.style.ImageSpan)23 Spannable (android.text.Spannable)22 RelativeSizeSpan (android.text.style.RelativeSizeSpan)22 View (android.view.View)22 BackgroundColorSpan (android.text.style.BackgroundColorSpan)19 TypefaceSpan (android.text.style.TypefaceSpan)16 StrikethroughSpan (android.text.style.StrikethroughSpan)14 UnderlineSpan (android.text.style.UnderlineSpan)13 Drawable (android.graphics.drawable.Drawable)12 CharacterStyle (android.text.style.CharacterStyle)11 EditText (android.widget.EditText)11 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)10 SuperscriptSpan (android.text.style.SuperscriptSpan)8 LinearLayout (android.widget.LinearLayout)8 URLSpan (android.text.style.URLSpan)7