Example usage for android.text SpannableString SpannableString

List of usage examples for android.text SpannableString SpannableString

Introduction

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

Prototype

public SpannableString(CharSequence source) 

Source Link

Document

For the backward compatibility reasons, this constructor copies all spans including android.text.NoCopySpan .

Usage

From source file:net.yoching.android.MainActivity.java

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open,
            R.string.drawer_close) {/*from  w w  w  .j a v  a 2  s . c om*/

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

            SpannableString s = new SpannableString("64 WREXAGRAMS");
            s.setSpan(new TypefaceSpan("Exo-Bold.otf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            getSupportActionBar().setTitle(s);

            // invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mActivityTitle);
            //   invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

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

public void setMyTitle(String t) {
    SpannableString s = new SpannableString(t);
    s.setSpan(new TypefaceSpan("sans-serif"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actionBar.setTitle(s);/*from  ww  w  .  ja va 2s .c  o  m*/
}

From source file:io.github.hidroh.materialistic.data.HackerNewsItem.java

@Override
public Spannable getDisplayedAuthor(Context context, boolean linkify, int color) {
    if (displayedAuthor == null) {
        if (TextUtils.isEmpty(by)) {
            displayedAuthor = new SpannableString("");
        } else {/*  w  w w  .ja v a  2  s .co  m*/
            defaultColor = ContextCompat.getColor(context, AppUtils.getThemedResId(context,
                    linkify ? android.R.attr.textColorLink : android.R.attr.textColorSecondary));
            displayedAuthor = createAuthorSpannable(linkify);
        }
    }
    if (displayedAuthor.length() == 0) {
        return displayedAuthor;
    }
    displayedAuthor.setSpan(new ForegroundColorSpan(color != 0 ? color : defaultColor),
            AUTHOR_SEPARATOR.length(), displayedAuthor.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    return displayedAuthor;
}

From source file:com.grepsound.fragments.MyProfileFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_myprofile, container, false);

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) rootView.findViewById(R.id.pager);

    mViewPager.setOffscreenPageLimit(2);
    mViewPager.setAdapter(mPagerAdapter);

    mHeader = rootView.findViewById(R.id.header);
    mPagerAdapter.setTabHolderScrollingContent(this);

    final PagerSlidingTabStrip strip = PagerSlidingTabStrip.class.cast(rootView.findViewById(R.id.pts_main));
    strip.setViewPager(mViewPager);/*ww w .j a v  a  2 s.  co m*/
    strip.setOnPageChangeListener(this);

    mUserCover = (ImageView) rootView.findViewById(R.id.user_cover);

    mSpannableString = new SpannableString(getString(R.string.app_name));

    mName = (TextView) rootView.findViewById(R.id.user_name);
    mFollowers = (TwoTextCounter) rootView.findViewById(R.id.followers_button);
    mFollowers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCallbacks.displayFollowers();
        }
    });
    mFollowing = (TwoTextCounter) rootView.findViewById(R.id.following_button);
    mFollowing.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCallbacks.displayFollowing();
        }
    });
    mCity = (TextView) rootView.findViewById(R.id.user_city);

    mFollowers.setLabel("followers");
    mFollowing.setLabel("following");

    mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(0xffffffff);

    return rootView;
}

From source file:com.android.example.notificationshowcase.NotificationService.java

@Override
protected void onHandleIntent(Intent intent) {
    ArrayList<Notification> mNotifications = new ArrayList<Notification>();
    NotificationManager noMa = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    int bigtextId = mNotifications.size();
    mNotifications.add(makeBigTextNotification(this, 0, bigtextId, System.currentTimeMillis()));

    int uploadId = mNotifications.size();
    long uploadWhen = System.currentTimeMillis();
    mNotifications.add(makeUploadNotification(this, 10, uploadWhen));

    Notification phoneCall = new NotificationCompat.Builder(this).setContentTitle("Incoming call")
            .setContentText("Matias Duarte").setLargeIcon(getBitmap(this, R.drawable.matias_hed))
            .setSmallIcon(R.drawable.stat_sys_phone_call).setDefaults(Notification.DEFAULT_SOUND)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Matias"))
            .addAction(R.drawable.ic_dial_action_call, "Answer",
                    ToastService.getPendingIntent(this, "call answered"))
            .addAction(R.drawable.ic_end_call, "Ignore", ToastService.getPendingIntent(this, "call ignored"))
            .setAutoCancel(true).build();
    phoneCall.flags |= Notification.FLAG_INSISTENT;
    mNotifications.add(phoneCall);/*from w  w w  .  ja va2s .  com*/

    mNotifications.add(
            new NotificationCompat.Builder(this).setContentTitle("Stopwatch PRO").setContentText("Counting up")
                    .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Stopwatch"))
                    .setSmallIcon(R.drawable.stat_notify_alarm).setUsesChronometer(true).build());

    mNotifications.add(
            new NotificationCompat.Builder(this).setContentTitle("J Planning").setContentText("The Botcave")
                    .setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.stat_notify_calendar)
                    .setContentIntent(ToastService.getPendingIntent(this, "Clicked on calendar event"))
                    .setContentInfo("7PM")
                    .addAction(R.drawable.stat_notify_snooze, "+10 min",
                            ToastService.getPendingIntent(this, "snoozed 10 min"))
                    .addAction(R.drawable.stat_notify_snooze_longer, "+1 hour",
                            ToastService.getPendingIntent(this, "snoozed 1 hr"))
                    .addAction(R.drawable.stat_notify_email, "Email",
                            makeEmailIntent(this, "gabec@example.com,mcleron@example.com,dsandler@example.com"))
                    .build());

    BitmapDrawable d = (BitmapDrawable) getResources().getDrawable(R.drawable.romainguy_rockaway);
    mNotifications.add(new NotificationCompat.BigPictureStyle(new NotificationCompat.Builder(this)
            .setContentTitle("Romain Guy")
            .setContentText("I was lucky to find a Canon 5D Mk III at a local Bay Area "
                    + "store last week but I had not been able to try it in the field "
                    + "until tonight. After a few days of rain the sky finally cleared "
                    + "up. Rockaway Beach did not disappoint and I was finally able to "
                    + "see what my new camera feels like when shooting landscapes.")
            .setSmallIcon(R.drawable.ic_stat_gplus)
            .setContentIntent(ToastService.getPendingIntent(this, "Clicked on bigPicture"))
            .setLargeIcon(getBitmap(this, R.drawable.romainguy_hed))
            .addAction(R.drawable.add, "Add to Gallery",
                    ToastService.getPendingIntent(this, "added! (just kidding)"))
            .setSubText("talk rocks!")).bigPicture(d.getBitmap()).build());

    // Note: this may conflict with real email notifications
    StyleSpan bold = new StyleSpan(Typeface.BOLD);
    SpannableString line1 = new SpannableString("Alice: hey there!");
    line1.setSpan(bold, 0, 5, 0);
    SpannableString line2 = new SpannableString("Bob: hi there!");
    line2.setSpan(bold, 0, 3, 0);
    SpannableString line3 = new SpannableString("Charlie: Iz IN UR EMAILZ!!");
    line3.setSpan(bold, 0, 7, 0);
    mNotifications.add(new NotificationCompat.InboxStyle(
            new NotificationCompat.Builder(this).setContentTitle("24 new messages")
                    .setContentText("You have mail!").setSubText("test.hugo2@gmail.com")
                    .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Email"))
                    .setSmallIcon(R.drawable.stat_notify_email)).setSummaryText("+21 more").addLine(line1)
                            .addLine(line2).addLine(line3).build());

    mNotifications
            .add(new NotificationCompat.Builder(this).setContentTitle("Twitter").setContentText("New mentions")
                    .setContentIntent(ToastService.getPendingIntent(this, "Clicked on Twitter"))
                    .setSmallIcon(R.drawable.twitter_icon).setNumber(15)
                    .setPriority(NotificationCompat.PRIORITY_LOW).build());

    for (int i = 0; i < mNotifications.size(); i++) {
        noMa.notify(NOTIFICATION_ID + i, mNotifications.get(i));
    }

    ProgressService.startProgressUpdater(this, uploadId, uploadWhen, 0);
}

From source file:com.aniruddhc.acemusic.player.MusicLibraryEditorActivity.MusicLibraryEditorActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.add_to_music_library, menu);

    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(UIElementsHelper.getGeneralActionBarBackground(mContext));
    actionBar.setIcon(// w  w  w  . j a  v a 2s  . c o  m
            mContext.getResources().getIdentifier(libraryIconName, "drawable", mContext.getPackageName()));
    SpannableString s = new SpannableString(libraryName);
    s.setSpan(new TypefaceSpan(this, "RobotoCondensed-Light"), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actionBar.setTitle(s);

    return super.onCreateOptionsMenu(menu);
}

From source file:com.github.socialc0de.gsw.android.MainActivity.java

private void fillLayout() {
    SpannableString s = new SpannableString(getString(R.string.app_name));
    s.setSpan(new com.github.socialc0de.gsw.android.TypefaceSpan(getApplicationContext(), "fabiolo.otf"), 0,
            s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ((Toolbar) findViewById(R.id.app_bar)).setTitle(s);
    mTinyDB = new TinyDB(this);
    loadCategories();//  www.  ja  va  2s.c o m
    loadAccount();
    if (mDrawer != null && mFragments != null) {
        for (int i = 0; i < mFragments.size(); i++) {
            mDrawer.removeItem(i);
        }
    }

    //create the drawer and remember the `Drawer` result object

    headerResult = new AccountHeaderBuilder().withActivity(this).withHeaderBackground(R.drawable.productback)
            .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                @Override
                public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                    return false;
                }
            }).withSelectionListEnabledForSingleProfile(false).build();

    mFragments.clear();

    mFragments.add(new LocalFragment(this));
    mFragments.add(new CategoryFragment(this));
    mFragments.add(new ProfileFragment());
    mFragments.add(new PhraseFragment());
    mFragments.add(new FAQFragment());
    mFragments.add(new AuthorityMapFragment());
    mFragments.add(new DonateFragment());
    mFragments.add(new AboutFragment());

    if (mDrawer == null) {
        mDrawer = new DrawerBuilder().withActivity(this).withToolbar((Toolbar) findViewById(R.id.app_bar))
                .withActionBarDrawerToggle(true).withAccountHeader(headerResult)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName(R.string.sharing_local)
                                .withIcon(R.drawable.ic_place_black_36dp),
                        new PrimaryDrawerItem().withName(R.string.sharing_categories)
                                .withIcon(R.drawable.ic_view_day_black_36dp),
                        new PrimaryDrawerItem().withName(R.string.profile)
                                .withIcon(R.drawable.ic_assignment_ind_black_36dp),
                        new PrimaryDrawerItem().withName(R.string.phrasebook)
                                .withIcon(R.drawable.ic_translate_black_36dp),
                        new PrimaryDrawerItem().withName(R.string.faq)
                                .withIcon(R.drawable.ic_question_answer_black_36dp),
                        new PrimaryDrawerItem().withName(R.string.authority_map)
                                .withIcon(R.drawable.ic_map_black_36dp),
                        new PrimaryDrawerItem().withName(R.string.donate)
                                .withIcon(R.drawable.ic_attach_money_black_36dp),
                        new PrimaryDrawerItem().withName(R.string.about)
                                .withIcon(R.drawable.ic_info_black_36dp))
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        // do something with the clicked item :D
                        if (getSupportFragmentManager().findFragmentByTag(
                                mFragments.get(position - 1).getClass().getSimpleName()) != null)
                            return false;
                        getSupportFragmentManager().popBackStack(null,
                                FragmentManager.POP_BACK_STACK_INCLUSIVE);
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.container, mFragments.get(position - 1),
                                        mFragments.get(position - 1).getClass().getSimpleName())
                                .addToBackStack(null).commit();

                        // closes Drawer
                        return false;
                    }
                }).build();
    }

    getSupportFragmentManager().beginTransaction().replace(R.id.container, mFragments.get(0)).commit();
}

From source file:dev.drsoran.moloko.util.UIUtils.java

public final static void makeLink(TextView textView, String text, ClickableSpan onClickHandler) {
    final SpannableString spannableText = new SpannableString(text);
    makeLink(textView, spannableText, onClickHandler);
}

From source file:com.doomonafireball.hackerswiperfree.android.activity.MainActivity.java

private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {
    SpannableString link = new SpannableString(text);
    link.setSpan(new ClickableString(listener), 0, text.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
    return link;//from   www  .j a  v a 2 s.c om
}

From source file:com.amazon.android.ui.widget.EllipsizedTextView.java

/**
 * Sets ellipsis properties.//from   ww  w  .ja v  a  2 s  . c o m
 *
 * @param widthMeasureSpec  Ellipsis width.
 * @param heightMeasureSpec Ellipsis height.
 * @param layout            Layout for ellipsis.
 * @param lastLine          Last line length for ellipsis.
 * @param maxLines          Max lines in ellipsis.
 */
private void setEllipsis(int widthMeasureSpec, int heightMeasureSpec, Layout layout, int lastLine,
        int maxLines) {

    mIsEllipsized = true;
    setFocusable(true);
    setClickable(true);
    final SpannableString ss = new SpannableString(mCharSequence);
    String visibleText = mCharSequence.toString();

    mEllipsisImage = new StateImageSpan(getContext(), mGuillemetDrawableId, ImageSpan.ALIGN_BASELINE);

    final SpannableStringBuilder spannedText = new SpannableStringBuilder();
    int ellipsisIndex = layout.getLineStart(Math.min(lastLine + 1, maxLines));

    // Keep chopping words off until the ellipsis is on a visible line or there is only one
    // line left.
    do {
        // Only truncate the last line for long description.
        if (lastLine >= maxLines) {
            // Getting the first word break index before the last index of maxline.
            int safeBreakIndex = breakBefore(visibleText, ellipsisIndex, BreakIterator.getWordInstance());
            final int maxLineStart = layout.getLineStart(maxLines - 1);

            // If this check pass, it means we just truncated a word that is longer than a line.
            if (safeBreakIndex < maxLineStart) {
                // Need to check character by character and break in the middle now. Checking
                // word by word should cover most cases, only do this if a word is longer than
                // line width.
                safeBreakIndex = breakBefore(visibleText, ellipsisIndex, BreakIterator.getCharacterInstance());
            }
            ellipsisIndex = safeBreakIndex;
        }

        visibleText = visibleText.substring(0, ellipsisIndex);
        final CharSequence charOutput = ss.subSequence(0, ellipsisIndex);

        // Re-add ellipsis and convert to image
        spannedText.replace(0, spannedText.length(), charOutput);
        spannedText.append(ELLIPSIS);

        spannedText.setSpan(mEllipsisImage, ellipsisIndex, ellipsisIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        // Reset text and re-measure.
        super.setText(spannedText, BufferType.SPANNABLE);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    } while (getLineCount() > getMaxLines() && getLineCount() > 1);
    requestFocus();
}