0

I just working on demo app where I require to set the the border width and the border color programmatically.

I know that can be done using the xml drawable but I want to do all these at run time. I know it is good to have these by creating the xml and use it but my requirement is such dynamic so I have to do all these stuff at runtime instead of below approach.

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

Setting the drawable.

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

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • You would likely need to either create a custom view and use the onDraw method to do this, or dynamically create a relative layout that is 2dp wider/higher than the EditText, set the margin of the editText to one and put the edit text in the container, adding the rule to center in parent. – zgc7009 Nov 11 '14 at 17:33
  • http://stackoverflow.com/questions/17950258/how-to-programmatically-create-or-alter-a-drawable-made-of-lines-of-different-co use this link – Piyush Nov 11 '14 at 17:34
  • @zgc7009 I don't have much knowledge of writing custom view. Can you hint What I have to override in `onDraw`. – N Sharma Nov 11 '14 at 17:46
  • onDraw literally draws your view. It basically gives you blank canvas and you have to determine what is drawn there. It seems like overkill here though. It is probably even slightly more in depth than @piyush link, though I didn't really read through the link completely. Is there a reason you want it done programmatically? – zgc7009 Nov 11 '14 at 17:48
  • @zgc7009 yes it because i will get these data from web service then inflate layout accordingly. i would appreciate if you help – N Sharma Nov 11 '14 at 17:52

1 Answers1

1

Shape Drawables is what came to mind first. The Android Guide has some good details on this.

    int x = 10;
    int y = 10;
    int width = 300;
    int height = 50;

    mDrawable = new ShapeDrawable(new RectShape()); // rectangle for border
    mDrawable.getPaint().setColor(0xff74AC23); // set color
    mDrawable.setBounds(x, y, x + width, y + height);   

    setContentView(mDrawable);

But that wouldn't be a hollow rectangle, as you'd probably want for the border. See this answer for more discussion about using a border. The tricky part is getting transparency. It looks like some have settled for drawing four lines around the border of their canvas.

A canvas can be created like this.

Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Community
  • 1
  • 1
NSouth
  • 5,067
  • 7
  • 48
  • 83
  • I tried your above code `mDrawable = new ShapeDrawable(new RectShape()); // rectangle for border mDrawable.getPaint().setColor(0xff74AC23); // set color mDrawable.setBounds(x, y, x + width, y + height); ` but this made my edittext color to complete black. I want only border. can you help how canvas i have to set and help to achieve this – N Sharma Nov 13 '14 at 16:28
  • I was afraid of that. Instead of a RectShape, you could make four line shapes around the border. Or you could try making an image of a border and have it stretch as needed. Sorry, I haven't had to do this kind of thing before. – NSouth Nov 13 '14 at 18:39
  • +1 hmm looks like your answer is near around my requirement. thanks for your help. – N Sharma Nov 13 '14 at 18:56