java


Android: Adding lines in edittext [duplicate]


I was taking a look at the notepad sample in the android SDK see here: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html
Thing is it only draws the current line the cursor is on e.g http://cdn2.staztic.com/screenshots/simple-notepad-app-al-1.jpg
But I'd like to display lines that fill up the screen e.g. http://www.itismyworld.info/wp-content/uploads/2010/03/AK-notebook.png
Any suggestions would be great. The relevent 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);
}

This is the code, based on jkhouws1's suggestion and google's 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 Eclipse IDE press Ctrl+Shift+O to add all needed imports

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

maybe after that for loop, you draw estimated* additional lines.
getHeight() will return EditText's height in pixels
getLineHeight() will height of one standard line
so getHeight/getlineHeight-getCount will be number of lines left to draw.
you can't use getLineBounds, using the above functions you could calculate the position of the remaining lines to draw.
*Estimated since formatting of text could change the line height, but since there is no text in these lines yet that shouldnt be an issue. But for that same reason you should only draw the remaining lines, and not use this to draw all the lines.

<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 code from Max4ever's answer:
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);
}
}


Related Links

Make Zip task run after dependencies are resolved
Angularjs to spring controller
Creating a static method that meshes two strings together
LongSerializer vs ByteArraySerializer in kafka
How can I have a group of JSpinner objects with a maximum total value?
Optimizing memory leakage in JavaFX
runtime.getRuntime.exec Freezes executing
read formatted BLE adverting data in android logcat but cant invoke it
How to assign database row id to a button
Runtime exceptions: what role do they have in defining an ADT, a methods “contract” and expressing the intentions with respect to a method
5th element from end of singly linked list without traversing the list [closed]
Log4j - How to log the application name?
Service Pattern in Spring may be more efficient with state
Android: Highlight <em> tags in SpannableString
Jsoup: Linebreaks in URL cannot be removed
Get string method returning null when word is definately not null

Categories

HOME
ms-access
compiler-construction
pdf
wso2-am
image
openmp
zeromq
openstack
google-api-php-client
plot
tesseract
yahoo-oauth
fsm
baqend
cross-browser
fingerprint
facebook-messenger-bot
metatrader4
row
google-cloud-ml
phaser
caml
serverless-framework
ef-migrations
facebook-page
angular-ui
lucene.net
realex-payments-api
captiveportal
icloud-api
spark-jobserver
django-storage
user-interaction
internet-explorer-9
wtx
libssl
primitive
instant-messaging
r-raster
bcd
bosh
oracle-fusion-middleware
http-get
caret
hybridauth
android-kernel
http-referer
wpf-controls
main
preconditions
fedex
checkboxlist
errordocument
winscp-net
statsd
boost-preprocessor
windows-mobile-6.5
qtwebview
pearson
sqldf
jspdf-autotable
dynamics-sl
gnome-shell-extensions
watchconnectivity
color-picker
libpng
bonobo
elements
gstreamer-0.10
ptrace
lemon
google-places
computer-algebra-systems
emailrelay
ios9.1
proj4js
dereference
streambase
html-helper
app42
runtime.exec
apc
blending
type-equivalence
wsdl-2.0
factory-method
cisco-jtapi
first-responder
frameset
dotnethighcharts
jquery-ui-layout
external-accessory
blackberry-playbook
jmock
telerik-scheduler
remember-me
radcombobox
visitor-statistic
fluent-interface
sortable-tables
.nettiers
dbal
memory-size
eqatec
privilege
defensive-programming
zune
uiq3





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization