Android: draw a line in text mode

I asked a question previously called "Android: Regular / Horizontal Lines in a TextView" on Android: Regular / Horizontal Lines in a Textview . But I do not have the necessary answer. So, I plan to do this by drawing a TextView string in java.Is there a way to draw a line inside a TextView? or if I get the end of the line from java code, then I can add a textview for each line. Any help would be greatly appreciated.

+5
source share
2 answers

I use the line-drawing technique between each line of text in an EditText , and then create an EditText that is not editable by setting setKeyListener (null) to a custom EditText object, so that the EditText acts like a TextView :)


Custom EditText that draws lines between each displayed line of text:

public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x800000FF);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();
        Rect r = mRect;
        Paint paint = mPaint;

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, r);

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        }

        super.onDraw(canvas);
    }
} 

Now use the object of the LinedEditText class , where you need a TextView and make it inaccessible for editing.

Example:

public class HorizontalLine extends Activity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);
        setTitle("Android: Ruled/horizonal lines in Textview");

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        LinedEditText et = new LinedEditText(this, null);
        et.setText("The name of our country is Bangladesh. I am proud of my country :)");
        et.setLayoutParams(textViewLayoutParams);
        et.setKeyListener(null);

        ll.addView(et);
        this.setContentView(ll);

    }

}

et.setKeyListener (null) makes EditText not editable, so it acts like a TextView.


Output:

enter image description here

Problem with cursor:

et.setKeyListener(null), , EditText. , EditText, :

 et.setEnabled(false);
+15

, . , , db, , XML :

<View android:background="#colorYouWant" android:layout_width = "fill_parent" android:layout_height="1dip" android:id="+@id/horizontalLine"/>

java-, Java-:

View horizontalLine = (View)findViewById(R.id.horizontalLine);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
horizontalLine.setLayoutParams(lp);

, , , - , .

+1

All Articles