How to have EditText with border in Android Lollipop
I am developing an Android app. I need to know how we can have a EditText with border. In Lolipop they have completely changed the EditText style. Can we do it without using drawables?
Solution 1:
Write editTextBackground.xml in drawable folder in resources
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/borderColor" />
</shape>
don't forget to declare color in resources named borderColor
.
and assign this background to the EditText
in xml background attribute
<EditText
android:id="@+id/text"
android:background="@drawable/editTextBackground"
/>
and it'll set border to EditText
.
UPDATE
You can change border of edit text without drawable by using style
attribute
style="@style/Widget.AppCompat.EditText"
for more details visit customize edit text
Solution 2:
You can use a drawable. Create a drawable layout file in your drawable folder. Paste this code. You can as well modify it - border.xml.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/divider" />
<solid
android:color="#00FFFFFF"
android:paddingLeft="10dp"
android:paddingTop="10dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
in your EditText view, add
android:background="@drawable/border"
Solution 3:
You can do it using xml.
Create an xml layout and name it like my_edit_text_border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<item>
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
<corners android:radius="5dp" />
<stroke
android:width="2dp"
android:color="#949494"
/>
</shape>
</item>
</selector>
Add background to your Edittext
<EditText
android:id="@+id/editText1"
..
android:background="@layout/my_edit_text_border">
Solution 4:
For correct work your shape should be with selector and item tags
<?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="#ffffff" />
<stroke android:width="1dp"
android:color="@color/shape_border_active"/>
</shape>
</item>
</selector>