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.catrobat.catroid.ui.ScratchConverterActivity.java

private void appendColoredBetaLabelToTitle(final int color) {
    final String title = getString(R.string.title_activity_scratch_converter);
    final String beta = getString(R.string.beta).toUpperCase(Locale.getDefault());
    final SpannableString spanTitle = new SpannableString(title + " " + beta);
    final int begin = title.length() + 1;
    final int end = begin + beta.length();
    spanTitle.setSpan(new ForegroundColorSpan(color), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    getActionBar().setTitle(spanTitle);//from   w  w  w.j ava 2s .c o  m
}

From source file:org.solovyev.android.calculator.widget.CalculatorWidget.java

@Nonnull
private SpannedString initCursorString(@Nonnull Context context) {
    if (cursorString == null) {
        final SpannableString s = App.colorString("|",
                ContextCompat.getColor(context, R.color.cpp_widget_cursor));
        // this will override any other style span (f.e. italic)
        s.setSpan(new StyleSpan(Typeface.NORMAL), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        cursorString = new SpannedString(s);
    }/* w  w w.  j a va2  s.  c o m*/
    return cursorString;
}

From source file:com.flowzr.activity.MainActivity.java

private void setMyTabText(Tab tab, String text) {
    SpannableString s = new SpannableString(text);
    s.setSpan(new TypefaceSpan("sans-serif"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tab.setText(s);/*ww  w.ja  va  2s .  co m*/
}

From source file:org.sirimangalo.meditationplus.AdapterChat.java

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView;/*w ww.j  av a 2 s.c o m*/

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.list_item_chat, parent, false);
    } else {
        rowView = convertView;
    }

    JSONObject p = values.get(position);
    try {
        int then = Integer.parseInt(p.getString("time"));
        long nowL = System.currentTimeMillis() / 1000;
        int now = (int) nowL;

        int ela = now - then;
        int day = 60 * 60 * 24;
        ela = ela > day ? day : ela;
        int intColor = 255 - Math.round((float) ela * 255 / day);
        intColor = intColor > 100 ? intColor : 100;
        String hexTransparency = Integer.toHexString(intColor);
        hexTransparency = hexTransparency.length() > 1 ? hexTransparency : "0" + hexTransparency;
        String hexColor = "#" + hexTransparency + "000000";
        int transparency = Color.parseColor(hexColor);

        TextView time = (TextView) rowView.findViewById(R.id.time);
        if (time != null) {
            String ts = null;
            ts = Utils.time2Ago(then);
            time.setText(ts);
            time.setTextColor(transparency);
        }

        TextView mess = (TextView) rowView.findViewById(R.id.message);
        if (mess != null) {

            final String username = p.getString("username");

            String messageString = p.getString("message");

            messageString = Html.fromHtml(messageString).toString();

            SpannableString messageSpan = Utils.replaceSmilies(context, messageString, intColor);

            if (messageString.contains("@" + loggedUser)) {
                int index = messageString.indexOf("@" + loggedUser);
                messageSpan.setSpan(new StyleSpan(Typeface.BOLD), index, index + loggedUser.length() + 1,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                messageSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#" + hexTransparency + "990000")),
                        index, index + loggedUser.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            for (String user : users) {
                if (!user.equals(loggedUser) && messageString.contains("@" + user)) {
                    int index = messageString.indexOf("@" + user);
                    messageSpan = Utils.createProfileSpan(context, index, index + user.length() + 1, user,
                            messageSpan);
                    messageSpan.setSpan(
                            new ForegroundColorSpan(Color.parseColor("#" + hexTransparency + "009900")), index,
                            index + user.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }

            Spannable userSpan = Utils.createProfileSpan(context, 0, username.length(), username,
                    new SpannableString(username + ": "));

            if (p.getString("me").equals("true"))
                userSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#" + hexTransparency + "0000FF")), 0,
                        userSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            else
                userSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#" + hexTransparency + "000000")), 0,
                        userSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            CharSequence full = TextUtils.concat(userSpan, messageSpan);

            mess.setTextColor(transparency);
            mess.setText(full);
            mess.setMovementMethod(LinkMovementMethod.getInstance());
            Linkify.addLinks(mess, Linkify.ALL);
            mess.setTag(p.getString("cid"));
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return rowView;

}

From source file:com.eugene.fithealthmaingit.MainActivity.java

private void applyFontToMenuItem(MenuItem mi) {
    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Regular.ttf");
    SpannableString mNewTitle = new SpannableString(mi.getTitle());
    mNewTitle.setSpan(new CustomTypefaceSpan("", font), 0, mNewTitle.length(),
            Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    mi.setTitle(mNewTitle);//from  w  w  w .j a  va 2 s.  com
}

From source file:org.floens.chan.ui.controller.ThemeSettingsController.java

@Override
public void onCreate() {
    super.onCreate();

    navigationItem.setTitle(R.string.settings_screen_theme);
    navigationItem.swipeable = false;/*from  w  ww  . j  a va  2 s. com*/
    view = inflateRes(R.layout.controller_theme);

    themeHelper = ThemeHelper.getInstance();
    themes = themeHelper.getThemes();

    pager = (ViewPager) view.findViewById(R.id.pager);
    done = (FloatingActionButton) view.findViewById(R.id.add);
    done.setOnClickListener(this);

    textView = (TextView) view.findViewById(R.id.text);

    SpannableString changeAccentColor = new SpannableString(getString(R.string.setting_theme_accent));
    changeAccentColor.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            showAccentColorPicker();
        }
    }, 0, changeAccentColor.length(), 0);

    textView.setText(TextUtils.concat(getString(R.string.setting_theme_explanation), changeAccentColor));
    textView.setMovementMethod(LinkMovementMethod.getInstance());

    adapter = new Adapter();
    pager.setAdapter(adapter);

    ChanSettings.ThemeColor currentSettingsTheme = ChanSettings.getThemeAndColor();
    for (int i = 0; i < themeHelper.getThemes().size(); i++) {
        Theme theme = themeHelper.getThemes().get(i);
        ThemeHelper.PrimaryColor primaryColor = theme.primaryColor;

        if (theme.name.equals(currentSettingsTheme.theme)) {
            // Current theme
            pager.setCurrentItem(i, false);
        }
        selectedPrimaryColors.add(primaryColor);
    }
    selectedAccentColor = themeHelper.getTheme().accentColor;
    done.setBackgroundTintList(ColorStateList.valueOf(selectedAccentColor.color));
}

From source file:com.android.browser.GearsBaseDialog.java

/**
 * Display a button as an HTML link. Remove the background, set the
 * text color to R.color.dialog_link and draw an underline
 *//*from  ww w  .j ava2 s  . c o m*/
void displayAsLink(Button button) {
    if (button == null) {
        return;
    }

    CharSequence text = button.getText();
    button.setBackgroundDrawable(null);
    int color = getResources().getColor(R.color.dialog_link);
    button.setTextColor(color);
    SpannableString str = new SpannableString(text);
    str.setSpan(new UnderlineSpan(), 0, str.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    button.setText(str);
    button.setFocusable(false);
}

From source file:org.mozilla.focus.fragment.UrlInputFragment.java

@Override
public void onFilter(String searchText, InlineAutocompleteEditText view) {
    // If the UrlInputFragment has already been hidden, don't bother with filtering. Because of the text
    // input architecture on Android it's possible for onFilter() to be called after we've already
    // hidden the Fragment, see the relevant bug for more background:
    // https://github.com/mozilla-mobile/focus-android/issues/441#issuecomment-293691141
    if (!isVisible()) {
        return;//w w w  . j a  va2s. co m
    }

    urlAutoCompleteFilter.onFilter(searchText, view);

    if (searchText.length() == 0) {
        clearView.setVisibility(View.GONE);
        searchViewContainer.setVisibility(View.GONE);
    } else {
        clearView.setVisibility(View.VISIBLE);

        final String hint = getString(R.string.search_hint, searchText);

        final SpannableString content = new SpannableString(hint);
        content.setSpan(new StyleSpan(Typeface.BOLD), hint.length() - searchText.length(), hint.length(), 0);

        searchView.setText(content);
        searchViewContainer.setVisibility(View.VISIBLE);
    }
}

From source file:cm.aptoide.pt.webservices.login.Login.java

private void drawLoginForm() {
    setContentView(R.layout.form_login);
    username_box = (EditText) findViewById(R.id.username);
    password_box = (EditText) findViewById(R.id.password);
    checkShowPass = (CheckBox) findViewById(R.id.show_login_passwd);
    checkShowPass.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                password_box.setTransformationMethod(null);
            } else {
                password_box.setTransformationMethod(new PasswordTransformationMethod());
            }//from w  w w  .  java 2 s .  co m
        }
    });
    checkShowPass.setEnabled(true);

    createUser = (Button) findViewById(R.id.new_to_aptoide);
    SpannableString newUserString = new SpannableString(getString(R.string.new_to_aptoide));
    newUserString.setSpan(new UnderlineSpan(), 0, newUserString.length(), 0);
    createUser.setText(newUserString);

    forgot_password = (TextView) findViewById(R.id.forgot_password);
    SpannableString forgetString = new SpannableString(getString(R.string.forgot_passwd));
    forgetString.setSpan(new UnderlineSpan(), 0, forgetString.length(), 0);
    forgot_password.setText(forgetString);
    forgot_password.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent passwordRecovery = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://m.aptoide.com/account/password-recovery"));
            startActivity(passwordRecovery);
        }
    });
}

From source file:uk.org.ngo.squeezer.itemlist.AlarmView.java

private void setDowText(AlarmViewHolder viewHolder, int day) {
    SpannableString text = new SpannableString(ServerString.getAlarmShortDayText(day));
    if (viewHolder.alarm.isDayActive(day)) {
        text.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), 0);
        text.setSpan(new ForegroundColorSpan(mColorSelected), 0, text.length(), 0);
        Drawable underline = mResources.getDrawable(R.drawable.underline);
        float textSize = (new Paint()).measureText(text.toString());
        underline.setBounds(0, 0, (int) (textSize * mDensity), (int) (1 * mDensity));
        viewHolder.dowTexts[day].setCompoundDrawables(null, null, null, underline);
    } else//from  w ww  .java 2  s  .co  m
        viewHolder.dowTexts[day].setCompoundDrawables(null, null, null, null);
    viewHolder.dowTexts[day].setText(text);
}