Skip to content

Repository files navigation

Android TextViewOutline Demo

This is a demo project on how to create a TextView with an outer stroke.

textview outline preview

  • After creating the TextViewOutline class as shown in the project, simply call the <TextViewOutline /> as a normal <TextView /> inside your layout:
<games.moisoni.textviewoutline.TextViewOutline
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView Outline"android:textColor="@color/white"app:outlineColor="@color/black"app:outlineSize="3dp" />
  • Optionally, you can cast shadow:
<games.moisoni.textviewoutline.TextViewOutline
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView Outline Shadow"android:textColor="@color/white"android:shadowRadius="1"android:shadowColor="#00BCD4"android:shadowDx="0"android:shadowDy="8"app:outlineColor="@color/black"app:outlineSize="3dp" />

Steps:

  • First create the attrs.xml inside values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleablename="TextViewOutline">
<attrname="outlineSize"format="dimension" />
<attrname="outlineColor"format="color|reference" />
<attrname="android:shadowRadius" />
<attrname="android:shadowDx" />
<attrname="android:shadowDy" />
<attrname="android:shadowColor" />
</declare-styleable>
</resources>
  • Then, create the TextViewOutline class:
publicclassTextViewOutlineextendsAppCompatTextView {
privatestaticfinalintDEFAULT_OUTLINE_SIZE = 0;
privatestaticfinalintDEFAULT_OUTLINE_COLOR = Color.TRANSPARENT;
privateintmOutlineSize;
privateintmOutlineColor;
privateintmTextColor;
privatefloatmShadowRadius;
privatefloatmShadowDx;
privatefloatmShadowDy;
privateintmShadowColor;
publicTextViewOutline(@NonNullContextcontext) {
super(context);
}
publicTextViewOutline(@NonNullContextcontext, @NullableAttributeSetattrs) {
super(context, attrs);
setAttributes(attrs);
}
privatevoidsetAttributes(AttributeSetattr) {
mOutlineSize = DEFAULT_OUTLINE_SIZE;
mOutlineColor = DEFAULT_OUTLINE_COLOR;
mTextColor = getCurrentTextColor();
if (attr != null) {
TypedArraya = getContext().obtainStyledAttributes(attr, R.styleable.TextViewOutline);
if (a.hasValue(R.styleable.TextViewOutline_outlineSize)) {
mOutlineSize = (int) a.getDimension(R.styleable.TextViewOutline_outlineSize, DEFAULT_OUTLINE_SIZE);
}
if (a.hasValue(R.styleable.TextViewOutline_outlineColor)) {
mOutlineColor = a.getColor(R.styleable.TextViewOutline_outlineColor, DEFAULT_OUTLINE_COLOR);
}
if (a.hasValue(R.styleable.TextViewOutline_android_shadowRadius)
|| a.hasValue(R.styleable.TextViewOutline_android_shadowDx)
|| a.hasValue(R.styleable.TextViewOutline_android_shadowDy)
|| a.hasValue(R.styleable.TextViewOutline_android_shadowColor)) {
mShadowRadius = a.getFloat(R.styleable.TextViewOutline_android_shadowRadius, 0);
mShadowDx = a.getFloat(R.styleable.TextViewOutline_android_shadowDx, 0);
mShadowDy = a.getFloat(R.styleable.TextViewOutline_android_shadowDy, 0);
mShadowColor = a.getColor(R.styleable.TextViewOutline_android_shadowColor, Color.TRANSPARENT);
}
a.recycle();
}
}
@OverrideprotectedvoidonMeasure(intwidthMeasureSpec, intheightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setPaintToOutline();
}
privatevoidsetPaintToOutline() {
Paintpaint = getPaint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(mOutlineSize);
super.setTextColor(mOutlineColor);
super.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
}
privatevoidsetPainToRegular() {
Paintpaint = getPaint();
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(0);
super.setTextColor(mTextColor);
super.setShadowLayer(mShadowRadius, mShadowDx, mShadowDy, mShadowColor);
}
@OverridepublicvoidsetTextColor(intcolor) {
super.setTextColor(color);
mTextColor = color;
}
@OverrideprotectedvoidonDraw(Canvascanvas) {
setPaintToOutline();
super.onDraw(canvas);
setPainToRegular();
super.onDraw(canvas);
}
}

About

This is a demo project on how to create a TextView with an outer stroke.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages