1

Can anyone tell me how to create edittext so that it would look something like this?

enter image description here

Basically what I want is to replace the text box for edittext with just one single line! Is that possible?

2 Answers2

2

You can have a drawable with an underline, then you can put that in your xml:

android:background="@drawable/edit_text_underline"

It's not an elegant solution, but it is a solution.

This is the image:

enter image description here

Also, you can use a .9 image. In this case, do not forget set padding = 0. Sorry for my horrible English.

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
SolArabehety
  • 8,467
  • 5
  • 38
  • 42
1

Try this code :

public class LinedEditText extends EditText 
{
    private Rect mRect;
    private Paint mPaint;
    private Bitmap bitmap;

    public LinedEditText(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        mRect = new Rect();
        mPaint = new Paint();
        // define the style of line
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        // define the color of line
        mPaint.setColor(Color.BLACK);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.what_wirite_bg);

    }

    @Override
    protected void onDraw(Canvas canvas) 
    {
        int height = getHeight();
        int lHeight = getLineHeight();
        // the number of line
        int count = height / lHeight;
        if (getLineCount() > count) 
        {
            // for long text with scrolling
            count = getLineCount();
        }
        Rect r = mRect;
        Paint paint = mPaint;
        setPadding(10, 0, 10, 15);

        // first line
        int baseline = getLineBounds(0, r);
        //setCompoundDrawablePadding(5);
        if(HomeActivity.height>640)
        {
            //setPadding(0, 15, 0, 0);
            setLineSpacing(25.0f, 1.0f);
        }
        else
        {
            setLineSpacing(12.0f, 1.0f);
        }
        //setLineSpacing(20.0f, 0.5f);
        //setPadding(0, 15, 0, 0);
        /*setCompoundDrawablePadding(5);
        setLineSpacing(15.0f, 0.8f); ///////////////////Spacing change
        setPadding(0, 15, 0, 0); */ ///////////////////padding change
        // draw the remaining lines.
        for (int i = 0; i < count; i++) 
        {
            //canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            if(HomeActivity.height>640)
            {
                canvas.drawBitmap(bitmap, r.left, baseline + 16.0f, paint); //////////////To be Change
            }
            else
            {
                canvas.drawBitmap(bitmap, r.left, baseline + 5.5f, paint); //////////////To be Change
            }

            //canvas.drawBitmap(bitmap, r.left, baseline + 5.5f, paint); //////////////To be Change
            // next line
            baseline += (getLineHeight());
        }
        super.onDraw(canvas);
    }

     @Override
        public InputConnection onCreateInputConnection(EditorInfo outAttrs)
        {
            InputConnection conn = super.onCreateInputConnection(outAttrs);
            outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
            return conn;
        }

    @Override
    public Editable getText() {
        // TODO Auto-generated method stub
        return super.getText();
    }

And use this EditText in xml layout. Using this your ediText have background which you want..

Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39