Skip to content

Instantly share code, notes, and snippets.

@lifeparticle
Last active February 9, 2023 11:22
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lifeparticle/de9224424d14a9d6e185 to your computer and use it in GitHub Desktop.
Custom Edittext with Line Number

Custom Edittext with Line Number

Mou icon

MIT License
Copyright (c) 2023 Mahbub Zaman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<rupantor.cusedittext.MyEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</ScrollView>
package rupantor.cusedittext;
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 S Mahbub Uz Zaman on 5/9/15.
* Lisence Under MIT
*/
public class MyEditText extends EditText {
private Rect rect;
private Paint paint;
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
rect = new Rect();
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
}
@Override
protected void onDraw(Canvas canvas) {
int baseline = getBaseline();
for (int i = 0; i < getLineCount(); i++) {
canvas.drawText("" + (i+1), rect.left, baseline, paint);
baseline += getLineHeight();
}
super.onDraw(canvas);
}
}
@IchZerowan
Copy link

If the code is longer then 100 lines, the text becomes to appear over the line numbers...

@francisnnumbi
Copy link

I think, we should set left padding to adjust automatically!

@pishguy
Copy link

pishguy commented May 26, 2017

how can i change line numbers direction from left to right?

@pishguy
Copy link

pishguy commented May 26, 2017

@francisnnumbi i dont have any problem? on which api version you have problem?

@yhojann-cl
Copy link

Change extends EditText to extends android.support.v7.widget.AppCompatEditText, change getBaseline() to getBaseline();. You need change the color for custom style color text.

@hichemkanon
Copy link

need to set the Padding i use this is perfect

public class LineNumberedEditText extends androidx.appcompat.widget.AppCompatEditText
{
private final Context context;
private Rect rect;
private Paint paint;

public LineNumberedEditText(Context context)
{
    super(context);
    this.context = context;
    init();
}

public LineNumberedEditText(Context context, AttributeSet attrs)
{
    super(context, attrs);
    this.context = context;
    init();
}

public LineNumberedEditText(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    this.context=context;
    init();
}

private void init()
{
    rect = new Rect();
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.GRAY);
    paint.setTextSize(30);
    paint.setTypeface(Typeface.MONOSPACE);
}


@Override
protected void onDraw(Canvas canvas) {

    int baseline;
    int lineCount = getLineCount();
    int lineNumber = 1;

    for (int i = 0; i < lineCount; ++i)
    {
        baseline=getLineBounds(i, null);
        if (i == 0)
        {
            canvas.drawText(""+lineNumber, rect.left, baseline, paint);
            ++lineNumber;
        }
        else if (getText().charAt(getLayout().getLineStart(i) - 1) == '\n')
        {
            canvas.drawText(""+lineNumber, rect.left, baseline, paint);
            ++lineNumber;
        }
    }

// for setting edittext start padding
if(lineCount<100)
{
setPadding(45,getPaddingTop(),getPaddingRight(),getPaddingBottom());
}
else if(lineCount>99 && lineCount<1000)
{
setPadding(63,getPaddingTop(),getPaddingRight(),getPaddingBottom());
}
else if(lineCount>999 && lineCount<10000)
{
setPadding(73,getPaddingTop(),getPaddingRight(),getPaddingBottom());
}
else if(lineCount>9999 && lineCount<100000)
{
setPadding(83,getPaddingTop(),getPaddingRight(),getPaddingBottom());
}

    super.onDraw(canvas);
}

}

@t-arn
Copy link

t-arn commented Jan 17, 2023

@lifeparticle
I'm very excited about this class! I'm trying to create a code editor widget for Toga (www.beeware.org) and this class might just be what I need for the Android implementation layer!

I have 3 questions:

Would you be willing to change the license of this code to BSD-3-clause, MIT or Apache? The Toga maintainers do not want to put Toga under GPL.

From what I see, this class generates line numbers for all lines. Won't that be slow when there are many lines? How could the code be adapted to just generate line numbers for the visible lines?

When there is no line wrapping, the numbering is just fine as it is. But when line wrapping is active, I don't want a new line number for the wrapped parts. I only want a new line number when the previous display line ended with a newline character "\n". How can this be achieved?

@lifeparticle
Copy link
Author

@t-arn

I've added an MIT license.

I'm not sure about 2 and 3 since I don't actively maintain this project. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment