Custom EditText with Line Number

Custom Edittext with Line Number

Mou icon

view raw
readme.md
hosted with ❤ by GitHub

<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>

view raw
main.xml
hosted with ❤ by GitHub

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

view raw
MyEditText.java
hosted with ❤ by GitHub