Drawing multiple lines in edittext, for example. notebook

I took a look at a sample notepad in the Android SDK. See here: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html

A thing is just drawing the current line on which the cursor is on, for example http://cdn2.staztic.com/screenshots/simple-notepad-app-al-1.jpg

But I would like to display lines filling the screen, for example. http://www.itismyworld.info/wp-content/uploads/2010/03/AK-notebook.png

Any suggestions would be great. The corresponding bit of code seems to be here:

protected void onDraw(Canvas canvas) { // Gets the number of lines of text in the View. int count = getLineCount(); // Gets the global Rect and Paint objects Rect r = mRect; Paint paint = mPaint; /* * Draws one line in the rectangle for every line of text in the EditText */ for (int i = 0; i < count; i++) { // Gets the baseline coordinates for the current line of text int baseline = getLineBounds(i, r); /* * Draws a line in the background from the left of the rectangle to the right, * at a vertical position one dip below the baseline, using the "paint" object * for details. */ canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); } // Finishes up by calling the parent method super.onDraw(canvas); } 
+6
source share
4 answers

This is code based on jkhouws1 and google note editor

 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.FILL_AND_STROKE); mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR HERE } @Override protected void onDraw(Canvas canvas) { //int count = getLineCount(); int height = getHeight(); int line_height = getLineHeight(); int count = height / line_height; if (getLineCount() > count) count = getLineCount();//for long text with scrolling Rect r = mRect; Paint paint = mPaint; int baseline = getLineBounds(0, r);//first line for (int i = 0; i < count; i++) { canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); baseline += getLineHeight();//next line } super.onDraw(canvas); } } 

In the Eclipse IDE, press Ctrl + Shift + O to add all the necessary imports.

+31
source

I think this is what you need:

 public class LinedEditText extends EditText { private static Paint linePaint; static { linePaint = new Paint(); linePaint.setColor(Color.BLACK); linePaint.setStyle(Style.STROKE); } public LinedEditText(Context context, AttributeSet attributes) { super(context, attributes); } @Override protected void onDraw(Canvas canvas) { Rect bounds = new Rect(); int firstLineY = getLineBounds(0, bounds); int lineHeight = getLineHeight(); int totalLines = Math.max(getLineCount(), getHeight() / lineHeight); for (int i = 0; i < totalLines; i++) { int lineY = firstLineY + i * lineHeight; canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint); } super.onDraw(canvas); } } 
+3
source

perhaps after that for the cycle you draw the estimated * additional lines.

getHeight () will return the EditText height in pixels getLineHeight () will have the height of one standard line

therefore getHeight / getlineHeight-getCount will contain the number of lines to draw.

you cannot use getLineBounds using the functions above, you could calculate the position of the remaining lines to draw.

* It is believed that formatting text can change the height of a line, but since there is no text in these lines, this should not be a problem. But for the same reason, you should draw the remaining lines, and not use this to draw all the lines.

+2
source
 <com.example.goh2.pronoornotepad.LinedEditText android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffcc4b" android:gravity="top|left" android:singleLine="false" android:text="" /> 

The above XML works with the Max4ever answer 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.FILL_AND_STROKE); mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR HERE } @Override protected void onDraw(Canvas canvas) { //int count = getLineCount(); int height = getHeight(); int line_height = getLineHeight(); int count = height / line_height; if (getLineCount() > count) count = getLineCount();//for long text with scrolling Rect r = mRect; Paint paint = mPaint; int baseline = getLineBounds(0, r);//first line for (int i = 0; i < count; i++) { canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); baseline += getLineHeight();//next line } super.onDraw(canvas); } } 
-1
source

Source: https://habr.com/ru/post/887885/


All Articles