Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Edittext with Line Number

Custom Edittext with Line Number

Mou icon

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

This comment has been minimized.

Show comment
Hide comment
@Ihor01

Ihor01 May 2, 2017

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

Ihor01 commented May 2, 2017

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

@francisnnumbi

This comment has been minimized.

Show comment
Hide comment
@francisnnumbi

francisnnumbi May 16, 2017

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

francisnnumbi commented May 16, 2017

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

@MahdiPishguy

This comment has been minimized.

Show comment
Hide comment
@MahdiPishguy

MahdiPishguy May 26, 2017

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

MahdiPishguy commented May 26, 2017

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

@MahdiPishguy

This comment has been minimized.

Show comment
Hide comment
@MahdiPishguy

MahdiPishguy May 26, 2017

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

MahdiPishguy commented May 26, 2017

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

@WHK102

This comment has been minimized.

Show comment
Hide comment
@WHK102

WHK102 Jan 7, 2018

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

WHK102 commented Jan 7, 2018

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

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