Example usage for android.text Spannable setSpan

List of usage examples for android.text Spannable setSpan

Introduction

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

Prototype

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

Source Link

Document

Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

Usage

From source file:com.nkahoang.screenstandby.Main.java

private void warning() {
    try {//w  ww  .j  a  v  a 2 s .co m
        String build = android.os.Build.MODEL.toLowerCase();
        if (build.contains("i535") || build.contains("d2vzw") || build.contains("d2spr")
                || build.contains("d2tmo")) {
            TextView tv = (TextView) this.findViewById(R.id.textView1);
            Spannable warning = new SpannableString(
                    "IMPORTANT! This app is currently has compatibility problem with your device (" + build
                            + ") as it may freezes up the screen. I am sorry for the problem and is trying my best to fix this");
            warning.setSpan(new ForegroundColorSpan(Color.RED), 0, warning.length(), 0);
            tv.setText(warning);
        }
    } catch (Exception ex) {
    }
}

From source file:org.catrobat.catroid.ui.fragment.LookFragment.java

private void updateActionModeTitle() {
    int numberOfSelectedItems = adapter.getAmountOfCheckedItems();

    if (numberOfSelectedItems == 0) {
        actionMode.setTitle(actionModeTitle);
    } else {/*from   w  w w  .j av  a  2s.c o m*/
        String appendix = multipleItemAppendixActionMode;

        if (numberOfSelectedItems == 1) {
            appendix = singleItemAppendixActionMode;
        }

        String numberOfItems = Integer.toString(numberOfSelectedItems);
        String completeTitle = actionModeTitle + " " + numberOfItems + " " + appendix;

        int titleLength = actionModeTitle.length();

        Spannable completeSpannedTitle = new SpannableString(completeTitle);
        completeSpannedTitle.setSpan(
                new ForegroundColorSpan(getResources().getColor(R.color.actionbar_title_color)),
                titleLength + 1, titleLength + (1 + numberOfItems.length()),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        actionMode.setTitle(completeSpannedTitle);
    }
}

From source file:com.flipzu.flipzu.Recorder.java

public void showFlipzuTipsOffline(LinearLayout cc, LayoutParams params, String msg) {
    final float scale = getResources().getDisplayMetrics().density;
    final int pixel_5 = 5 / (int) (scale + 0.5f);

    LinearLayout cl = new LinearLayout(Recorder.this);
    cl.setOrientation(LinearLayout.HORIZONTAL);
    cl.setPadding(0, 0, 0, pixel_5);/*from   w  w w .j  a v  a 2  s. co  m*/

    /* comment */
    String tips_username = "Flipzu Tips";

    TextView comment_tv = new TextView(Recorder.this);
    comment_tv.setLayoutParams(params);
    comment_tv.setText(tips_username + ": " + msg, TextView.BufferType.SPANNABLE);
    comment_tv.setTextColor(Color.parseColor("#656565"));
    Spannable comment_span = (Spannable) comment_tv.getText();
    comment_span.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, comment_tv.getText().length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    comment_span.setSpan(new ForegroundColorSpan(Color.parseColor("#182e5b")), 0, tips_username.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    comment_tv.setText(comment_span);
    cl.addView(comment_tv);

    cc.addView(cl);
}

From source file:com.flipzu.flipzu.Recorder.java

@Override
public void onCommentsReceived(Hashtable<String, String>[] comments) {
    debug.logV(TAG, "onCommentsReceived");

    final LinearLayout cc = (LinearLayout) findViewById(R.id.comments_container);

    /* cleanup comments first */
    cc.removeAllViews();/*  ww  w  . j  a  va 2s. c  o m*/

    /* get pixel values for various DIPs */
    final float scale = getResources().getDisplayMetrics().density;
    //      final int pixel_10 = 10 / (int) (scale + 0.5f);
    final int pixel_5 = 5 / (int) (scale + 0.5f);
    //      final int pixel_30 = 30 / (int) (scale + 0.5f);
    final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    if (comments != null && comments.length > 0) {

        for (int i = 0; i < comments.length; i++) {

            LinearLayout cl = new LinearLayout(Recorder.this);
            cl.setOrientation(LinearLayout.HORIZONTAL);
            cl.setPadding(0, 0, 0, pixel_5);

            debug.logD(TAG, "Refresher comment " + comments[i]);

            /* comment */
            TextView comment_tv = new TextView(Recorder.this);
            comment_tv.setLayoutParams(params);
            comment_tv.setText(comments[i].get("username") + ": " + comments[i].get("comment"),
                    TextView.BufferType.SPANNABLE);
            comment_tv.setTextColor(Color.parseColor("#656565"));
            Spannable comment_span = (Spannable) comment_tv.getText();
            comment_span.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
                    comment_tv.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            comment_span.setSpan(new ForegroundColorSpan(Color.parseColor("#182e5b")), 0,
                    comments[i].get("username").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            comment_tv.setText(comment_span);
            cl.addView(comment_tv);

            cc.addView(cl);

        }

    } else {
        if (mState == recorderState.RECORDING) {
            String msg = "You're LIVE! Your broadcast can be heard at http://flipzu.com/" + mUser.getUsername();
            showFlipzuTipsOffline(cc, params, msg);
        } else {
            String msg = "Pick a good and descriptive broadcast title. A great title will attract more listeners!";
            showFlipzuTipsOffline(cc, params, msg);
            msg = "You can disable sharing in Twitter and Facebook by clicking the logo buttons. This is great for testing.";
            showFlipzuTipsOffline(cc, params, msg);
            msg = "Press the \"Start Broadcast\" button and let them hear you!";
            showFlipzuTipsOffline(cc, params, msg);
        }
    }
}

From source file:com.keylesspalace.tusky.activity.ComposeActivity.java

private static void highlightSpans(Spannable text, int colour) {
    // Strip all existing colour spans.
    int n = text.length();
    ForegroundColorSpan[] oldSpans = text.getSpans(0, n, ForegroundColorSpan.class);
    for (int i = oldSpans.length - 1; i >= 0; i--) {
        text.removeSpan(oldSpans[i]);/*from w ww.  jav  a2s  .  c  om*/
    }
    // Colour the mentions and hashtags.
    String string = text.toString();
    int start;
    int end = 0;
    while (end < n) {
        char[] chars = { '#', '@' };
        FindCharsResult found = findStart(string, end, chars);
        start = found.stringIndex;
        if (start < 0) {
            break;
        }
        if (found.charIndex == 0) {
            end = findEndOfHashtag(string, start);
        } else if (found.charIndex == 1) {
            end = findEndOfMention(string, start);
        } else {
            break;
        }
        if (end < 0) {
            break;
        }
        text.setSpan(new ForegroundColorSpan(colour), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
}

From source file:im.zico.fancy.common.widget.HackyMovementMethod.java

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
    if (mGray == null) {
        mGray = new BackgroundColorSpan(
                ContextCompat.getColor(widget.getContext(), R.color.alpha_spannable_pressed));
    }/* w  w w. j a  va 2s .  c o  m*/

    mIsLinkHit = false;

    int action = event.getAction();

    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        x -= widget.getTotalPaddingLeft();
        y -= widget.getTotalPaddingTop();
        x += widget.getScrollX();
        y += widget.getScrollY();

        int line = widget.getLayout().getLineForVertical(y);
        int offset = widget.getLayout().getOffsetForHorizontal(line, x);

        ClickableSpan[] spans = buffer.getSpans(offset, offset, ClickableSpan.class);

        if (spans.length != 0) {
            int start = buffer.getSpanStart(spans[0]);
            int end = buffer.getSpanEnd(spans[0]);
            mIsLinkHit = true;
            if (action == MotionEvent.ACTION_DOWN) {
                buffer.setSpan(mGray, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else if (action == MotionEvent.ACTION_UP) {
                spans[0].onClick(widget);
                buffer.removeSpan(mGray);
            }
            return true;
        }
    } else {
        buffer.removeSpan(mGray);
    }

    return Touch.onTouchEvent(widget, buffer, event);
}

From source file:org.onebusaway.android.report.ui.Open311ProblemFragment.java

/**
 * Dynamically creates an edit text// w w  w .java 2 s. c om
 *
 * @param open311Attribute contains the open311 attributes
 */
private void createEditText(Open311Attribute open311Attribute) {
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.report_issue_text_item, null, false);

    ImageView icon = ((ImageView) layout.findViewById(R.id.ri_ic_question_answer));
    icon.setColorFilter(getResources().getColor(R.color.material_gray));

    Spannable desc = new SpannableString(MyTextUtils.toSentenceCase(open311Attribute.getDescription()));
    EditText editText = ((EditText) layout.findViewById(R.id.riti_editText));
    if (open311Attribute.getRequired()) {
        Spannable req = new SpannableString("(required)");
        req.setSpan(new ForegroundColorSpan(Color.RED), 0, req.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        editText.setHint(TextUtils.concat(desc, " ", req));
    } else {
        editText.setHint(desc);
    }

    if (Open311DataType.NUMBER.equals(open311Attribute.getDatatype())) {
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
    } else if (Open311DataType.DATETIME.equals(open311Attribute.getDatatype())) {
        editText.setInputType(InputType.TYPE_CLASS_DATETIME);
    }

    // Restore view state from attribute result hash map
    AttributeValue av = mAttributeValueHashMap.get(open311Attribute.getCode());
    if (av != null) {
        editText.setText(av.getSingleValue());
    }

    // Dynamically fill stop id if this is a transit service
    // And if this is a bus stop field
    if (ServiceUtils.isTransitServiceByType(mService.getType())
            && ServiceUtils.isStopIdField(open311Attribute.getDescription())) {

        icon.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ri_flag_triangle));

        ObaStop obaStop = getIssueLocationHelper().getObaStop();
        if (obaStop != null) {
            editText.setText(obaStop.getStopCode());
        }
    }

    mInfoLayout.addView(layout);
    mDynamicAttributeUIMap.put(open311Attribute.getCode(), editText);
}

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

/**
 * Copies the spans from the region <code>start...end</code> in
 * <code>source</code> to the region
 * <code>destoff...destoff+end-start</code> in <code>dest</code>.
 * Spans in <code>source</code> that begin before <code>start</code>
 * or end after <code>end</code> but overlap this range are trimmed
 * as if they began at <code>start</code> or ended at <code>end</code>.
 *
 * @throws IndexOutOfBoundsException if any of the copied spans
 * are out of range in <code>dest</code>.
 *//*from w w w  .jav  a  2  s. co  m*/
public static void copySpansFrom(Spanned source, int start, int end, Class kind, Spannable dest, int destoff) {
    if (kind == null) {
        kind = Object.class;
    }

    Object[] spans = source.getSpans(start, end, kind);

    for (int i = 0; i < spans.length; i++) {
        int st = source.getSpanStart(spans[i]);
        int en = source.getSpanEnd(spans[i]);
        int fl = source.getSpanFlags(spans[i]);

        if (st < start)
            st = start;
        if (en > end)
            en = end;

        dest.setSpan(spans[i], st - start + destoff, en - start + destoff, fl);
    }
}

From source file:org.onebusaway.android.report.ui.Open311ProblemFragment.java

/**
 * Dynamically creates checkboxes/*from w w w . jav a2s.com*/
 *
 * @param open311Attribute contains the open311 attributes
 */
private void createMultiValueList(Open311Attribute open311Attribute) {
    ArrayList<Object> values = (ArrayList<Object>) open311Attribute.getValues();
    if (values != null && values.size() > 0) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.report_issue_multi_value_list_item,
                null, false);

        ((ImageView) layout.findViewById(R.id.ri_ic_checkbox))
                .setColorFilter(getResources().getColor(R.color.material_gray));

        Spannable word = new SpannableString(open311Attribute.getDescription());
        ((TextView) layout.findViewById(R.id.rimvli_textView)).setText(word);

        if (open311Attribute.getRequired()) {
            Spannable wordTwo = new SpannableString(" *Required");
            wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            ((TextView) layout.findViewById(R.id.rimvli_textView)).append(wordTwo);
        }

        // Restore view state from attribute result hash map
        AttributeValue av = mAttributeValueHashMap.get(open311Attribute.getCode());

        LinearLayout cg = (LinearLayout) layout.findViewById(R.id.rimvli_checkBoxGroup);
        for (int i = 0; i < values.size(); i++) {
            LinkedHashMap<String, String> value = (LinkedHashMap<String, String>) values.get(i);
            CheckBox cb = new CheckBox(getActivity());
            cg.addView(cb);
            String attributeKey = "";
            String attributeValue = "";
            for (LinkedHashMap.Entry<String, String> entry : value.entrySet()) {
                if (Open311Attribute.NAME.equals(entry.getKey())) {
                    cb.setText(entry.getValue());
                    if (av != null && av.getValues().contains(entry.getValue())) {
                        cb.setChecked(true);
                    }
                    attributeKey = open311Attribute.getCode() + entry.getValue();
                } else if (Open311Attribute.KEY.equals(entry.getKey())) {
                    attributeValue = entry.getValue();
                }
            }
            mOpen311AttributeKeyNameMap.put(attributeKey, attributeValue);
        }

        mInfoLayout.addView(layout);
        mDynamicAttributeUIMap.put(open311Attribute.getCode(), cg);
    }
}

From source file:org.onebusaway.android.report.ui.Open311ProblemFragment.java

/**
 * Dynamically creates radio buttons//from ww w  .j  a  v  a2  s . co m
 *
 * @param open311Attribute contains the open311 attributes
 */
private void createSingleValueList(Open311Attribute open311Attribute) {
    ArrayList<Object> values = (ArrayList<Object>) open311Attribute.getValues();
    if (values != null && values.size() > 0) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.report_issue_single_value_list_item,
                null, false);
        layout.setSaveEnabled(true);
        ((ImageView) layout.findViewById(R.id.ri_ic_radio))
                .setColorFilter(getResources().getColor(R.color.material_gray));

        Spannable word = new SpannableString(open311Attribute.getDescription());
        ((TextView) layout.findViewById(R.id.risvli_textView)).setText(word);

        if (open311Attribute.getRequired()) {
            Spannable wordTwo = new SpannableString(" *Required");
            wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            ((TextView) layout.findViewById(R.id.risvli_textView)).append(wordTwo);
        }

        RadioGroup rg = (RadioGroup) layout.findViewById(R.id.risvli_radioGroup);
        rg.setOrientation(RadioGroup.VERTICAL);

        // Restore view state from attribute result hash map
        AttributeValue av = mAttributeValueHashMap.get(open311Attribute.getCode());
        String entryValue = null;
        if (av != null) {
            entryValue = av.getSingleValue();
        }

        for (int i = 0; i < values.size(); i++) {
            LinkedHashMap<String, String> value = (LinkedHashMap<String, String>) values.get(i);
            RadioButton rb = new RadioButton(getActivity());
            rg.addView(rb); //the RadioButtons are added to the radioGroup instead of the layout
            String attributeKey = "";
            String attributeValue = "";
            for (LinkedHashMap.Entry<String, String> entry : value.entrySet()) {
                if (Open311Attribute.NAME.equals(entry.getKey())) {
                    rb.setText(entry.getValue());
                    if (entryValue != null && entryValue.equalsIgnoreCase(entry.getValue())) {
                        rb.setChecked(true);
                    }
                    attributeKey = open311Attribute.getCode() + entry.getValue();
                } else if (Open311Attribute.KEY.equals(entry.getKey())) {
                    attributeValue = entry.getValue();
                }
            }
            mOpen311AttributeKeyNameMap.put(attributeKey, attributeValue);
        }

        mInfoLayout.addView(layout);
        mDynamicAttributeUIMap.put(open311Attribute.getCode(), rg);
    }
}