[android] Edittext change border color with shape.xml

I make a shape.xml file under res -> drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
        <solid android:color="#ffffff" />
        <stroke android:width="1dip" android:color="#ff9900" />
</selector>

And then I use it on an EditText:

<EditText
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:id="@+id/editText"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="300dp"
    android:hint="@string/hint"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:singleLine="true"
    android:background="@drawable/shape"/>

But the result is that it doesn't change the border color at all. Why, what's wrong?

This question is related to android android-edittext

The answer is


Use this code on xml . i hope it will be work

<?xml version="1.0" encoding="utf-8" ?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:thickness="0dp"
           android:shape="rectangle">
      <stroke android:width="3dp"
             android:color="#4799E8"/>
      <corners android:radius="5dp" />
      <gradient
       android:startColor="#C8C8C8"
       android:endColor="#FFFFFF"
       android:type="linear"
       android:angle="270"/>
    </shape>

Check below code may will help you, Using stroke can make border in edit text and change it's color too as shown below...

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<stroke
    android:width="2dp"
    android:color="@color/secondary" />
<corners
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />

Add this as background in to edit text. Thanks!


selector is used for applying multiple alternate drawables for different status of the view, so in this case, there is no need for selector

instead use shape

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#ff9900" />
</shape>

Use root tag as shape instead of selector in your shape.xml file, and it will resolve your problem!


This is work for me: Drwable->New->Drawable Resource File->create xml file

  <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#e0e0e0" />
        <stroke android:width="2dp" android:color="#a4b0ba" />
    </shape>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     android:shape="rectangle">
     <solid android:color="#ffffff" />
     <stroke android:width="1dip" android:color="#ff9900" />
 </selector>

You have to remove > this from selector root tag, like below

   <selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

As well as move your code to shape from selector.


Step 1:Create a border.xml in Drawable folder

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:radius="2dp"
        />
    <solid android:color="#ffffff"
        />
    <stroke
        android:width="2dip"
        android:color="#000" />
</shape>

Step 2: Create a EditText in XML File

 <EditText
        android:id="@+id/etEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:hint="Enter Email"
        android:padding="10dp"
        android:layout_marginRight="25dp"
        android:background="@drawable/border"
        android:inputType="textEmailAddress"
        android:singleLine="true" />

i use as following for over come this matter

edittext_style.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="1dp"
        android:color="#c8c8c8"/>
<corners android:radius="0dp" />

And applied as bellow

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editTextName"
            android:background="@drawable/edit_text_style"/>

try like this..