Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:32:58+00:00 2026-06-02T20:32:58+00:00

From the tutorial I have created the layout: public static class LinedEditText extends EditText

  • 0

From the tutorial I have created the layout:

  public static 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.STROKE);
            mPaint.setColor(0x80000000);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            int count = getLineCount();
            Rect r = mRect;
            Paint paint = mPaint;

            for (int i = 0; i < count; i++) {
                int baseline = getLineBounds(i, r);
                canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            }

            super.onDraw(canvas);
        }
    }

<view xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.bbbfr.mynotepad.NotepadText$LinedEditText"
    android:id="@+id/note"
    android:background="#ffd6e5"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp"
    android:scrollbars="vertical"
    android:fadingEdge="vertical"
    android:gravity="top"
    android:textSize="22sp"
    android:textColor="#000000"
    android:inputType="textMultiLine"
    android:capitalize="sentences"
/>

This makes only the first line underlined. Is it possible to make all the lines underlined, even if there is only one line in the edtittext?

I tried changing the loop e.g. for (int i = 0; i < 5; i++) but then I receive this error:

04-28 08:29:05.093: E/AndroidRuntime(14398):
java.lang.IndexOutOfBoundsException: 2, 1 04-28 08:29:05.093:
E/AndroidRuntime(14398): at
android.text.PackedIntVector.getValue(PackedIntVector.java:70) 04-28
08:29:05.093: E/AndroidRuntime(14398): at
android.text.DynamicLayout.getLineTop(DynamicLayout.java:367) 04-28
08:29:05.093: E/AndroidRuntime(14398): at
android.text.Layout.getLineBottom(Layout.java:831) 04-28 08:29:05.093:
E/AndroidRuntime(14398): at
android.text.Layout.getLineBounds(Layout.java:437) 04-28 08:29:05.093:
E/AndroidRuntime(14398): at
android.widget.TextView.getLineBounds(TextView.java:4122) 04-28
08:29:05.093: E/AndroidRuntime(14398): at
com.bbbfr.mynotepad.NotepadText$LinedEditText.onDraw(NotepadText.java:56)

to this line: int baseline = getLineBounds(i, r);

I have also set android:lines="5" in the view.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:33:01+00:00Added an answer on June 2, 2026 at 8:33 pm

    If you don’t mind the underline having the same colour as the text in the EditText, you should really just use the built-in UnderlineSpan, either by creating it yourself or indirectly through Html.fromHtml(...).

    private void createUnderlinedText() {
        String text = "I am underlined text\nLine #2\nLine #3\nLine #4\nLine #5";
    
        EditText underlineSpanEditText = (EditText) findViewById(R.id.underlinespan_edittext);
        SpannableStringBuilder sb = new SpannableStringBuilder(text);
        sb.setSpan(new UnderlineSpan(), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        underlineSpanEditText.setText(sb);
    
        EditText htmlUnderlineEditText = (EditText) findViewById(R.id.html_underline_edittext);
        String html = "<u>I am underlined text</br>Line #2</br>Line #3</br>Line #4</br>Line #5</u>";
        htmlUnderlineEditText.setText(Html.fromHtml(html));
    }
    

    The main difference with your current approach is that this will only underline the actual text, and not the whole text line. For example, if you run my code snippet, you will find that the underline does not extend to the end of the line when it’s broken off by the \n or <br/>. However, depending on the behaviour your after, this may not be what you’re looking for.


    Edit: So if I understand you correctly, you basically want to keep drawing horizontal lines in your EditText, no matter wether there is text or not? The ‘underline’ part in your question was kind of misleading, since, as it turns out, that has little to do with it (in the traditional meaning of the word :)).

    Anyways, you can’t use getLineCount() since that will always return the number of lines that contain actual text. That would mean you would have to ‘fill’ any remaing space with new line characters to get the desired effect, which sounds kind of yucky… A better alternative is probably to base the drawing of horizontal lines on the total height of the EditText. A quick example, which you can obviously tweak to your own liking:

    public class LinedEditText extends EditText {
        private Paint mPaint = new Paint();
    
        public LinedEditText(Context context) {
            super(context);
            initPaint();
        }
    
        public LinedEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            initPaint();
        }
    
        public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            initPaint();
        }
    
        private void initPaint() {
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(0x80000000);
        }
    
        @Override protected void onDraw(Canvas canvas) {
            int left = getLeft();
            int right = getRight();
            int paddingTop = getPaddingTop();
            int paddingBottom = getPaddingBottom();
            int paddingLeft = getPaddingLeft();
            int paddingRight = getPaddingRight();
            int height = getHeight();
            int lineHeight = getLineHeight();
            int count = (height-paddingTop-paddingBottom) / lineHeight;
    
            for (int i = 0; i < count; i++) {
                int baseline = lineHeight * (i+1) + paddingTop;
                canvas.drawLine(left+paddingLeft, baseline, right-paddingRight, baseline, mPaint);
            }
    
            super.onDraw(canvas);
        }
    }
    

    The result looks like this:

    LinedEditText

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class (from a simple Spring tutorial) public class CarValidator implements
I have created a custom class from a tutorial online. It works fine, but
I have learned from various tutorial that If a client can reasonably be expected
I have been following the affableBean tutorial from the NetBeans site located here .
I am learning JPA from this tutorial . I have some confusions in understanding
I'm playing with the distributed programming tutorial from the 5.4 documentation, and have run
I'm new to NHibernate... I have been following this NHibernate Tutorial from Gabriel Schenker
See the excerpt below taken from a PL/SQL tutorial. The part I have an
I have done a couple of simple swing based apps with static layout, but
I have created an application where i am fetching some data from a webservice,which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.