Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
any idea to create a lined edit text like note pad, in android.
Posted
Updated 1-Jun-18 0:02am

By the looks of it it's covered in this tutorial on the android developer site.

http://developer.android.com/resources/tutorials/notepad/index.html[^]
 
Share this answer
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;

/**
* Created by Valentine on 9/28/2015.
*/
public class LinedEditText extends android.support.v7.widget.AppCompatEditText {

private Rect rect;
private Paint paint;


public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);

rect = new Rect();
paint = new Paint();

paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.parseColor("#909090"));


setTypeface(context);
}

public void setLineColor(int color) {
paint.setColor(color);
}

@Override
protected void onDraw(Canvas canvas) {
int height = getHeight();
int lineHeight = getLineHeight();
int count = height / lineHeight;

// For long text with scrolling
if (getLineCount() > count) {
count = getLineCount();
}

// Draw first line
int baseline = getLineBounds(0, rect);

rect.left = getPaddingLeft() - 100;


rect.offset(5, 5);

for (int i = 0; i < count; i++) {

Log.e("Base line : ", "" + baseline);


canvas.drawLine(rect.left, baseline + 40, rect.right, baseline + 40, paint);

baseline += getLineHeight();
}
super.onDraw(canvas);
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
dispatchKeyEvent(event);
return false;
}
return super.onKeyPreIme(keyCode, event);
}

private void setTypeface(Context context) {
this.setTypeface(Typeface.createFromAsset(context.getAssets(), "avenirnextregular.ttf"));
}
}
 
Share this answer
 
Comments
CHill60 1-Jun-18 9:13am    
You have reopened a 6 year old, answered question to post someone else's code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900