Android add border to edit text programmatically
Asked Answered
C

5

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>
Coumarone answered 31/10, 2013 at 12:16 Comment(9)
editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_text_back)); try this @CoumaroneLyophobic
I think this thread might help you. #3496769Marceau
The example SO question you linked is quite rich to explain ..Ambulator
@BojanJovanovic I tried your answer but still no result, instead of extending TextView, I just extended EditText. Thanks thoughCoumarone
@Indiandroid I tried it but it did not work, see my editCoumarone
@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.Coumarone
@Coumarone it does not matter weather you call it from XML or from your java file .Ambulator
@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 problemCoumarone
@Coumarone 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?Convulsive
W
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>
Wastage answered 31/10, 2013 at 12:43 Comment(2)
Hi, it did not work, probably because I created my edit text programmatically. See my editCoumarone
@simmant It is a good answer, but it only works for an edit text that was defined in xml. That is where my problem liesCoumarone
L
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);
    }
}
Lawanda answered 13/5, 2015 at 9:57 Comment(1)
The code looks good but you don't have to set the Paint object as final since your instantiate it again in the second constructorAzerbaijani
W
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);
Wastage answered 31/10, 2013 at 13:40 Comment(1)
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.Coumarone
R
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>
Renunciation answered 31/10, 2013 at 12:32 Comment(7)
What is? The xml is saved to my res/drawableCoumarone
Weird, maybe try again in a couple of minutes?Coumarone
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="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>Renunciation
just copy and saved into the file_name.xml into res/drawableRenunciation
Hi, I have tried this, it does work for an edit text that exists in xml, but mine does not, since it was created in java. Thanks for the effort thoughCoumarone
what you tried ... plese write about it ... cz I hope that you are missing somethingRenunciation
Sure, I have added borders to an edit text before, but they were defined in xml. Now I created a edit text in java and adding a border does not work. The edit text works so there are no errors, the border just does not displayCoumarone
A
0

Use

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

Instated of

editText.setBackgroundResource(R.drawable.edit_text_back);
Ambulator answered 31/10, 2013 at 13:23 Comment(4)
Hi, sorry, its a no go. I tried it just now again, and still the same result, just my edit text without any border.Coumarone
@Coumarone i just tried on my my galaxy note-3 . seems it works .Ambulator
<EditText android:id="@+id/edt_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/edit_tx_bg" android:ems="10" android:inputType="textEmailAddress" > <requestFocus /> </EditText> edtLogin.setBackground(getResources().getDrawable(R.drawable.test));Ambulator
I edited my answer, that is what I meant by not defining my edit text in xml, sorry if I caused any confusionCoumarone

© 2022 - 2024 — McMap. All rights reserved.