Edittext change border color with shape.xml
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" />
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
.
<?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
.