Thursday, August 20, 2015

How to set EditText border color programmatically in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#c8cedc"
    >
    <EditText
        android:id="@+id/et"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:hint="This is first EditText"
        android:fontFamily="sans-serif-condensed"
        />
    <EditText
        android:id="@+id/et2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:fontFamily="sans-serif-condensed"
        android:hint="This is second EditText"
        android:layout_below="@id/et"
        />
</RelativeLayout>
res/drawable/edittext_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#fffae5"/>
            <stroke
                android:color="#ff1600"
                android:width="1dp"
                />
            <corners android:radius="7dp"/>
        </shape>
    </item>
</selector>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;
import android.graphics.drawable.ShapeDrawable;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        final EditText et = (EditText) findViewById(R.id.et);
        final EditText et2 = (EditText) findViewById(R.id.et2);

        /*
            setBackgroundResource (int resid)
                Set the background to a given resource. The resource
                should refer to a Drawable object or 0 to remove the background.
         */
        // Set the background resource of EditText widget
        et.setBackgroundResource(R.drawable.edittext_bg);

        // Or you can use this method
        /*
            setBackground (Drawable background)
                Set the background to a given Drawable, or remove
                the background. If the background has padding,
                this View's padding is set to the background's padding.
         */
        //et.setBackground(getDrawable(R.drawable.edittext_bg));

        // Create a border programmatically
        ShapeDrawable shape = new ShapeDrawable(new RectShape());
        shape.getPaint().setColor(Color.RED);
        shape.getPaint().setStyle(Paint.Style.STROKE);
        shape.getPaint().setStrokeWidth(3);

        // Assign the created border to EditText widget
        et2.setBackground(shape);
   }
}
More android examples