Example usage for android.text.style StyleSpan StyleSpan

List of usage examples for android.text.style StyleSpan StyleSpan

Introduction

In this page you can find the example usage for android.text.style StyleSpan StyleSpan.

Prototype

public StyleSpan(@NonNull Parcel src) 

Source Link

Document

Creates a StyleSpan from a parcel.

Usage

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

/**
 * Return the layout for a numbered event. Create it if not already existing
 *///  ww w .j  ava 2  s  . c om
private StaticLayout getEventLayout(StaticLayout[] layouts, int i, Event event, Paint paint, Rect r) {
    if (i < 0 || i >= layouts.length) {
        return null;
    }

    StaticLayout layout = layouts[i];
    // Check if we have already initialized the StaticLayout and that
    // the width hasn't changed (due to vertical resizing which causes
    // re-layout of events at min height)
    if (layout == null || r.width() != layout.getWidth()) {
        SpannableStringBuilder bob = new SpannableStringBuilder();
        if (event.title != null) {
            // MAX - 1 since we add a space
            bob.append(drawTextSanitizer(event.title.toString(), MAX_EVENT_TEXT_LEN - 1));
            bob.setSpan(new StyleSpan(Typeface.BOLD), 0, bob.length(), 0);
            bob.append(' ');
        }
        if (event.location != null) {
            bob.append(drawTextSanitizer(event.location.toString(), MAX_EVENT_TEXT_LEN - bob.length()));
        }

        paint.setColor(mEventTextColor);

        // Leave a one pixel boundary on the left and right of the rectangle for the event
        layout = new StaticLayout(bob, 0, bob.length(), new TextPaint(paint), r.width(), Alignment.ALIGN_NORMAL,
                1.0f, 0.0f, true, null, r.width());

        layouts[i] = layout;
    }
    layout.getPaint().setAlpha(mEventsAlpha);
    return layout;
}

From source file:com.hichinaschool.flashcards.libanki.Sched.java

/**
 * Deck finished state ******************************************************
 * *****************************************
 *//*from  www .  j av  a2 s  . c o m*/

public CharSequence finishedMsg(Context context) {
    SpannableStringBuilder sb = new SpannableStringBuilder();
    sb.append(context.getString(R.string.studyoptions_congrats_finished));
    StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
    sb.setSpan(boldSpan, 0, sb.length(), 0);
    sb.append(_nextDueMsg(context));
    // sb.append("\n\n");
    // sb.append(_tomorrowDueMsg(context));
    return sb;
}

From source file:ru.valle.btc.MainActivity.java

private CharSequence getPrivateKeyTypeLabel(final KeyPair keyPair) {
    int typeWithCompression = keyPair.privateKey.type == BTCUtils.PrivateKeyInfo.TYPE_BRAIN_WALLET
            && keyPair.privateKey.isPublicKeyCompressed ? keyPair.privateKey.type + 1 : keyPair.privateKey.type;
    CharSequence keyType = getResources().getTextArray(R.array.private_keys_types)[typeWithCompression];
    SpannableString keyTypeLabel = new SpannableString(getString(R.string.private_key_type, keyType));
    int keyTypeStart = keyTypeLabel.toString().indexOf(keyType.toString());
    keyTypeLabel.setSpan(new StyleSpan(Typeface.BOLD), keyTypeStart, keyTypeStart + keyType.length(),
            SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE);
    if (keyPair.privateKey.type == BTCUtils.PrivateKeyInfo.TYPE_BRAIN_WALLET) {
        String compressionStrToSpan = keyType.toString().substring(keyType.toString().indexOf(',') + 2);
        int start = keyTypeLabel.toString().indexOf(compressionStrToSpan);
        if (start >= 0) {

            ClickableSpan switchPublicKeyCompressionSpan = new ClickableSpan() {
                @Override//from   w  w w .  j  a  v a  2 s  .  c  om
                public void onClick(View widget) {
                    cancelAllRunningTasks();
                    switchingCompressionTypeTask = new AsyncTask<Void, Void, KeyPair>() {

                        @Override
                        protected KeyPair doInBackground(Void... params) {
                            return new KeyPair(new BTCUtils.PrivateKeyInfo(keyPair.privateKey.type,
                                    keyPair.privateKey.privateKeyEncoded, keyPair.privateKey.privateKeyDecoded,
                                    !keyPair.privateKey.isPublicKeyCompressed));
                        }

                        @Override
                        protected void onPostExecute(KeyPair keyPair) {
                            switchingCompressionTypeTask = null;
                            onKeyPairModify(false, keyPair);
                        }
                    };
                    switchingCompressionTypeTask.execute();
                }
            };
            keyTypeLabel.setSpan(switchPublicKeyCompressionSpan, start, start + compressionStrToSpan.length(),
                    SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE);
        }
    }
    return keyTypeLabel;
}

From source file:cgeo.geocaching.CacheDetailActivity.java

private void addWarning(final UnknownTagsHandler unknownTagsHandler, final Editable description) {
    if (unknownTagsHandler.isProblematicDetected()) {
        final int startPos = description.length();
        final IConnector connector = ConnectorFactory.getConnector(cache);
        if (StringUtils.isNotEmpty(cache.getUrl())) {
            final Spanned tableNote = Html.fromHtml(res.getString(R.string.cache_description_table_note,
                    "<a href=\"" + cache.getUrl() + "\">" + connector.getName() + "</a>"));
            description.append("\n\n").append(tableNote);
            description.setSpan(new StyleSpan(Typeface.ITALIC), startPos, description.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }//from  w w  w.j  a  va2 s  . c om
    }
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void showAccessNotification(int uid) {
    String name = TextUtils.join(", ", Util.getApplicationNames(uid, ServiceSinkhole.this));

    Intent main = new Intent(ServiceSinkhole.this, ActivityMain.class);
    main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
    PendingIntent pi = PendingIntent.getActivity(ServiceSinkhole.this, uid + 10000, main,
            PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorOn, tv, true);
    int colorOn = tv.data;
    getTheme().resolveAttribute(R.attr.colorOff, tv, true);
    int colorOff = tv.data;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_cloud_upload_white_24dp).setGroup("AccessAttempt")
            .setContentTitle(getString(R.string.app_name)).setContentText(getString(R.string.msg_access, name))
            .setContentIntent(pi).setColor(colorOff).setOngoing(false).setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);
    }//w ww .j  a  v a  2 s  .  co  m

    DateFormat df = new SimpleDateFormat("dd HH:mm");

    NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);
    String sname = getString(R.string.msg_access, name);
    int pos = sname.indexOf(name);
    Spannable sp = new SpannableString(sname);
    sp.setSpan(new StyleSpan(Typeface.BOLD), pos, pos + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    notification.addLine(sp);

    Cursor cursor = DatabaseHelper.getInstance(ServiceSinkhole.this).getAccessUnset(uid, 7);
    int colDAddr = cursor.getColumnIndex("daddr");
    int colTime = cursor.getColumnIndex("time");
    int colAllowed = cursor.getColumnIndex("allowed");
    while (cursor.moveToNext()) {
        StringBuilder sb = new StringBuilder();
        sb.append(df.format(cursor.getLong(colTime))).append(' ');

        String daddr = cursor.getString(colDAddr);
        if (Util.isNumericAddress(daddr))
            try {
                daddr = InetAddress.getByName(daddr).getHostName();
            } catch (UnknownHostException ignored) {
            }
        sb.append(daddr);

        int allowed = cursor.getInt(colAllowed);
        if (allowed >= 0) {
            pos = sb.indexOf(daddr);
            sp = new SpannableString(sb);
            ForegroundColorSpan fgsp = new ForegroundColorSpan(allowed > 0 ? colorOn : colorOff);
            sp.setSpan(fgsp, pos, pos + daddr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        notification.addLine(sp);
    }
    cursor.close();

    NotificationManagerCompat.from(this).notify(uid + 10000, notification.build());
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static CharSequence getKeywordBoldedText(final CharSequence orig, final String... keywords) {
    return getKeywordHighlightedText(orig, new StyleSpan(Typeface.BOLD), keywords);
}

From source file:com.ichi2.anki2.Reviewer.java

private void displayCardQuestion() {
    // show timer, if activated in the deck's preferences
    initTimer();//from w w w  .  ja  va 2 s. c  om

    sDisplayAnswer = false;

    if (mButtonHeight == 0 && mRelativeButtonSize != 100) {
        mButtonHeight = mFlipCard.getHeight() * mRelativeButtonSize / 100;
        mFlipCard.setHeight(mButtonHeight);
        mEase1.setHeight(mButtonHeight);
        mEase2.setHeight(mButtonHeight);
        mEase3.setHeight(mButtonHeight);
        mEase4.setHeight(mButtonHeight);
    }

    setInterface();

    String question = mCurrentCard.getQuestion(mCurrentSimpleInterface);
    question = typeAnsQuestionFilter(question);

    if (mPrefFixArabic) {
        question = ArabicUtilities.reshapeSentence(question, true);
    }

    Log.i(AnkiDroidApp.TAG, "question: '" + question + "'");

    String displayString = "";

    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(question);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(
                    getResources().getString(R.string.simple_interface_hint, R.string.card_details_question));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        // If the user wants to write the answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.VISIBLE);

            // Show soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(mAnswerField, InputMethodManager.SHOW_FORCED);
        }

        displayString = enrichWithQADiv(question, false);

        if (mSpeakText) {
            // ReadText.setLanguageInformation(Model.getModel(DeckManager.getMainDeck(),
            // mCurrentCard.getCardModelId(), false).getId(), mCurrentCard.getCardModelId());
        }

        if (mPrefRecord) {
            Recorder.reset();
            // check for auto record
            try {
                if (mSched.getCol().getDecks().confForDid(mCurrentCard.getDid()).getBoolean("autoRecord")) {
                    Recorder.startRecording();
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }

    }

    updateCard(displayString);
    hideEaseButtons();

    // If the user want to show answer automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowAnswerTask);
        mTimeoutHandler.postDelayed(mShowAnswerTask, mWaitAnswerSecond * 1000);
    }
}

From source file:com.ichi2.anki2.Reviewer.java

private void displayCardAnswer() {
    Log.i(AnkiDroidApp.TAG, "displayCardAnswer");

    // prevent answering (by e.g. gestures) before card is loaded
    if (mCurrentCard == null) {
        return;//from w  w w. j a  v  a2  s  . co  m
    }

    sDisplayAnswer = true;
    setFlipCardAnimation();

    String answer = mCurrentCard.getAnswer(mCurrentSimpleInterface);
    answer = typeAnsAnswerFilter(answer);

    String displayString = "";

    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(answer);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(
                    getResources().getString(R.string.simple_interface_hint, R.string.card_details_answer));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        Sound.stopSounds();
        Recorder.stopRecording();

        if (mPrefFixArabic) {
            // reshape
            answer = ArabicUtilities.reshapeSentence(answer, true);
        }

        // If the user wrote an answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.GONE);
            if (mCurrentCard != null) {
                if (mPrefFixArabic) {
                    // reshape
                    mTypeCorrect = ArabicUtilities.reshapeSentence(mTypeCorrect, true);
                }
                // Obtain the user answer and the correct answer
                String userAnswer = mAnswerField.getText().toString();
                Matcher matcher = sSpanPattern.matcher(Utils.stripHTMLMedia(mTypeCorrect));
                String correctAnswer = matcher.replaceAll("");
                matcher = sBrPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("\n");
                matcher = Sound.sSoundPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("");
                Log.i(AnkiDroidApp.TAG, "correct answer = " + correctAnswer);

                // Obtain the diff and send it to updateCard
                DiffEngine diff = new DiffEngine();

                StringBuffer span = new StringBuffer();
                span.append("<span style=\"font-family: '").append(mTypeFont).append("'; font-size: ")
                        .append(mTypeSize).append("px\">");
                span.append(diff.diff_prettyHtml(diff.diff_main(userAnswer, correctAnswer), mNightMode));
                span.append("</span>");
                span.append("<br/>").append(answer);
                displayString = enrichWithQADiv(span.toString(), true);
            }

            // Hide soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mAnswerField.getWindowToken(), 0);
        } else {
            displayString = enrichWithQADiv(answer, true);
        }
    }

    mIsSelecting = false;
    updateCard(displayString);
    showEaseButtons();

    // If the user want to show next question automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowQuestionTask);
        mTimeoutHandler.postDelayed(mShowQuestionTask, mWaitQuestionSecond * 1000);
    }
}

From source file:com.hichinaschool.flashcards.anki.Reviewer.java

private void displayCardQuestion() {
    // show timer, if activated in the deck's preferences
    initTimer();/*from  w w w.ja v  a  2s  . c  o  m*/

    sDisplayAnswer = false;

    if (mButtonHeight == 0 && mRelativeButtonSize != 100) {
        mButtonHeight = mFlipCard.getHeight() * mRelativeButtonSize / 100;
        mFlipCard.setHeight(mButtonHeight);
        mEase1.setHeight(mButtonHeight);
        mEase2.setHeight(mButtonHeight);
        mEase3.setHeight(mButtonHeight);
        mEase4.setHeight(mButtonHeight);
    }

    setInterface();

    String question = mCurrentCard.getQuestion(mCurrentSimpleInterface);
    question = typeAnsQuestionFilter(question);

    if (mPrefFixArabic) {
        question = ArabicUtilities.reshapeSentence(question, true);
    }

    // Log.i(AnkiDroidApp.TAG, "question: '" + question + "'");

    String displayString = "";

    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(question);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(
                    getResources().getString(R.string.simple_interface_hint, R.string.card_details_question));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        // If the user wants to write the answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.VISIBLE);

            // Show soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(mAnswerField, InputMethodManager.SHOW_FORCED);
        }

        displayString = enrichWithQADiv(question, false);

        if (mSpeakText) {
            // ReadText.setLanguageInformation(Model.getModel(DeckManager.getMainDeck(),
            // mCurrentCard.getCardModelId(), false).getId(), mCurrentCard.getCardModelId());
        }
    }

    updateCard(displayString);
    hideEaseButtons();

    // If the user want to show answer automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowAnswerTask);
        mTimeoutHandler.postDelayed(mShowAnswerTask, mWaitAnswerSecond * 1000);
    }
}

From source file:com.hichinaschool.flashcards.anki.Reviewer.java

private void displayCardAnswer() {
    // Log.i(AnkiDroidApp.TAG, "displayCardAnswer");

    // prevent answering (by e.g. gestures) before card is loaded
    if (mCurrentCard == null) {
        return;/*from w  w w  .j a v  a2  s. co m*/
    }

    sDisplayAnswer = true;
    setFlipCardAnimation();

    String answer = mCurrentCard.getAnswer(mCurrentSimpleInterface);
    answer = typeAnsAnswerFilter(answer);

    String displayString = "";

    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(answer);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(
                    getResources().getString(R.string.simple_interface_hint, R.string.card_details_answer));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        Sound.stopSounds();

        if (mPrefFixArabic) {
            // reshape
            answer = ArabicUtilities.reshapeSentence(answer, true);
        }

        // If the user wrote an answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.GONE);
            if (mCurrentCard != null) {
                if (mPrefFixArabic) {
                    // reshape
                    mTypeCorrect = ArabicUtilities.reshapeSentence(mTypeCorrect, true);
                }
                // Obtain the user answer and the correct answer
                String userAnswer = mAnswerField.getText().toString();
                Matcher matcher = sSpanPattern.matcher(Utils.stripHTMLMedia(mTypeCorrect));
                String correctAnswer = matcher.replaceAll("");
                matcher = sBrPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("\n");
                matcher = Sound.sSoundPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("");
                // Log.i(AnkiDroidApp.TAG, "correct answer = " + correctAnswer);

                // Obtain the diff and send it to updateCard
                DiffEngine diff = new DiffEngine();

                StringBuffer span = new StringBuffer();
                span.append("<span style=\"font-family: '").append(mTypeFont).append("'; font-size: ")
                        .append(mTypeSize).append("px\">");
                span.append(diff.diff_prettyHtml(diff.diff_main(userAnswer, correctAnswer), mNightMode));
                span.append("</span>");
                span.append("<br/>").append(answer);
                displayString = enrichWithQADiv(span.toString(), true);
            }

            // Hide soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mAnswerField.getWindowToken(), 0);
        } else {
            displayString = enrichWithQADiv(answer, true);
        }
    }

    mIsSelecting = false;
    updateCard(displayString);
    showEaseButtons();

    // If the user want to show next question automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowQuestionTask);
        mTimeoutHandler.postDelayed(mShowQuestionTask, mWaitQuestionSecond * 1000);
    }
}