Skip to content

Instantly share code, notes, and snippets.

@tpb1908
Created March 2, 2017 20:28
Show Gist options
  • Save tpb1908/570e807573c34a772b23464982630654 to your computer and use it in GitHub Desktop.
Save tpb1908/570e807573c34a772b23464982630654 to your computer and use it in GitHub Desktop.
Android bar span to draw a line through the full width of a TextView
package com.yourpackage.package
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.text.style.ReplacementSpan;
/**
* Created by theo on 02/03/17.
*/
public class BarSpan extends ReplacementSpan {
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
return 0;
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
final int mid = (top + bottom) / 2; // Draw at the midpoint of the line
// The width could be edited to draw across x% of the TextView with x + canvas.getWidth()/k
final RectF r = new RectF(x, mid, x + canvas.getWidth(), mid + 3);
canvas.drawRect(r, paint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment