Reputation: 1351
I am trying to find a simple solution to adding an outline to text, to make it stand out no matter what background sits behind it.
This question was previously marked as duplicate, but the answer linked does not work. Therefore, this is still an ongoing issue. The answer linked is the following:
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Paint strokePaint = new Paint();
strokePaint.setARGB(255, 0, 0, 0);
strokePaint.setTextAlign(Paint.Align.CENTER);
strokePaint.setTextSize(16);
strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(2);
Paint textPaint = new Paint();
textPaint.setARGB(255, 255, 255, 255);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(16);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
Path path = new Path();
String text = "Some Text";
textPaint.getTextPath(text, 0, text.length(), 0, 100, path);
canvas.drawPath(path, strokePaint);
canvas.drawPath(path, textPaint);
super.draw(canvas, mapView, shadow);
}
The line:
super.draw(canvas, mapView, shadow);
Is picked up by the compiler as an error. The .draw part is red, as it "cannot resolve method". Since this solution is not a working solution, this question is therefore not a duplicate.
I have also tried another example that creates a class as follows: (Edited below, the compiler forced me to add a constructor, as shown).
public class OutlineTextView extends TextView {
public OutlineTextView(Context context) { //added this section
super(context);
}
@Override
public void draw(Canvas canvas) {
for (int i = 0; i < 5; i++) {
super.draw(canvas);
}
}
}
And then the xml file attempts to use this class as follows:
<OutlineTextView
android:shadowColor="#000"
android:shadowRadius="3.0"
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textSize="160dp"
android:typeface="sans"
android:textColor="#ffffe0"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true" />
This throws an error when attempting to launch this activity:
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.OutlineTextView" on path: DexPathList[[zip file "/data/app/rule02.touchpool-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
Any further ideas on how to get this to work, anyone?
Upvotes: 2
Views: 1440
Reputation: 11873
You need to use your full package name for a custom view. So your current layout should become,
<com.example.OutlineTextView
android:shadowColor="#000"
android:shadowRadius="3.0"
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textSize="160dp"
android:typeface="sans"
android:textColor="#ffffe0"
android:layout_marginTop="60dp"
android:layout_centerHorizontal="true" />
Upvotes: 1