SpannableString example

SpannableString


Code in Activity:


        final TextView textView = (TextView) findViewById(R.id.textView);
        SpannableString spannableString = 
                    new SpannableString("Spannable String Example\n\nClickable");
        spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, 0); // S
        spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 1, 2, 0); // p
        spannableString.setSpan(new StyleSpan(Typeface.BOLD), 2, 3, 0); // a
        spannableString.setSpan(new StyleSpan(Typeface.ITALIC), 3, 4, 0); // n
        spannableString.setSpan(new UnderlineSpan(), 4, 5, 0); // n
        spannableString.setSpan(new BackgroundColorSpan(Color.YELLOW), 5, 6, 0); // a
        spannableString.setSpan(new BackgroundColorSpan(Color.GREEN), 6, 7, 0); // b

        spannableString.setSpan(new RelativeSizeSpan(2f), 7, 8, 0); // l
        spannableString.setSpan(new RelativeSizeSpan(0.5f), 8, 9, 0); // e
        spannableString.setSpan(new StrikethroughSpan(), 9, 10, 0); // S
        spannableString.setSpan(new SuperscriptSpan(), 10, 11, 0); // t
        spannableString.setSpan(new SubscriptSpan(), 11, 12, 0); // r


        spannableString.setSpan(
             new URLSpan("http://androidlibs.net/alexandroid"), 17, 24, 0); // Example

        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Snackbar.make(textView, "Click", Snackbar.LENGTH_SHORT).show();
            }
        };

        spannableString.setSpan(clickableSpan, 25, spannableString.length(), 0);

        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString);

XML:


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="net.alexandroid.test2.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:gravity="center"
        app:layout_constraintLeft_toLeftOf="@+id/content_main"
        app:layout_constraintTop_toTopOf="@+id/content_main"
        app:layout_constraintRight_toRightOf="@+id/content_main"
        app:layout_constraintBottom_toBottomOf="@+id/content_main"
        app:layout_constraintVertical_bias="0.45" />

</android.support.constraint.ConstraintLayout>

developer.android.com


One Comments

  • Слава

    June 27, 2016

    Раскатать бы как подключится.Или как отключиться.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.