I am trying from many day but did not get any solution . i have refer this Android:Draw line on a textview I have tried to create dotted line in text view , done with it , working fine , but i want to add dotted line till length of the text that text view contain , If Text View have one line text then it work fine But if text view have multi line text then for the line which have text half or less then screen width then also dotted line appear with i dont want.... I draw dotted line using Paint ...please help me .... Thanks in Advance
Asked
Active
Viewed 2,664 times
1
-
can you post your code please? – Lia Pronina Aug 30 '13 at 06:56
-
Can you provide us with some code what u've done so far . . . – Jilberta Aug 30 '13 at 06:57
-
mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(0xFFFF0000); mPaint.setARGB(255, 0, 0, 0); mPaint.setStyle(Paint.Style.STROKE); mPaint.setPathEffect(new DashPathEffect(new float[]{ 3, 3, }, 0)); mPaint.setStrokeWidth(1);.. this code to draw dotted line and in OnDraw to check number of line i have done this thing for (int i = 0; i < cnt; i++) { int baseLine = getLineBounds(i, r); canvas.drawLine(0, baseLine + 1, r.right, baseLine + 1, mPaint); } – ADT Aug 30 '13 at 07:01
-
Do you want each line highlighted in multiline `TextView`? – Lia Pronina Aug 30 '13 at 07:04
-
i want to underline (dotted line) below text , means below character of textview – ADT Aug 30 '13 at 07:07
-
Do you want the dotted line? Or a smooth line is also fine? – Anukool Aug 30 '13 at 07:12
1 Answers
3
Relying on the topic, in which you do, try to write in the constructor of the class LinedEditText
mPaint = new Paint();
mPaint.setARGB(255, 0, 0, 0);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float[] {10,10}, 0));
instead of
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x800000FF);
EDIT:
and tell me please again, do you want like this:
or this:
EDIT:
MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
String text = "Sample Sample Sample Sample Sample Sample Sample Sample Sample Sample";
LinedEditText et = new LinedEditText(this, null, text);
et.setText(text);
et.setLayoutParams(textViewLayoutParams);
et.setKeyListener(null);
ll.addView(et);
this.setContentView(ll);
}
LinedEditText:
public class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;
String text;
public LinedEditText(Context context, AttributeSet attrs, String text) {
super(context, attrs);
this.text = text;
mRect = new Rect();
mPaint = new Paint();
mPaint.setARGB(255, 0, 0, 0);
mPaint.setStyle(Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float[] { 10, 10 }, 0));
}
@Override
protected void onDraw(Canvas canvas) {
Rect r = mRect;
Paint paint = mPaint;
int lineCount = getLineCount();
int size = getLayout().getLineStart(lineCount-1);
String str = getText().toString().substring(size);
float densityMultiplier = getContext().getResources().getDisplayMetrics().density;
float scaledPx = 20 * densityMultiplier;
paint.setTextSize(scaledPx);
float i = paint.measureText(str);
for (int k = 0; k < lineCount-1; k++) {
int baseline = getLineBounds(k, r);
canvas.drawLine(r.left, baseline + 2, r.right, baseline + 2, paint);
}
int baseline = getLineBounds(lineCount-1, r);
canvas.drawLine(r.left, baseline + 2, i, baseline + 2, paint);
super.onDraw(canvas);
}
}

Lia Pronina
- 766
- 3
- 7
-
-
last picture contains multi line with dash line , but last line which contain 2 words Sample Sample i want dash only for that two words – ADT Aug 30 '13 at 07:56
-
I found some solution, in the near future I will make it to the response – Lia Pronina Aug 30 '13 at 09:58
-
-
-
-
-
-
Hi Lia Pronina , can u tell me that ...is it possible to set background color of text only and not Text view..? – ADT Sep 02 '13 at 12:42
-
Hi, what do you mean? Color of background or color of text? You can to write like this, if I understood you correct `
-
u have given code to set dotted for text only , like that i want to set background color for that text only – ADT Sep 02 '13 at 13:01
-
create a new question for this, need some time to check out what you're asking, I can't say immediately. You can use `setBackgroundColor(R.drawable.ic_launcher);` in `LinedEditTextfor` for example. – Lia Pronina Sep 02 '13 at 13:08
-
I have create new question , please make response if u know how to do that... http://stackoverflow.com/questions/18584267/android-setbackground-color-for-custom-text-view – ADT Sep 03 '13 at 11:13
-
Hi Lia Pronina .. can u tell me how to differentiate multiple line in TextView ,if i set background color of textview – ADT Sep 05 '13 at 06:04