Click here to Skip to main content
12,946,768 members (47,337 online)
Click here to Skip to main content

Stats

22.8K views
73 bookmarked
Posted 9 Jan 2013

Android - customize EditText like a page in textbook.

, 9 Jan 2013 CPOL
Customize EditText in Android with has lines inside.
.classpath
.project
.settings
org.eclipse.jdt.core.prefs
assets
bin
gen
vn
com
enclave
android
demo
ic_launcher-web.png
libs
android-support-v4.jar
project.properties
res
drawable-hdpi
ic_launcher.png
drawable-ldpi
ic_launcher.png
drawable-mdpi
ic_launcher.png
drawable-xhdpi
ic_launcher.png
layout
menu
values
src
vn
com
enclave
android
demo
package vn.com.enclave.android.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * @author Phat ( Phillip ) H . VU <vuhongphat@hotmail.com>
 * 
 */
public class LineEditText extends EditText {
	private Rect mRect;
	private Paint mPaint;

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

		mRect = new Rect();
		mPaint = new Paint();
		// define the style of line
		mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		// define the color of line
		mPaint.setColor(Color.BLACK);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int height = getHeight();
		int lHeight = getLineHeight();
		// the number of line
		int count = height / lHeight;
		if (getLineCount() > count) {
			// for long text with scrolling
			count = getLineCount();
		}
		Rect r = mRect;
		Paint paint = mPaint;

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

		// draw the remaining lines.
		for (int i = 0; i < count; i++) {
			canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
			 // next line
			baseline += getLineHeight();
		}
		super.onDraw(canvas);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author


You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 9 Jan 2013
Article Copyright 2013 by Phat (Phillip) H. VU
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid