The Wayback Machine - https://web.archive.org/web/20240811214927/https://www.geeksforgeeks.org/how-to-customize-appcompat-edittext-in-android/
Open In App

How to Customize AppCompat EditText in Android?

Last Updated : 18 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The EditText is one of the important UI element which takes the data as input from the user. The usual EditText in Android which looks decent enough has only the hint text and line which makes the user click on that line and insert the data. Refer to the article EditText widget in Android using Java with Examples, which explains basics about the normal AppCompat EditText. In this article, it’s been discussed how to customize the AppCompat EditText. Have a look at the following image to differentiate between the usual non-customized AppCompat EditText and the customized AppCompat EditText.

Customize AppCompat EditText in Android

Steps to customize the AppCompat EditText in Android

Step 1: Create an empty Activity Project

Step 2: Working with the activity_main.xml file

  • Only two EditText widgets are implemented in the main layout file. One is Username and another is Password field.
  • Invoke the following code to implement the same UI.

XML




<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <!--E-mail field-->
        <EditText
            android:id="@+id/emailField1"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:hint="Email" />
  
        <!--Password field-->
        <EditText
            android:id="@+id/passwordField1"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:hint="Password" />
  
    </LinearLayout>
  
</ScrollView>


Output UI:

Customize AppCompat EditText in Android

Step 3: Import vector icons

  • Import vector icons into the drawable folder.
  • To import the vector icons right-click on the drawable folder > New > Vector Asset.
  • Make sure to make copies of the single icon in the drawable folder. One icon when the edit text is out of focus and when the edit text is in focus.
  • Set the color of the icon to grey when the edit text is out of focus and set the color of the icon to colorPrimary when the EditText is in focus.
  • Refer to the following image to get the steps discussed above.

Customize AppCompat EditText in Android

  • And combining both the icons make a selector XML layout custom_mail_icon as following.

XML




<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--the icon turns to green when the edit text is in focus-->
    <item android:drawable="@drawable/ic_mail_focused" android:state_focused="true" />
  
    <!--the icon turns to grey when the edit text is out of focus-->
    <item android:drawable="@drawable/ic_mail" android:state_focused="false" />
</selector>


  • The same goes for the password field icon.

First, the EditText with the cut-cornered layout is discussed

Step 1: Create a Selector layout background for the EditText

  • This is the layout that gives the cut-cornered box for the EditText field.
  • To implement the same create a custom_edit_text_cut.xml file under the drawable folder and invoke the following code.

XML




<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  
    <!--the outline box turns to green when the edit text is in focus-->
    <item android:state_enabled="true" android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <stroke android:width="2dp" android:color="@color/colorPrimary" />
        </shape>
    </item>
  
    <!--the outline box turns to grey when the edit text is out of focus-->
    <item android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <stroke android:width="2dp" android:color="@android:color/darker_gray" />
        </shape>
    </item>
  
</selector>


Step 2: Working with the activity_main.xml file

  • In the activity_main.xml file invoke the background for the EditText as custom_edit_text_cut.xml.

XML




<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <!--invoke the background as the 
            custom_edit_text_cut-->
        <EditText
            android:id="@+id/emailField1"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/custom_edit_text_cut"
            android:drawableStart="@drawable/custom_mail_icon"
            android:drawablePadding="12dp"
            android:hint="Email"
            android:paddingStart="12dp"
            android:paddingEnd="12dp" />
  
        <!--same background for the password field 
            as the custom_edit_text_cut-->
        <EditText
            android:id="@+id/passwordField1"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/custom_edit_text_cut"
            android:drawableStart="@drawable/custom_vpn_icon"
            android:drawablePadding="12dp"
            android:hint="Password"
            android:paddingStart="12dp"
            android:paddingEnd="12dp" />
  
    </LinearLayout>
  
</ScrollView>


Output: Run on Emulator

Second, the EditText with the round-cornered layout is discussed

Step 1: Create a Selector layout background for the EditText

  • This is the layout that gives the cut-cornered box for the EditText field.
  • To implement the same create a custom_edit_text_rounded.xml file under the drawable folder and invoke the following code.

XML




<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  
    <!--The outline turns to colorPrimary when the EditText is in focus-->
    <item android:state_enabled="true" android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <corners android:radius="12dp" />
            <stroke android:width="2dp" android:color="@color/colorPrimary" />
        </shape>
    </item>
  
    <!--The outline turns to grey when the EditText is out of focus-->
    <item android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <corners android:radius="12dp" />
            <stroke android:width="2dp" android:color="@android:color/darker_gray" />
        </shape>
    </item>
  
</selector>


Step 2: Working with the activity_main.xml file

  • In the activity_main.xml file invoke the background for the EditText as custom_edit_text_rounded.xml.

XML




<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  
        <!--invoke the background as the 
            custom_edit_text_rounded-->
        <EditText
            android:id="@+id/emailField1"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/custom_edit_text_rounded"
            android:drawableStart="@drawable/custom_mail_icon"
            android:drawablePadding="12dp"
            android:hint="Email"
            android:paddingStart="12dp"
            android:paddingEnd="12dp" />
  
        <!--same background for the password field 
            as the custom_edit_text_rounded-->
        <EditText
            android:id="@+id/passwordField1"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/custom_edit_text_rounded"
            android:drawableStart="@drawable/custom_vpn_icon"
            android:drawablePadding="12dp"
            android:hint="Password"
            android:paddingStart="12dp"
            android:paddingEnd="12dp" />
  
    </LinearLayout>
  
</ScrollView>


Output: Run on Emulator



Similar Reads

Fix "Error Could not find method implementation() for arguments [com.android.support:appcompat-v7:26.0.0]" in Android Studio
Implementation is a dependency configuration used for library declaration and was introduced in the Android Gradle Plugin 3.0 by Google. Implementation dependencies are declared in this configuration which is internal and not meant for consumer exposure. The main reason for "Could not find method implementation() for arguments [com.android.support:
3 min read
Android - You need to use a Theme.AppCompat theme (or descendant) with this activity
You need to use a Theme.AppCompat theme (or descendant) with this activity is a common error message that appears when trying to run an Android application. This error occurs because the Android operating system requires the use of a specific theme for the application's main activity. In this article, we'll explore the cause of this error and the s
4 min read
Android EditText in Kotlin
EditText is used to get input from the user. EditText is commonly used in forms and login or registration screens. Following steps are used to create EditText in Kotlin: Add a EditText in activity_main.xml file.Add a Button in activity_main.xml file.Open MainActivity.kt file and set OnClickListener for the button to get the user input from EditText
3 min read
EditText widget in Android using Java with Examples
Widget refers to the elements of the UI (User Interface) that helps user interacts with the Android App. Edittext is one of many such widgets which can be used to retrieve text data from user. Edittext refers to the widget that displays an empty textfield in which a user can enter the required text and this text is further used inside our applicati
4 min read
How to Add Mask to an EditText in Android?
EditText is an android widget. It is a User Interface element used for entering and modifying data. It returns data in String format. Masking refers to the process of putting something in place of something else. Therefore by Masking an EditText, the blank space is replaced with some default text, known as Mask. This mask gets removed as soon as th
3 min read
Theming of Material Design EditText in Android with Example
Prerequisite: Material Design EditText in Android with Examples In the previous article Material Design EditText in Android with Examples Material design Text Fields offers more customization and makes the user experience better than the normal text fields. For example, Icon to clear the entire EditText field, helper text, etc. In this article, it'
4 min read
Key Properties of Material Design EditText in Android
In the previous article Material Design EditText in Android with Examples its been discussed how to implement the Material design EditText and how they differ from the normal EditText. In this article its been discussed on how to customize MDC edit text fields with their key properties. Have a look at the following image to get the idea about the k
4 min read
EditText in Android using Jetpack Compose
EditText is one of the most important widget which is seen in most of the apps. This widget is generally use to get the data from users. Users can directly communicate with the app using this widget. This widget is used to get the data from user in the form of numbers, text or any other text. In this article we will take a look at the implementatio
5 min read
Working With the EditText in Android
EditText is one of the basic UI widgets, which is used to take the input from the user. The EditText is derived or is the extension of the TextView in Android. This article its been discussed in detail about the EditText in Android. The article also contains some of the redirections to other articles, also refer to them to get the detailed perspect
9 min read
How to Make TextView and EditText Selectable in Android?
In this article, we are going to implement a very important feature related to TextView. While using any social media app or like using Facebook you may have seen there is a particular type of TextView which you cannot copy like a caption that people write on their posts. You can select the message but there few texts which cannot select or copy. S
3 min read
three90RightbarBannerImg