Hot File

Android - customize EditText like a page in textbook

View: 2053    Dowload: 1   Comment: 0   Post by: naruto  
Author: none   Category: Java mobile   Fields: Computers - Technology

8 point/6 review File has been tested

Download   android-customize-edittext.zip (416.41 KB)

You need to Sign In to download the file android-customize-edittext.zip
If you do not have an account then Sign up for free here

Download error   Report copyright violation

This tip will show a useful tip in Android. That's customizing EditText, so you will have an EditText looks like a page in textbook. So, in some cases, it will make your application be more interesting.

Introduction

This tip will show a useful tip in Android. That's customizing EditText, so you will have an EditText looks like a page in textbook. So, in some cases, it will make your application be more interesting.

Background

The basic idea here is I will define a custome EditText field in Java. Then, in the main layout, instead of using to provide an EditText field, we will replace it with our Java class that be extended class EditText.

Using the code 

Firstly, we will define a Java class that extend class EditText, named LineEditText 

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 
 * 
 */
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);
 }
} 

 Then, in layout that you want to show EditText field, declare like this:


 

 That's all.
The result when application is running :

 

Android - customize EditText like a page in textbook

Android - customize EditText like a page in textbook Posted on 21-03-2014  This tip will show a useful tip in Android. That's customizing EditText, so you will have an EditText looks like a page in textbook. So, in some cases, it will make your application be more interesting. 1.3333333333333/10 2053

Comment:

To comment you must be logged in members.

Files with category

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd