Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm using the technique of drawing lines between each line of text in <strong>EditText</strong> and then I will make the <strong>EditText</strong> non-editable by setting <strong>setKeyListener(null)</strong> to the custom EditText object so that, the EditText acts like a <strong>TextView</strong> :)</p> <hr> <h2>A custom EditText that draws lines between each line of text that is displayed:</h2> <pre><code>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 &lt; count; i++) { int baseline = getLineBounds(i, r); canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); } super.onDraw(canvas); } } </code></pre> <hr> <p>Now use object of <strong>LinedEditText</strong> class where you need your <strong>TextView</strong> and make it non-editable.</p> <h2>An Example:</h2> <pre><code>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); } } </code></pre> <hr> <p><strong>et.setKeyListener(null)</strong> makes the EditText non-editable so, it acts like a TextView.</p> <hr> <h2>The Output:</h2> <p><img src="https://i.stack.imgur.com/5G300.png" alt="enter image description here"></p> <h2>Issue with cursor:</h2> <p>If you use <strong>et.setKeyListener(null)</strong> only then it is just not listening to keys but user can see a cursor on the EditText. If you don't want this cursor just disable the EditText by adding this line:</p> <pre><code> et.setEnabled(false); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload