Create a gist now

Instantly share code, notes, and snippets.

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 commented May 2, 2017

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

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

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

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

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