The Wayback Machine - https://web.archive.org/web/20240927040359/https://www.geeksforgeeks.org/material-design-edittext-in-android-with-examples/
Open In App

Material Design EditText in Android with Example

Last Updated : 01 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

EditText is one of the important UI elements. Edittext refers to the widget that displays an empty text field in which a user can enter the required text and this text is further used inside the application. In this article it has been discussed to implement the special type of text fields, those are called Material Design EditText. Have a look at the normal edit text in Android and the Material design Text fields in Android. The design and the easy-to-use implementation make them different from normal EditText fields.


Material Design EditText

Step by Step Implementation

In this example, we are going to demonstrate two important types of Material Design EditText:

  1. Filled EditText
  2. Outlined EditText

Step 1: Create a New Project


Step 2: Invoke the dependency to the app level gradle file

implementation ‘com.google.android.material:material:1.12.10 

  • Get the app level gradle file by going to app > build.gradle file. And click on the “Sync Now” button. And make sure the system should be connected to the network.
  • Refer to the following image to locate and invoke the dependency in-app level gradle file (Under project hierarchy view).


App level Gradle file


Step 3: Change the base theme of the application

  • We need to change the base theme of the application because we are using the material design components. Otherwise, the application crashes immediately after it is launched.
  • To change the base theme of the application open app > src > main > res > values > styles.xml. 


styles.xml
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>


Refer to the following image to locate the styles.xml  file and change the base theme of the application.


styles.xml file


Implementing the Material Design Filled EditText

Step 4: Working with the activity_main.xml file

  • Invoke the following code to implement the filled EditText.
  • Below is the code for the activity_main.xml file.
  • Comments are added inside the code to understand the code in more detail.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <!--this is the filled layout box for the edit text-->
    <!--this layout must be used to reposition or change 
        the height and width of the edit text-->
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/filledTextField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:layout_marginEnd="32dp"
        android:hint="Enter something">

        <!--this is the actual edit text which takes the input-->
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

    <!--sample button to submit entered data 
        inside from edit text-->
    <Button
        android:id="@+id/submit_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="32dp"
        android:text="Submit" />

    <!--text view which previews the entered data by user-->
    <TextView
        android:id="@+id/text_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:text="You Entered : "
        android:textSize="18sp" />

</LinearLayout>


In the above code the “com.google.android.material.textfield.TextInputLayout”  makes the filled box for the EditText field.

And “com.google.android.material.textfield.TextInputEditText” which is the actual edit text which takes input from the user and this must be used to handle all the inputs in the MainActivity file.

Output UI is produced as: 

Material Design EditText


Step 5: Working with the MainActivity file 

  • Now invoke the following java code to handle the material design EditText.
  • Below is the code for the MainActivity file.
  • Comments are added inside the code to understand the code in more detail.
Java
package com.example.material_design_java;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.textfield.TextInputEditText;

public class MainActivity extends AppCompatActivity {

    // UI widgets to handle
    Button bSubmit;
    TextInputEditText mEditText;
    TextView tvTextPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Register the UI widgets
        // with their appropriate IDs.
        bSubmit = findViewById(R.id.submit_button);
        mEditText = findViewById(R.id.edit_text);
        tvTextPreview = findViewById(R.id.text_preview);

        // handle submit button to preview the entered data
        bSubmit.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                // set the entered data to text preview
                tvTextPreview.setText("You Entered : " + mEditText.getText().toString());
            }
        });
    }
}
Kotlin
package com.example.material_design_android_kotlin

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.textfield.TextInputEditText

class MainActivity : AppCompatActivity() {

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Register the UI widgets with their appropriate IDs.
        val bSubmit = findViewById<Button>(R.id.submit_button)
        val mEditText = findViewById<TextInputEditText>(R.id.edit_text)
        val tvTextPreview = findViewById<TextView>(R.id.text_preview)

        // handle submit button to
        // preview the entered data
        bSubmit.setOnClickListener {
            tvTextPreview.text = "You Entered : " + mEditText.text.toString()
        }
    }
}

Output: Run on Emulator


Implementing the Material Design Outlined EditText

Step 6: Working with the activity_main.xml file

  • Invoke the following code to implement the filled edit text.
  • Only difference is the style attribute in the “com.google.android.material.textfield.TextInputLayout” to be invoked.
  • Comments are added inside the code to understand the code in more detail.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <!--this is the outlined layout box for the edit text-->
    <!--this layout must be used to reposition or change the 
        height and width of the edit text-->
    <!--to get the outlined edit text the style attribute as 
        following must be invoked-->
    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/filledTextField"

        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:layout_marginEnd="32dp"
        android:hint="Enter something">

        <!--this is the actual edit text which takes the input-->
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

    <!--sample button to submit entered data inside from edit text-->
    <Button
        android:id="@+id/submit_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="32dp"
        android:text="Submit" />

    <!--text view which previews the entered data by user-->
    <TextView
        android:id="@+id/text_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="32dp"
        android:text="You Entered : "
        android:textSize="18sp" />

</LinearLayout>

Following output UI is produced:

Material Design EditText

Step 7: Same as Step 5

Refer to Step 5

Output: Run on Emulator



Previous Article
Next Article

Similar Reads

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
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
How to Customize AppCompat EditText in Android?
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 no
4 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
How to Check Typing Status of EditText in Android?
In this article, we are going to implement an important and quite interesting feature. While Chatting with your friends over various social media platforms you may have found that if your friend is typing something then it shows Typing. Here are going to implement the same. While we start typing something, It will show typing, and when we erase eve
2 min read
How to Set Restriction For URL in Android EditText?
In this article, we are going to set restrictions in our EditText for URL. Whenever we are creating any form and we want to get the portfolio website of the user or even if we want to restrict the user to write the only URL then we can use this feature. This enables a user to user to write URL only. Step By Step Implementation Step 1: Create a New
2 min read
Invoking Search Button in Keyboard While Typing in EditText in Android
EditText can sometimes be a way of communicating with applications. Generally, EditTexts are used to fill in information or queries by the user and execute some tasks. Once the text is typed, the information or the query can be passed in the background to make things available or do some operations. But to pass it, there should be some method like
2 min read
How to Stop EditText from Gaining Focus at Activity Startup in Android?
When working on an Android Project, you might face a hazy error, the activity starts, and then the EditText immediately gets the focus and the soft keyboard is forced open. Well, that's not something that we expect, and let's address this, and get rid of it! Method #1: Removing the Request Focus from the EditText You may force override the focus of
2 min read
How to Set Minimum and Maximum Input Value in EditText in Android?
In Android, EditText is a subclass of TextView which is configured to be editable. EditText is used for giving textual input such as characters, strings, numbers, etc. It has no limitation on the type of input unless explicitly attributed. Meaning we can attribute the EditText to accept only a string or a number. On top of this, we can create metho
3 min read
Restrict EditText Input to Some Special Characters in Android
In some of the other applications, most of us must have witnessed that while typing in some field, we get a warning or a flash message that certain characters are not allowed or are not accepted as valid input. For example, sometimes, while renaming a file or a folder in Windows, if we simply type in '?', we get a message that '?' is not accepted a
2 min read
How to Change Input Type of EditText Programmatically in Android?
EditText in Android UI or applications is an input field, that is used to give input text to the application. This input text can be of multiple types like text (Characters), number (Integers), etc. These fields can be used to take text input from the users to perform any desired operation. Let us say, a login activity in our application comprises
3 min read
How to Search an Item in ListView using EditText and TextWatcher in Android?
Some applications provide a search bar for looking up any particular item from a list of items. Technically, a search bar is an EditText and an item list can be a ListView, RecyclerView, or a GridView that contains some items. Now, when a user types something in the EditText, the list must update in accordance with the text that the user types. Str
3 min read
How to Disable Copy Functionality in EditText in Android?
In Android, EditText is used to let users type in text that can be numbers, characters, strings, or a paragraph. Data from EditText can be fetched and used to perform desired operations. As the primary input of an EditText is text, it can be manually typed or copied from another source and simply be pasted into it. Similarly, a text in EditText can
2 min read
How to Add Image on EditText in Android?
In Android, An EditText is an overlay over a TextView that makes it editable using text by invoking a soft keyboard in run time. It is generally implemented to collect text data from the user. EditText is most commonly used in forms that require users to fill in details and for passing a search query. In this article, we will show you how you could
3 min read
How to PopUp DatePicker While Clicking on EditText in Android?
In android applications date pickers are used to pick the date from the calendars and display them within our text view. Many times we have to display this date picker dialog box by clicking on edit text and then we have to display that date within our edit text. In this article, we will take a look at How to Pop Up a Date Picker Dialog in the andr
4 min read
Implement Form Validation (Error to EditText) in Android
Many of the already existing Android applications when it comes to the forms include user details. If the user enters the wrong information into the text fields or if the user leaves the text fields without filling them, there is a need to provide certain alert information to the TextFields which are unfilled and which contain the wrong information
6 min read
Theming Material Design Snackbars in Android with Example
In the previous article, it's been discussed Snackbar Material Design Components in Android. In this article, it's been discussed how we can theme material design Snackbar and increase the User Experience. Theming Example 1:This method is done using the styles.xml file. Where we need to override the default style of the Snackbar.Have a look at the
5 min read
Material Design Buttons in Android with Example
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Android applications. If you like the way how the UI el
8 min read
Material Design Components Chips in Android with Example
Chips in android are one of the components which are used to make the choice filters, actions, and display the selectable options in the compact area of the Android Window. In this article, it's been discussed how to implement the very basic selectable chips for the filtering of the options. Have a look at the following image to get an idea of what
4 min read
Dynamic EditText in Kotlin
EditText is used to get input from the user. EditText is commonly used in login or registration screens. we already learn how to create an EditText using layout. In this article, we will learn about how to create android EditText programmatically in kotlin. At first, we will create a new android application. Then, we will create an EditText dynamic
2 min read
Material Design Text Input Field using Jetpack Compose in Android
Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. Compose is built to support material design principles. Many of its UI elements implement material design out of the box. In this article, we will explain
3 min read
Theming Material Design Buttons in Android with Examples
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android application. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Android applications. If you like the way how the UI ele
8 min read
Material Design Date Picker in Android
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Android applications. If you like the way how the UI el
8 min read
Material Design Buttons using Jetpack Compose in Android
Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs. Compose is built to support material design principles. Many of its UI elements implement material design out of the box. In this article, we will explain
4 min read
Material Design Time Picker in Android
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android application. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Android applications. If you like the way how the UI ele
4 min read
Material Design Components Sliders in Android
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Android applications. Material design in Android is one
4 min read