Search in sources :

Example 26 with Spanned

use of android.text.Spanned in project platform_frameworks_base by android.

the class SuggestionsPopupWindowTest method testTextAppearanceInSuggestionsPopup.

@SmallTest
public void testTextAppearanceInSuggestionsPopup() {
    final String text = "abc def ghi";
    final String[] singleWordCandidates = { "DEF", "Def" };
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), singleWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final String[] multiWordCandidates = { "ABC DEF GHI", "Abc Def Ghi" };
    final SuggestionSpan multiWordSuggestionSpan = new SuggestionSpan(getActivity(), multiWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final TypedArray array = getActivity().obtainStyledAttributes(com.android.internal.R.styleable.Theme);
    final int id = array.getResourceId(com.android.internal.R.styleable.Theme_textEditSuggestionHighlightStyle, 0);
    array.recycle();
    final TextAppearanceSpan expectedSpan = new TextAppearanceSpan(getActivity(), id);
    final TextPaint tmpTp = new TextPaint();
    expectedSpan.updateDrawState(tmpTp);
    final int expectedHighlightTextColor = tmpTp.getColor();
    final float expectedHighlightTextSize = tmpTp.getTextSize();
    final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
    // *XX* means that XX is highlighted.
    for (int i = 0; i < 2; i++) {
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(replaceText(text));
        setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
        setSuggestionSpan(multiWordSuggestionSpan, 0, text.length());
        showSuggestionsPopup();
        assertSuggestionsPopupIsDisplayed();
        assertSuggestionsPopupContainsItem("abc DEF ghi");
        assertSuggestionsPopupContainsItem("abc Def ghi");
        assertSuggestionsPopupContainsItem("ABC DEF GHI");
        assertSuggestionsPopupContainsItem("Abc Def Ghi");
        assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
        onSuggestionsPopup().check(new ViewAssertion() {

            @Override
            public void check(View view, NoMatchingViewException e) {
                final ListView listView = (ListView) view.findViewById(com.android.internal.R.id.suggestionContainer);
                assertNotNull(listView);
                final int childNum = listView.getChildCount();
                assertEquals(singleWordCandidates.length + multiWordCandidates.length, childNum);
                for (int j = 0; j < childNum; j++) {
                    final TextView suggestion = (TextView) listView.getChildAt(j);
                    assertNotNull(suggestion);
                    final Spanned spanned = (Spanned) suggestion.getText();
                    assertNotNull(spanned);
                    // Check that the suggestion item order is kept.
                    final String expectedText;
                    if (j < singleWordCandidates.length) {
                        expectedText = "abc " + singleWordCandidates[j] + " ghi";
                    } else {
                        expectedText = multiWordCandidates[j - singleWordCandidates.length];
                    }
                    assertEquals(expectedText, spanned.toString());
                    // Check that the text is highlighted with correct color and text size.
                    final TextAppearanceSpan[] taSpan = spanned.getSpans(text.indexOf('d'), text.indexOf('f') + 1, TextAppearanceSpan.class);
                    assertEquals(1, taSpan.length);
                    TextPaint tp = new TextPaint();
                    taSpan[0].updateDrawState(tp);
                    assertEquals(expectedHighlightTextColor, tp.getColor());
                    assertEquals(expectedHighlightTextSize, tp.getTextSize());
                    // Check the correct part of the text is highlighted.
                    final int expectedStart;
                    final int expectedEnd;
                    if (j < singleWordCandidates.length) {
                        expectedStart = text.indexOf('d');
                        expectedEnd = text.indexOf('f') + 1;
                    } else {
                        expectedStart = 0;
                        expectedEnd = text.length();
                    }
                    assertEquals(expectedStart, spanned.getSpanStart(taSpan[0]));
                    assertEquals(expectedEnd, spanned.getSpanEnd(taSpan[0]));
                }
            }
        });
        pressBack();
        onView(withId(R.id.textview)).inRoot(withDecorView(is(getActivity().getWindow().getDecorView()))).perform(clearText());
    }
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) RootMatchers.withDecorView(android.support.test.espresso.matcher.RootMatchers.withDecorView) DragHandleUtils.onHandleView(android.widget.espresso.DragHandleUtils.onHandleView) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint) ViewAssertion(android.support.test.espresso.ViewAssertion) TypedArray(android.content.res.TypedArray) SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 27 with Spanned

use of android.text.Spanned in project androidquery by androidquery.

the class IntentListActivity method getAA.

private ArrayAdapter<ActivityItem> getAA() {
    list = new ArrayList<ActivityItem>();
    int[] ids = getResList();
    String[] names = getResources().getStringArray(ids[0]);
    String[] values = getResources().getStringArray(ids[1]);
    for (int i = 0; i < names.length; i++) {
        String name = names[i];
        String value = values[i];
        if (value.startsWith("http")) {
            list.add(new ActivityItem(null, name, value, null));
        } else {
            String[] vs = value.split(":");
            String meta = null;
            if (vs.length > 2) {
                meta = vs[2];
            }
            list.add(makeActivity(vs[0], name, vs[1], meta));
        }
    }
    if (type == null && (TestUtility.isTestDevice(this) || TestUtility.isEmulator())) {
        list.add(makeActivity("com.androidquery.test.AdhocActivity", "Ad Hoc Debug", "", null));
        list.add(makeActivity("com.androidquery.test.AdhocActivity2", "Ad Hoc Debug2", "", null));
    }
    ArrayAdapter<ActivityItem> result = new ArrayAdapter<ActivityItem>(this, R.layout.list_item, list) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }
            ActivityItem ai = (ActivityItem) getItem(position);
            AQuery aq = new AQuery(convertView);
            String text = ai.getName();
            String meta = ai.getMeta();
            if (meta != null) {
                text += "   <small><small><font color=\"red\">" + meta + "</font></small></small>";
            }
            Spanned span = Html.fromHtml(text);
            aq.id(R.id.name).text(span);
            return convertView;
        }
    };
    return result;
}
Also used : AQuery(com.androidquery.AQuery) ViewGroup(android.view.ViewGroup) View(android.view.View) AdapterView(android.widget.AdapterView) Spanned(android.text.Spanned) ArrayAdapter(android.widget.ArrayAdapter)

Example 28 with Spanned

use of android.text.Spanned in project platform_frameworks_base by android.

the class DateFormat method format.

/**
     * Given a format string and a {@link java.util.Calendar} object, returns a CharSequence
     * containing the requested date.
     * @param inFormat the format string, as described in {@link android.text.format.DateFormat}
     * @param inDate the date to format
     * @return a {@link CharSequence} containing the requested text
     */
public static CharSequence format(CharSequence inFormat, Calendar inDate) {
    SpannableStringBuilder s = new SpannableStringBuilder(inFormat);
    int count;
    LocaleData localeData = LocaleData.get(Locale.getDefault());
    int len = inFormat.length();
    for (int i = 0; i < len; i += count) {
        count = 1;
        int c = s.charAt(i);
        if (c == QUOTE) {
            count = appendQuotedText(s, i, len);
            len = s.length();
            continue;
        }
        while ((i + count < len) && (s.charAt(i + count) == c)) {
            count++;
        }
        String replacement;
        switch(c) {
            case 'A':
            case 'a':
                replacement = localeData.amPm[inDate.get(Calendar.AM_PM) - Calendar.AM];
                break;
            case 'd':
                replacement = zeroPad(inDate.get(Calendar.DATE), count);
                break;
            case 'c':
            case 'E':
                replacement = getDayOfWeekString(localeData, inDate.get(Calendar.DAY_OF_WEEK), count, c);
                break;
            // hour in am/pm (0-11)
            case 'K':
            case // hour in am/pm (1-12)
            'h':
                {
                    int hour = inDate.get(Calendar.HOUR);
                    if (c == 'h' && hour == 0) {
                        hour = 12;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            // hour in day (0-23)
            case 'H':
            case // hour in day (1-24) [but see note below]
            'k':
                {
                    int hour = inDate.get(Calendar.HOUR_OF_DAY);
                    // times are abusing 'k'. http://b/8359981.
                    if (false && c == 'k' && hour == 0) {
                        hour = 24;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            case 'L':
            case 'M':
                replacement = getMonthString(localeData, inDate.get(Calendar.MONTH), count, c);
                break;
            case 'm':
                replacement = zeroPad(inDate.get(Calendar.MINUTE), count);
                break;
            case 's':
                replacement = zeroPad(inDate.get(Calendar.SECOND), count);
                break;
            case 'y':
                replacement = getYearString(inDate.get(Calendar.YEAR), count);
                break;
            case 'z':
                replacement = getTimeZoneString(inDate, count);
                break;
            default:
                replacement = null;
                break;
        }
        if (replacement != null) {
            s.replace(i, i + count, replacement);
            // CARE: count is used in the for loop above
            count = replacement.length();
            len = s.length();
        }
    }
    if (inFormat instanceof Spanned) {
        return new SpannedString(s);
    } else {
        return s.toString();
    }
}
Also used : LocaleData(libcore.icu.LocaleData) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 29 with Spanned

use of android.text.Spanned in project platform_frameworks_base by android.

the class BaseInputConnection method ensureDefaultComposingSpans.

private void ensureDefaultComposingSpans() {
    if (mDefaultComposingSpans == null) {
        Context context;
        if (mTargetView != null) {
            context = mTargetView.getContext();
        } else if (mIMM.mServedView != null) {
            context = mIMM.mServedView.getContext();
        } else {
            context = null;
        }
        if (context != null) {
            TypedArray ta = context.getTheme().obtainStyledAttributes(new int[] { com.android.internal.R.attr.candidatesTextStyleSpans });
            CharSequence style = ta.getText(0);
            ta.recycle();
            if (style != null && style instanceof Spanned) {
                mDefaultComposingSpans = ((Spanned) style).getSpans(0, style.length(), Object.class);
            }
        }
    }
}
Also used : Context(android.content.Context) TypedArray(android.content.res.TypedArray) Spanned(android.text.Spanned)

Example 30 with Spanned

use of android.text.Spanned in project platform_frameworks_base by android.

the class TextView method onSaveInstanceState.

@Override
public Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();
    // Save state if we are forced to
    final boolean freezesText = getFreezesText();
    boolean hasSelection = false;
    int start = -1;
    int end = -1;
    if (mText != null) {
        start = getSelectionStart();
        end = getSelectionEnd();
        if (start >= 0 || end >= 0) {
            // Or save state if there is a selection
            hasSelection = true;
        }
    }
    if (freezesText || hasSelection) {
        SavedState ss = new SavedState(superState);
        if (freezesText) {
            if (mText instanceof Spanned) {
                final Spannable sp = new SpannableStringBuilder(mText);
                if (mEditor != null) {
                    removeMisspelledSpans(sp);
                    sp.removeSpan(mEditor.mSuggestionRangeSpan);
                }
                ss.text = sp;
            } else {
                ss.text = mText.toString();
            }
        }
        if (hasSelection) {
            // XXX Should also save the current scroll position!
            ss.selStart = start;
            ss.selEnd = end;
        }
        if (isFocused() && start >= 0 && end >= 0) {
            ss.frozenWithFocus = true;
        }
        ss.error = getError();
        if (mEditor != null) {
            ss.editorState = mEditor.saveInstanceState();
        }
        return ss;
    }
    return superState;
}
Also used : Parcelable(android.os.Parcelable) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spannable(android.text.Spannable) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

Spanned (android.text.Spanned)200 Paint (android.graphics.Paint)81 TextPaint (android.text.TextPaint)63 Spannable (android.text.Spannable)32 SpannableStringBuilder (android.text.SpannableStringBuilder)26 SuggestionSpan (android.text.style.SuggestionSpan)24 SpannableString (android.text.SpannableString)23 Editable (android.text.Editable)22 TextAppearanceSpan (android.text.style.TextAppearanceSpan)15 SpannedString (android.text.SpannedString)14 TypedArray (android.content.res.TypedArray)12 URLSpan (android.text.style.URLSpan)12 View (android.view.View)11 Context (android.content.Context)9 StyleSpan (android.text.style.StyleSpan)9 InputMethodManager (android.view.inputmethod.InputMethodManager)9 Parcelable (android.os.Parcelable)8 TextView (android.widget.TextView)8 WordIterator (android.text.method.WordIterator)7 CharacterStyle (android.text.style.CharacterStyle)7