2

I am using a custom edittext but the problem is that I am unable to set a text for my custom edittext. Here is what all I tried from the available answers on SO,

setText not working for a Custom Edittext

This did not work still. I did not get any error,so no clued why it did not work.

Custom TextView - setText() called before constructor

This also did not work.

XML FILE

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <com.random.practiceproject.LinedEditText
        android:layout_width="301dp"
        android:inputType="text"
        android:layout_height="match_parent" />  


</LinearLayout>

MainActivity

LinedEditText lt;
    EditText et; //Normal edittext works

    String s = "This is a sample string";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lt = new LinedEditText(this);
        et = (EditText) findViewById(R.id.newt);
        lt.setCursorVisible(true);
        lt.setTextColor(Color.BLACK);
        lt.setText(s,TextView.BufferType.EDITABLE);
        et.setText(s, TextView.BufferType.EDITABLE);
 }

Here is the code,

public class LinedEditText extends EditText {

    private static Paint linePaint;

    static {
        linePaint = new Paint();
        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Paint.Style.STROKE);
    }

    public LinedEditText(Context context)
    {

        super(context);

    }

    public LinedEditText(Context context, AttributeSet attributes) {
        super(context, attributes);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        Rect bounds = new Rect();
        int firstLineY = getLineBounds(0, bounds);

        /*int firstLineY=0;

        for(int i =0;i<getLineCount();i++)
        {
            firstLineY = (i + 1) * getLineHeight();
        }*/

        int lineHeight = getLineHeight();
        int totalLines = Math.max(getLineCount(), getHeight() / lineHeight);


        for (int i = 0; i < totalLines; i++) {
            int lineY = firstLineY + i * lineHeight;
            canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint);
        }

        super.onDraw(canvas);
    }


}
Community
  • 1
  • 1
oldcode
  • 1,669
  • 3
  • 22
  • 41

1 Answers1

2

The problem is that you create new instance of LineEditText in onCreate and working with it, but not adding to layout. In the layout there is another instance of LineEditText, that you don't use.

So you must either replace:

lt = new LinedEditText(this);

with:

lt = (LinedEditText) findViewById(/*provide your id*/)

or you need to add lt to layout through ViewGroup.addView(), but I think you need to use first variant.

Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
  • Just started to type the same :) – abbath Sep 28 '16 at 07:03
  • I thought since I am instantiating that would be enough. – oldcode Sep 28 '16 at 07:05
  • @WeirdNerd no. The main point is that activity in the begining of its lifecycle does not know anything about layout. But you can help it with that by calling `setContentView`. You can pass a xml layout or your custom ViewGroup. Then activity work with it. In your case it is xml-file with LinearLayout and inherited LinedEditText. Then you need to take your views and work with them through `findViewById` method. But instead of that you create a separate instance of `LinedEditText` – Michael Spitsin Sep 28 '16 at 07:08
  • @WeirdNerd ...And after that you have 2 instances of LinedEditText. One is parsed from xml layout and second one is created by you (by explicitly calling one argument constructor). First instance you don't use, and second you don't atatch to anything (to display it). – Michael Spitsin Sep 28 '16 at 07:10
  • Instantiating only creates the new object for you. Either you have to add this newly created object back to the layout or you have to first define it in the XML and then instead of doing a new for that object you have to do a findviewbyid and then get a reference to it – Anuj Sep 28 '16 at 07:18
  • @WeirdNerd will appreciate, if mark this answer as correct ;) – Michael Spitsin Sep 28 '16 at 07:23