Snippety Save

A wrapper class on top of SpannableStringBuilder with utility methods for android and custom spans.

Project README

snippety-android

A wrapper class on top of SpannableStringBuilder with utility methods for android and custom spans. You can trust Snippety and Truss (by Jake Warton) to write cool text snippets which might be a pain in the neck otherwise.

Installation

Add the following to your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Add the following dependency to your app build.gradle:

dependencies {
    compile 'com.github.fueled:snippety:{latest_version}'
}

How to use

Using Truss

Truss returns a CharSequence. There are 2 ways of using it:

  1. Nested append : pushSpan(Span), append(String) and popSpan()
CharSequence text = new Truss()
        .pushSpan(new BackgroundColorSpan(Color.RED))
        .append("Hello Snippety")
        .popSpan()
        .build();

  1. Inline append : append(String, Span)
CharSequence text = new Truss()
        .append("Hello Snippety", new BackgroundColorSpan(Color.RED))
        .build();

Using Snippety

Snippety could be thought of as a collection of different span(s) which are anything but wrappers around the actual span(s).

CharSequence text = new Truss()
        .append("Hello Snippety", new Snippety().backgroundColor(Color.RED))
        .build();

You can also add multiple span attributes at a time.

CharSequence text = new Truss()
        .append("Hello Snippety", new Snippety().backgroundColor(Color.RED).textColor(Color.WHITE))
        .build();

Finally

Set the CharSequence returned by Truss to your TextView.

textView.setText(new Truss()
        .append(new Snippety().backgroundColor(Color.RED).textColor(Color.WHITE))
        .build());

Attach OnClickListener to some text:

textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(new Truss()
        .append("Click Me!", new Snippety.OnClickListener() {
                                @Override
                                public void onClick() {
                                    Toast.makeText(getContext(), "Oooh it tickles!", Toast.LENGTH_SHORT).show();
                                }
                            })
        .build());

Demo

Here is how you can achieve the demo screenshot attached

textView.setText(new Truss()

        .appendSelectiveln("With Snippety, you can use:", "Snippety",
             new Snippety().textColor(Color.RED))
        .appendln()

        .pushSpan(new Snippety().typeface(typeface)) // TextTypefaceSpan
        .appendln("typeface for TypefaceSpan")
        .popSpan()

        .appendln("fontStyle for StyleSpan",
             new Snippety().fontStyle(Snippety.FontStyle.BOLD))  //  StyleSpan

        .appendln("fontStyle for Stylespan",
             new Snippety().fontStyle(Snippety.FontStyle.ITALIC))    //  StyleSpan

        .appendln("textColor for ForegroundColorSpan",
             new Snippety().textColor(Color.MAGENTA))    //  ForegroundColorSpan

        .appendln("backgroundColor for BackgroundColorSpan",
             new Snippety().backgroundColor(Color.YELLOW))   //  BackgroundColorSpan

        .appendln("roundedBackgroundColor\nfor RoundedBackgroundSpan",
             new Snippety().roundedBackgroundColor(Color.RED, Color.WHITE))  //  RoundedBackgroundSpan

        .appendln("textSizeAbsolute for AbsoluteSizeSpan",
             new Snippety().textSizeAbsolute(textSize))  //  AbsoluteSizeSpan

        .appendln("textSizeRelative for RelativeSizeSpan",
             new Snippety().textSizeRelative(1.2f))    //  RelativeSizeSpan

        .appendln("textMultiColor for MultiColorSpan",
             new Snippety().textMultiColor(colorsRainbow))   //  MultiColorSpan

        .appendln("underline for UnderlineSpan",
             new Snippety().underline()) //  UnderlineSpan

        .append("image for ImageSpan")
        .appendln(new Snippety().image(drawable)) //  ImageSpan

        .appendln("quote for QuoteSpan",
             new Snippety().quote(Color.RED))    //  QuoteSpan

        .appendln("strikethrough for StrikethroughSpan",
             new Snippety().strikethrough()) //  StrikethroughSpan

        .appendln("align for AlignmentSpan",
             new Snippety().align(Snippety.Indent.RIGHT))  //  AlignmentSpan

        .appendln("url for URLSpan",
             new Snippety().url("http://developer.android.com")) //  URLSpan

        .appendln("addOnClickListener for ClickableSpan",
             new Snippety().textColor(Color.BLUE).addOnClickListener(new Snippety.OnClickListener() {
                 @Override
                 public void onClick() {
                     Toast.makeText(getContext(), "Thanks for stopping by!", Toast.LENGTH_SHORT).show();
                 }
             })) //  ClickableSpan
        .build();

Snippety Snippets

Here are some code snippets for Snippety spans (tongue twister :D)

Text Helper Spans

  • Typeface
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), getString(R.string.font_sunshiney));
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().typeface(typeface))
        .build());
  • Font Style
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().fontStyle(Snippety.FontStyle.BOLD))
        .build());
  • Image Drawable
Drawable drawable = ContextCompat.getDrawable(getContext(), R.mipmap.ic_launcher);
textView.setText(new Truss()
        .append(new Snippety().image(drawable))
        .build());
  • Align
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().align(Snippety.Indent.RIGHT))
        .build());
  • Absolute Text Size
int textSize = getResources().getDimensionPixelOffset(R.dimen.text_large);
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().textSizeAbsolute(textSize))
        .build());
  • Relative Text Size
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().textSizeRelative(1.2f))
        .build());
  • Background Color
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().backgroundColor(Color.RED))
        .build());
  • Rounded Background Color
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().roundedBackgroundColor(Color.RED, Color.WHITE))
        .build());
  • Text Color
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().textColor(Color.BLUE))
        .build());
  • Text Multi Color
int[] colorsRainbow = getResources().getIntArray(R.array.rainbow);
textView.setText(new Truss()
        .append("Hello Snippety", new Snippety().textMultiColor(colorsRainbow))
        .build());

HTML Helper Spans

  • Ordered List
int leadWidth = getResources().getDimensionPixelOffset(R.dimen.space_medium);
int gapWidth = getResources().getDimensionPixelOffset(R.dimen.space_xlarge);
textView.setText(new Truss()
        .appendln("Number One", new Snippety().number(leadWidth, gapWidth, 1))
        .appendln("Number Two", new Snippety().number(leadWidth, gapWidth, 2))
        .build());
  • Unordered List
int leadWidth = getResources().getDimensionPixelOffset(R.dimen.space_medium);
int gapWidth = getResources().getDimensionPixelOffset(R.dimen.space_xlarge);
textView.setText(new Truss()
        .appendln("Bullet One", new Snippety().bullet(leadWidth, gapWidth))
        .appendln("Bullet Two", new Snippety().bullet(leadWidth, gapWidth))
        .build());
  • Custom Unordered List
int leadWidth = getResources().getDimensionPixelOffset(R.dimen.space_medium);
int gapWidth = getResources().getDimensionPixelOffset(R.dimen.space_xlarge);
textView.setText(new Truss()
        .appendln("Custom Bullet One", new Snippety().bullet(leadWidth, gapWidth, "I."))
        .appendln("Custom Bullet Two", new Snippety().bullet(leadWidth, gapWidth, "II."))
        .build());
  • Image Unordered List
Drawable drawable = ContextCompat.getDrawable(getContext(), R.mipmap.ic_launcher);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_star_black_24dp);
int padding = getResources().getDimensionPixelOffset(R.dimen.space_medium);
textView.setText(new Truss()
        .appendln("Image Bullet One", new Snippety().bullet(bitmap, padding))
        .appendln("Image Bullet Two", new Snippety().bullet(bitmap, padding))
        .build());
  • Horizontal Line
int lineWidth = getResources().getDimensionPixelOffset(R.dimen.one_dp);
int lineColor = ContextCompat.getColor(getContext(), R.color.grey_light);
textView.setText(new Truss()
        .appendln(new Snippety().hr(lineWidth, lineColor))
        .build());

Reference

License

Copyright 2017 Fueled

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Open Source Agenda is not affiliated with "Snippety" Project. README Source: Fueled/snippety

Open Source Agenda Badge

Open Source Agenda Rating