Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.fueled:snippety:1.1.0'
}
dependencies {
implementation("com.github.fueled:snippety:1.1.0")
}
<dependency>
<groupId>com.github.fueled</groupId>
<artifactId>snippety</artifactId>
<version>1.1.0</version>
</dependency>
libraryDependencies += "com.github.fueled" % "snippety" % "1.1.0"
:dependencies [[com.github.fueled/snippety "1.1.0"]]
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.
<img src="./README_images/ic_demo_screen.png" width="274" height="507"/><img src="./README_images/ic_page_screen.png" width="274" height="507"/><img src="./README_images/ic_html_screen.png" width="274" height="507"/>
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}'
}
Truss returns a CharSequence. There are 2 ways of using it:
pushSpan(Span), append(String) and popSpan()CharSequence text = new Truss()
.pushSpan(new BackgroundColorSpan(Color.RED))
.append("Hello Snippety")
.popSpan()
.build();
append(String, Span)CharSequence text = new Truss()
.append("Hello Snippety", new BackgroundColorSpan(Color.RED))
.build();
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();
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());
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();
Here are some code snippets for Snippety spans (tongue twister :D)
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), getString(R.string.font_sunshiney));
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().typeface(typeface))
.build());
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().fontStyle(Snippety.FontStyle.BOLD))
.build());
Drawable drawable = ContextCompat.getDrawable(getContext(), R.mipmap.ic_launcher);
textView.setText(new Truss()
.append(new Snippety().image(drawable))
.build());
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().align(Snippety.Indent.RIGHT))
.build());
int textSize = getResources().getDimensionPixelOffset(R.dimen.text_large);
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().textSizeAbsolute(textSize))
.build());
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().textSizeRelative(1.2f))
.build());
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().backgroundColor(Color.RED))
.build());
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().roundedBackgroundColor(Color.RED, Color.WHITE))
.build());
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().textColor(Color.BLUE))
.build());
int[] colorsRainbow = getResources().getIntArray(R.array.rainbow);
textView.setText(new Truss()
.append("Hello Snippety", new Snippety().textMultiColor(colorsRainbow))
.build());
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());
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());
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());
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());
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());
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.