Example usage for android.text SpannableString setSpan

List of usage examples for android.text SpannableString setSpan

Introduction

In this page you can find the example usage for android.text SpannableString setSpan.

Prototype

public void setSpan(Object what, int start, int end, int flags) 

Source Link

Usage

From source file:org.mozilla.gecko.DataReportingNotification.java

/**
 * Launch a notification of the data policy, and record notification time and version.
 *//*from w w w. j a va2 s .  co m*/
private static void notifyDataPolicy(Context context, SharedPreferences sharedPrefs) {
    boolean result = false;
    try {
        // Launch main App to launch Data choices when notification is clicked.
        Intent prefIntent = new Intent(GeckoApp.ACTION_LAUNCH_SETTINGS);
        prefIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME,
                AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);

        GeckoPreferences.setResourceToOpen(prefIntent, "preferences_vendor");
        prefIntent.putExtra(ALERT_NAME_DATAREPORTING_NOTIFICATION, true);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, prefIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        final Resources resources = context.getResources();

        // Create and send notification.
        String notificationTitle = resources.getString(R.string.datareporting_notification_title);
        String notificationSummary;
        if (Versions.preJB) {
            notificationSummary = resources.getString(R.string.datareporting_notification_action);
        } else {
            // Display partial version of Big Style notification for supporting devices.
            notificationSummary = resources.getString(R.string.datareporting_notification_summary);
        }
        String notificationAction = resources.getString(R.string.datareporting_notification_action);
        String notificationBigSummary = resources.getString(R.string.datareporting_notification_summary);

        // Make styled ticker text for display in notification bar.
        String tickerString = resources.getString(R.string.datareporting_notification_ticker_text);
        SpannableString tickerText = new SpannableString(tickerString);
        // Bold the notification title of the ticker text, which is the same string as notificationTitle.
        tickerText.setSpan(new StyleSpan(Typeface.BOLD), 0, notificationTitle.length(),
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        Notification notification = new NotificationCompat.Builder(context).setContentTitle(notificationTitle)
                .setContentText(notificationSummary).setSmallIcon(R.drawable.ic_status_logo).setAutoCancel(true)
                .setContentIntent(contentIntent)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBigSummary))
                .addAction(R.drawable.firefox_settings_alert, notificationAction, contentIntent)
                .setTicker(tickerText).build();

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        int notificationID = ALERT_NAME_DATAREPORTING_NOTIFICATION.hashCode();
        notificationManager.notify(notificationID, notification);

        // Record version and notification time.
        SharedPreferences.Editor editor = sharedPrefs.edit();
        long now = System.currentTimeMillis();
        editor.putLong(PREFS_POLICY_NOTIFIED_TIME, now);
        editor.putInt(PREFS_POLICY_VERSION, DATA_REPORTING_VERSION);
        editor.apply();
        result = true;
    } finally {
        // We want to track any errors, so record notification outcome.
        Telemetry.sendUIEvent(TelemetryContract.Event.POLICY_NOTIFICATION_SUCCESS, result);
    }
}

From source file:Main.java

public static CharSequence addIcon(CharSequence total, BitmapDrawable bitmapDrawable, int height) {
    SpannableString string = new SpannableString("  ");
    ImageSpan imageSpan = new ImageSpan(bitmapDrawable);

    int width = (int) (height
            / (bitmapDrawable.getIntrinsicHeight() / (float) bitmapDrawable.getIntrinsicWidth()));

    imageSpan.getDrawable().setBounds(0, 0, width, height);
    string.setSpan(imageSpan, 0, 1, 0);
    if (total == null) {
        return string;
    } else {//  w w w  .  j a  v  a 2s  .  c  o m
        return TextUtils.concat(total, string);
    }
}

From source file:Main.java

public static CharSequence scaleEmotions(String text) {
    SpannableString spannableString = new SpannableString(text);
    for (String emotion : emotions) {
        Pattern pattern = Pattern.compile(emotion);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(new RelativeSizeSpan(1.2f), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }/*  w ww  .j  a v a  2 s  .  c o m*/
    }
    return spannableString;
}

From source file:org.solovyev.android.calculator.App.java

@NonNull
public static SpannableString colorString(@Nonnull String s, int color) {
    final SpannableString spannable = new SpannableString(s);
    spannable.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spannable;
}

From source file:com.tasomaniac.openwith.settings.SettingsFragment.java

private static CharSequence coloredErrorString(Context context, CharSequence originalString) {
    SpannableString errorSpan = new SpannableString(originalString);
    ForegroundColorSpan colorSpan = new ForegroundColorSpan(
            ContextCompat.getColor(context, R.color.error_color));
    errorSpan.setSpan(colorSpan, 0, originalString.length(), 0);
    return errorSpan;
}

From source file:com.ibm.mil.readyapps.telco.hotspots.HotSpotActivity.java

private static SpannableString createSpan(String text, int start, int stop, Object... spans) {
    SpannableString spannableString = new SpannableString(text);
    for (Object span : spans) {
        spannableString.setSpan(span, start, stop, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }//from   ww w.  ja  v a  2 s. c  o  m
    return spannableString;
}

From source file:com.saulmm.cui.OrderDialogFragment.java

@BindingAdapter("app:spanOffset")
public static void setItemSpan(View v, int spanOffset) {
    final String itemText = ((TextView) v).getText().toString();
    final SpannableString sString = new SpannableString(itemText);

    sString.setSpan(new RelativeSizeSpan(1.65f), itemText.length() - spanOffset, itemText.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    ((TextView) v).setText(sString);//from  w  ww.  j  a v  a2 s . co m
}

From source file:com.nextgis.maplibui.util.ControlHelper.java

public static void highlightText(TextView textView) {
    final CharSequence text = textView.getText();
    final SpannableString spannableString = new SpannableString(text);
    spannableString.setSpan(new URLSpan(""), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {/*w  w  w  .  j  a  v a 2s  .c  om*/
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
    }

    return ss;
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {/*  w  w  w  .ja v a 2  s  .  c o m*/
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
        // Logger.e(TAG, ex);
    }

    return ss;
}