0
public 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.FILL_AND_STROKE);
    mPaint.setColor(Color.BLACK); //SET YOUR OWN COLOR HERE
}

@Override
protected void onDraw(Canvas canvas) {
    //int count = getLineCount();

    int height = getHeight();
    int line_height = getLineHeight();

    int count = height / line_height;

    if (getLineCount() > count)
        count = getLineCount();//for long text with scrolling

    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);//first line

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }

    super.onDraw(canvas);
}

**Im using this class for custom edit text and i am able to change properties in xml but im not getiing a reference to this custom edittext. How can I get reference to this edit text properly in java? my xml look like this **

    <com.example.goh2.pronoornotepad.LinedEditText
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="#ffff1904"
        android:background="#ffffcc4b"
        android:gravity="top|left"
        android:singleLine="false"
        android:id="@+id/et_textEditor"
        android:text=""
     />
Awais Ahmad
  • 427
  • 3
  • 17

1 Answers1

0

Did you try this?

LinedEditText myview = (LinedEditText) findViewById(R.id.et_textEditor);

If that doesn't work then it's usually an issue of xml hierarchy. Check this post: How to pass view reference to android custom view?

Community
  • 1
  • 1
ryye
  • 315
  • 4
  • 13
  • yes I did it... but im getting null pointer exception when i set background color lik myview.setbackgroundcolor(Color.Black) – Awais Ahmad Aug 29 '15 at 22:21