Android add border to edit text programmatically

This Code is working for draw border to any view programmatically

package com.example.border;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;

public class ShapeDrawableWithoutBottom extends ShapeDrawable {
    private float mLineWidth = 1f;
    private final Paint mLinePaint;
    private int color;

    public ShapeDrawableWithoutBottom() {

        // No color specified, so call constructor with default color White
        this(Color.WHITE);
    }

    public ShapeDrawableWithoutBottom(int layoutColor) {

        // use the setter defined below, to set the main color for this drawable
        // setColor(color);
        setColor(layoutColor);
        // setup the Paint for drawing the lines
        mLinePaint = new Paint();
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(mLineWidth);
    }

    public void setColor(int color) {
        Paint paint = getPaint();
        paint.setColor(color);

    }

    public void setLineColor(int color) {
        this.color = color;
    }

    public void setLineWidth(float lineWidth) {
        mLineWidth = lineWidth;
        mLinePaint.setStrokeWidth(mLineWidth);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        // bottom black line
        // //////////////////

        mLinePaint.setColor(Color.parseColor("#00000000"));
        mLinePaint.setAlpha((int) (255 * 0.0)); // Opacity 90%
        canvas.drawLine(getBounds().left, getBounds().bottom - mLineWidth
                * 0.5f, getBounds().right, getBounds().bottom - mLineWidth
                * 0.5f, mLinePaint);

        // translucent grey rim
        // /////////////////////

        mLinePaint.setColor(color);
        mLinePaint.setAlpha((int) (255 * 0.7)); // Opacity 70%

        // top
        canvas.drawLine(getBounds().left, getBounds().top + mLineWidth * 0.5f,
                getBounds().right, getBounds().top + mLineWidth * 0.5f,
                mLinePaint);

        // left
        canvas.drawLine(getBounds().left + mLineWidth * 0.5f,
                getBounds().bottom , getBounds().left + mLineWidth
                        * 0.5f, getBounds().top + mLineWidth, mLinePaint);

        // right
        canvas.drawLine(getBounds().right - mLineWidth * 0.5f,
                getBounds().bottom , getBounds().right - mLineWidth
                        * 0.5f, getBounds().top + mLineWidth, mLinePaint);

        // top white line
        // ///////////////

        mLinePaint.setColor(Color.WHITE);
        mLinePaint.setAlpha((int) (255 * 0.5)); // Opacity 50%
        canvas.drawLine(getBounds().left + mLineWidth, getBounds().top
                + mLineWidth * 1.5f, getBounds().right - mLineWidth,
                getBounds().top + mLineWidth * 1.5f, mLinePaint);
    }
}

Try this in this i have dynamically added edittext then set its background and it works.

 LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
                    EditText edit=new EditText(MainActivity.this);
                    edit.setBackgroundResource(R.drawable.abc);
                    edit.setMaxWidth(100);
                    edit.setMinHeight(100);
                    edit.setText("hello");
                    layout.addView(edit);

Well i also have the same issue which i solve by the following way. Its is an xml file put it on your drawable folder and set this xml into the background of that EditText

activity code:

EditText foo = (EditText)findViewById(R.id.editText);
foo.setBackgroundResource(R.drawable.backtext);

backtext.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
       <solid android:color="#ffffff" />
       <stroke android:width="1dip" android:color="#000000"/>
    </shape>