Last active
November 29, 2017 13:52
-
-
Save amannepid/aff44cc202b321646de6d52f60fc6170 to your computer and use it in GitHub Desktop.
Dashed Underlined TextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- TODO: Update this! --> | |
<color name="dashColor">#3F51B5</color> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @author Dipendra Sunar | |
*/ | |
public class DashedUnderlinedTextView extends AppCompatTextView { | |
private Rect mRect; | |
private Paint mPaint; | |
private float mStrokeWidth; | |
private int mUnderlinePadding; | |
public DashedUnderlinedTextView(Context context) { | |
this(context, null, 0); | |
} | |
public DashedUnderlinedTextView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public DashedUnderlinedTextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs, defStyleAttr); | |
/// turning off hardware acceleration | |
// can be removed/commented out if no dashed underline needed. | |
setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
} | |
private void init(Context context, AttributeSet attributeSet, int defStyle) { | |
float density = context.getResources().getDisplayMetrics().density; | |
TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.DashedUnderlinedTextView, defStyle, 0); | |
int underlineColor = typedArray.getColor(R.styleable.DashedUnderlinedTextView_underlineColor, ContextCompat.getColor(context, R.color.dashColor)); | |
mStrokeWidth = typedArray.getDimension(R.styleable.DashedUnderlinedTextView_underlineWidth, density * 2); | |
mUnderlinePadding = typedArray.getInteger(R.styleable.DashedUnderlinedTextView_underlinePadding, 2); | |
typedArray.recycle(); | |
mRect = new Rect(); | |
mPaint = new Paint(); | |
mPaint.setStyle(Paint.Style.STROKE); | |
mPaint.setColor(underlineColor); | |
mPaint.setStrokeWidth(mStrokeWidth); | |
// Comment below line if no dashed underline needed. | |
mPaint.setPathEffect(new DashPathEffect(new float[]{5,5},0)); | |
} | |
@SuppressWarnings("unused") | |
public int getUnderLineColor() { | |
return mPaint.getColor(); | |
} | |
@SuppressWarnings("unused") | |
public void setUnderLineColor(int mColor) { | |
mPaint.setColor(mColor); | |
invalidate(); | |
} | |
@SuppressWarnings("unused") | |
public float getUnderlineWidth() { | |
return mStrokeWidth; | |
} | |
@SuppressWarnings("unused") | |
public void setUnderlineWidth(float mStrokeWidth) { | |
this.mStrokeWidth = mStrokeWidth; | |
invalidate(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
int count = getLineCount(); | |
final Layout layout = getLayout(); | |
float x_start, x_stop, x_diff; | |
int firstCharInLine, lastCharInLine; | |
for (int i = 0; i < count; i++) { | |
int baseline = getLineBounds(i, mRect) + mUnderlinePadding; | |
firstCharInLine = layout.getLineStart(i); | |
lastCharInLine = layout.getLineEnd(i); | |
x_start = layout.getPrimaryHorizontal(firstCharInLine); | |
x_diff = layout.getPrimaryHorizontal(firstCharInLine + 1) - x_start; | |
x_stop = layout.getPrimaryHorizontal(lastCharInLine - 1) + x_diff; | |
canvas.drawLine(x_start, baseline + mStrokeWidth, x_stop, baseline + mStrokeWidth, mPaint); | |
} | |
super.onDraw(canvas); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<DashedUnderlinedTextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:text="Date of Service" | |
app:underlineColor="@color/dashColor" | |
app:underlineWidth="2dp" | |
app:underlinePadding="4"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<declare-styleable name="DashedUnderlinedTextView"> | |
<attr name="underlineWidth" format="dimension" /> | |
<attr name="underlineColor" format="color" /> | |
<attr name="underlinePadding" format="integer"/> | |
</declare-styleable> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment