Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create EditText with rounded corners? [closed]

Is there any way to create EditText that has rounded corners?

like image 923
pixel Avatar asked Sep 05 '10 14:09

pixel


People also ask

How do you make text edit round?

This example demonstrates how to create EditText with rounded corners in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I make TextView rounded?

xml file and add an attribute to that TextView, for which you want to add rounded corners. The attribute is android: background=”@drawable/rounded_corner_view”.


1 Answers

There is an easier way than the one written by CommonsWare. Just create a drawable resource that specifies the way the EditText will be drawn:

<?xml version="1.0" encoding="utf-8"?> <!--  res/drawable/rounded_edittext.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle"      android:padding="10dp">      <solid android:color="#FFFFFF" />     <corners         android:bottomRightRadius="15dp"         android:bottomLeftRadius="15dp"         android:topLeftRadius="15dp"         android:topRightRadius="15dp" /> </shape> 

Then, just reference this drawable in your layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">      <EditText           android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:padding="5dip"         android:background="@drawable/rounded_edittext" /> </LinearLayout> 

You will get something like:

alt text

Edit

Based on Mark's comment, I want to add the way you can create different states for your EditText:

<?xml version="1.0" encoding="utf-8"?> <!-- res/drawable/rounded_edittext_states.xml --> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item          android:state_pressed="true"          android:state_enabled="true"         android:drawable="@drawable/rounded_focused" />     <item          android:state_focused="true"          android:state_enabled="true"         android:drawable="@drawable/rounded_focused" />     <item          android:state_enabled="true"         android:drawable="@drawable/rounded_edittext" /> </selector> 

These are the states:

<?xml version="1.0" encoding="utf-8"?> <!-- res/drawable/rounded_edittext_focused.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle" android:padding="10dp">      <solid android:color="#FFFFFF"/>     <stroke android:width="2dp" android:color="#FF0000" />     <corners         android:bottomRightRadius="15dp"         android:bottomLeftRadius="15dp"         android:topLeftRadius="15dp"         android:topRightRadius="15dp" /> </shape> 

And... now, the EditText should look like:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">      <EditText           android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="@string/hello"         android:background="@drawable/rounded_edittext_states"         android:padding="5dip" /> </LinearLayout> 
like image 52
Cristian Avatar answered Sep 20 '22 01:09

Cristian