How to Add Mask to an EditText in Android?
Last Updated :
28 Apr, 2025
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 the user enters any character as input, and reappears when the text has been removed from the EditText. In this article, the masking is done with the help of JitPack Library, because it can be customized easily according to the need to implement various fields like phone number, date, etc. The code has been given in both Java and Kotlin Programming Language for Android.
Approach:
Add the support Library in your settings.gradle File. This library Jitpack is a novel package repository. It is made for JVM so that any library which is present in Github and Bitbucket can be directly used in the application.
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
// add the following
maven { url "https://jitpack.io" }
}
}Add the below dependency in the dependencies section of your App/Module build.gradle File. It is a simple Android EditText with custom mask support. Mask EditText is directly imported and is customized according to the use.
dependencies {
implementation 'ru.egslava:MaskedEditText:1.0.5'
}Now add the following code in the activity_main.xml file. It will create three mask EditTexts and one button in activity_main.xml.
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">
<br.com.sapereaude.maskedEditText.MaskedEditText
android:hint="#### #### #### ####"
android:layout_width="match_parent"
android:inputType="number"
app:mask="#### #### #### ####"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:id="@+id/card"/>
<!-- Set the masked characters -->
<br.com.sapereaude.maskedEditText.MaskedEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:id="@+id/phone"
android:hint="9876543210"
android:inputType="phone"
app:keep_hint="true"
app:mask="+91 ### ### ####"/>
<!-- Set the masked characters -->
<br.com.sapereaude.maskedEditText.MaskedEditText
android:hint="##:##:####"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:id="@+id/Date"
android:inputType="date"
app:mask="##:##:####"/>
<!-- Set the masked characters -->
<Button
android:id="@+id/showButton"
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="18sp"
android:text="Show"/>
</LinearLayout>
Now add the following code in the MainActivity File. All the three mask EditTexts and a button are defined. An onClickListener() is added on the button which creates a toast and shows all the data entered in the mask EditTexts.
Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import br.com.sapereaude.maskedEditText.MaskedEditText;
public class MainActivity extends AppCompatActivity {
MaskedEditText creditCardText,phoneNumText,dateText;
Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
creditCardText = findViewById(R.id.card);
phoneNumText = findViewById(R.id.phone);
dateText = findViewById(R.id.Date);
show = findViewById(R.id.showButton);
show.setOnClickListener(v -> {
// Display the information from the EditText with help of Toasts
Toast.makeText(MainActivity.this, "Credit Card Number " + creditCardText.getText() + "\n Phone Number "
+ phoneNumText.getText() + "\n Date " + dateText.getText(), Toast.LENGTH_LONG).show();
});
}
}
Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import br.com.sapereaude.maskedEditText.MaskedEditText
class MainActivity : AppCompatActivity() {
lateinit var creditCardText: MaskedEditText
lateinit var phoneNumText: MaskedEditText
lateinit var dateText: MaskedEditText
lateinit var show: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
creditCardText = findViewById(R.id.card)
phoneNumText = findViewById(R.id.phone)
dateText = findViewById(R.id.Date)
show = findViewById(R.id.showButton)
show.setOnClickListener {
// Display the information from the EditText with help of Toasts
Toast.makeText(this, ("Credit Card Number " + creditCardText.getText()) + "\n Phone Number "
+ phoneNumText.getText().toString() + "\n Date " + dateText.getText(), Toast.LENGTH_LONG
).show()
}
}
}
Output:
Similar Reads
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 quer
3 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
3 min read
Android EditText in Kotlin EditText is a widget in Android, that is used to get input from the user. EditText is commonly used in forms and login or registration screens. EditText extends the TextView class and provides more functionalities including handing text inputs such as cursor control, keyboard display and text valida
3 min read
How to Create Marquee Text in Android? In this article, we are going to create Marquee Text in Android Studio. Marquee is a scrolling piece of text that is displayed either horizontally or vertically. It is used to show some important notices or headlines. It makes the app UI much more attractive. Note that we are going to use Java as th
3 min read
How to Create Text Stickers in Android? Not only a picture is worth a thousand words but also, they are cooler than monotonous text. In this article, we will learn how to create stickers from the TextView. This will be helpful if you are creating an app in which you want to add some text overlay functionality, for example, an image editin
9 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.
2 min read
How to Draw a Track on Google Maps in Android? In many android apps, we have seen that there is a route marker from a source location to the destination location. In this article, we will take a look at How we can draw a track on Google Maps in Android. What we are going to build in this article? We will be building a simple application in whi
3 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 strin
3 min read
How to Dynamically Add Elements to a ListView in Android? ListView is a UI widget in android which is used in most android applications. We can display the list of data using the list view. We can dynamically add or remove items from the list view by performing some operations of the list. In this article, we will take a look at How to add Elements to a Li
4 min read
How to Add Text Drawable to ImageView in Android? In many android apps, you will get to see a feature in which you can see a simple text is displayed inside an ImageView or you can get to see that a text is displayed in a specific image or a shape. Mostly this type of view is seen in the Contacts application which is present on your Android device.
5 min read