15

I used this example and tried to add it to my edit text programmatically like editText.setBackgroundResource(R.drawable.edit_text_back);, but it does not work. How can I accomplish this? Any suggestions or ideas?

EDIT The editText is defined programmatically as well.

EditText editText = new EditText(this.getApplicationContext());

I added this to a table row

TRIED

editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));
editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_text_back));

EDIT TEXT CREATION

TableRow row = (TableRow) findViewById(R.id.table_row_kind);
TableRow.LayoutParams rowP = new TableRow.LayoutParams();
        rowP.setMargins(10, 0, 0, 0);
editText = new EditText(this.getApplicationContext());
editText .setGravity(Gravity.FILL_HORIZONTAL);
editText .setLayoutParams(rowP);
editText .setFilters(new InputFilter[]{txtFilter});
editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));

row.xml

<TableRow
    android:id="@+id/table_row_kind"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <TextView
       android:layout_width="250sp"
       android:text="Kind"
       android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
Community
  • 1
  • 1
Lunchbox
  • 1,518
  • 2
  • 16
  • 41
  • editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_text_back)); try this @Lunchbox – TheFlash Oct 31 '13 at 12:26
  • I think this thread might help you. http://stackoverflow.com/questions/3496269/how-to-put-a-border-around-an-android-textview/17709528#17709528 – Bojan Jovanovic Oct 31 '13 at 12:29
  • The example SO question you linked is quite rich to explain .. – dharam Oct 31 '13 at 12:45
  • @BojanJovanovic I tried your answer but still no result, instead of extending TextView, I just extended EditText. Thanks though – Lunchbox Oct 31 '13 at 12:46
  • @Indiandroid I tried it but it did not work, see my edit – Lunchbox Oct 31 '13 at 12:46
  • @dhams You are right, it does explain how to add a border to a view, but mine is added programmatically and does not want to work. – Lunchbox Oct 31 '13 at 12:56
  • @Lunchbox it does not matter weather you call it from XML or from your java file . – dharam Oct 31 '13 at 13:03
  • @dhams Mine does not work. I tested it with an edit text defined in xml and it worked, then I tested it with a edit text defined in java and it does not work. I don't understand why not, hence, the question. It is a really really weird problem – Lunchbox Oct 31 '13 at 13:06
  • @Lunchbox I have same issue. But there is problem only when you set background dynamically. If you set it in xml, it works fine. Did you got solution to that? – Shreyash Mahajan Oct 14 '15 at 12:38

6 Answers6

20

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>
mayuri
  • 366
  • 1
  • 4
  • 1
    Hi, it did not work, probably because I created my edit text programmatically. See my edit – Lunchbox Oct 31 '13 at 12:52
  • 1
    @simmant It is a good answer, but it only works for an edit text that was defined in xml. That is where my problem lies – Lunchbox Oct 31 '13 at 12:59
10

create a edittext.xml file into drawable folder

<?xml version="1.0" encoding="utf-8"?>
<!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<stroke
    android:width="1dp"
    android:color="@android:color/black" />
<corners
 android:bottomRightRadius="15dp"
 android:bottomLeftRadius="15dp"
 android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>


in your main.xml
<EditText
background="drawable/edittext.xml"
/>
Pankaj Arora
  • 11,041
  • 2
  • 38
  • 62
  • This would work for a edit text that was defined in xml, but mine is not – Lunchbox Oct 31 '13 at 12:54
  • u want to set bg border on edittext that you create dynamically,right? – Pankaj Arora Nov 01 '13 at 06:20
  • Yep, just black, I will change it to the correct color once it works – Lunchbox Nov 01 '13 at 06:23
  • This does not work:I derived many ui elements from standard ui elements. So I have no borders.My TextviewExt,which inherits from Textview does not have borders. I set backcolor of the textviewext programatically as well as text and textcolor. The shapes approach does some stuff,but it also seems not to work properly. I had to override onDraw of my TextViewExt and in this I could call setBackgroundRessource.This worked nowhere else in my derivate.Seems,as if some kind of caller must modify the child,but not the child itself.Also,it gives me two colors. And I need ONLY a black border.What now? – icbytes Jan 10 '14 at 09:49
  • @icbytes Did you fix your problem? I just ended up added my EditText to a LinearLayout instead. Than seemed to do the trick. TableRow was the problem, I'm still not sure why – Lunchbox Apr 04 '14 at 06:37
  • Long time ago,I solved it,I was also able to find out how to add round corners and linear gradients pure programatically.Though it is not the nicest gradient.This is what I am facing now.E.g.:I have a red button with round corners,borderwidth=1,black.It has a linear gradient,consisting of 5 colors,first one is white,3 following are red,last one is black,resulting in a faked 3D look of my round button.However,this is not the right glow/shade gradient.Buttons are highlighted in a small area upper right and darkened bottom left. I am looking for the right gradient to accomplish this with code. – icbytes Apr 04 '14 at 07:18
3

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);
    }
}
Jitesh Dalsaniya
  • 1,855
  • 2
  • 20
  • 35
  • The code looks good but you don't have to set the Paint object as final since your instantiate it again in the second constructor – StevenTB Sep 21 '16 at 08:23
1

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);
mayuri
  • 366
  • 1
  • 4
  • Hi, thanks, but it still doesn't work. I add my edit text to a TableRow and not a linearLayout. Added setMaxWidth and setMinHeight but it had no effect. – Lunchbox Oct 31 '13 at 14:59
0

This is you can make the border for the edit text. Save this into res/drawable

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
         <shape android:shape="rectangle">
         <gradient android:startColor="#f9f9f9"
                android:centerColor="#ffffff"
                android:endColor="#ffffff"
                android:angle="90"/>
         <stroke  android:width="1dp" android:color="#2B547E"/>
        </shape>
         </item>
    </layer-list>
King of Masses
  • 17,833
  • 4
  • 58
  • 76
Akarsh M
  • 1,626
  • 2
  • 24
  • 47
0

Use

 editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));

Instated of

editText.setBackgroundResource(R.drawable.edit_text_back);
dharam
  • 7,739
  • 4
  • 37
  • 70