user3265784
user3265784

Reputation:

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?

Upvotes: 39

Views: 121614

Answers (9)

satyakiran vakada
satyakiran vakada

Reputation: 61

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!

Upvotes: 1

Soumen Das
Soumen Das

Reputation: 1302

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>

Upvotes: 0

frogatto
frogatto

Reputation: 29285

Why using selector as the root tag? selector is used for applying multiple alternate drawables for different states of the view, so in this case, there is no need for selector.

Try the following code.

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

    <!-- Background Color -->
    <solid android:color="#ffffff" />

    <!-- Border Color -->
    <stroke android:width="1dp" android:color="#ff9900" />

    <!-- Round Corners -->
    <corners android:radius="5dp" />

</shape>

Also It's worth mentioning that all color entries support alpha channel as well, meaning that you can have transparent or semi-transparent colors. For example #RRGGBBAA.

Upvotes: 92

Sunny Sultan
Sunny Sultan

Reputation: 1180

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>

Upvotes: 1

Manikanta Reddy
Manikanta Reddy

Reputation: 641

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" />

Upvotes: 13

Narendra Baratam
Narendra Baratam

Reputation: 828

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

Upvotes: 2

Shanka Nuwan
Shanka Nuwan

Reputation: 43

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

Upvotes: 1

Harsh Patel
Harsh Patel

Reputation: 459

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>

Upvotes: 1

gsanskar
gsanskar

Reputation: 669

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

Upvotes: 0

Related Questions