How to Change Input Type of EditText Programmatically in Android?
Last Updated :
09 Dec, 2021
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 two inputs namely an email id and a numerical pin. So we need to create two EditText fields for taking the two inputs. This is how a general login activity page is assumed. However, we can create a similar activity using a single EditText and use it twice for taking two same or different types of input. The first input can be stored inside a variable upon a button click. At the same time, this click will clear the EditText. Now second input can be given and stored in another variable on button click. In this way, we shall get both the inputs using a single EditText. This is when both the inputs are of the same type. The problem arises when both input types are different, let us say, the first input is a string and the second is some numeric value. So in this article, we will show you how you could change the EditText input type programmatically using a simple Android app demonstration. Follow the below instructions once you launch the IDE.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We have created an EditText and two Buttons. These two non-functional buttons would invoke the EditText input type accordingly when clicked.
XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="50sp"/>
<Button
android:id="@+id/textBtn"
android:layout_above="@id/numBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="text"/>
<Button
android:id="@+id/numBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="number"/>
</RelativeLayout>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. The EditText and Buttons are declared into the main code. Here, the two functions are given the functionality that when clicked, EditText will change its input type. Comments are added inside the code to understand the code in more detail.
Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.InputType
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring and initializing the EditText
// and Buttons from the layout file
val mEditText = findViewById<EditText>(R.id.edit_text)
val mTextBtn = findViewById<Button>(R.id.textBtn)
val mNumBtn = findViewById<Button>(R.id.numBtn)
// When this button is clicked, the
// EditText input type will change to Text
mTextBtn.setOnClickListener {
mEditText.inputType = InputType.TYPE_CLASS_TEXT
}
// When this button is clicked, the
// EditText input type will change to Numbers
mNumBtn.setOnClickListener {
mEditText.inputType = InputType.TYPE_CLASS_NUMBER
}
}
}
Output:
You can see in the output that when these buttons are clicked, the input type changes.
Similar Reads
How to Change the Whole App Language in Android Programmatically? Android 7.0(API level 24) provides support for multilingual users, allowing the users to select multiple locales in the setting. A Locale object represents a specific geographical, political, or cultural region. Operations that require these Locale to perform a task are called locale-sensitive and u
5 min read
How to Change TextView Size Programmatically in Android? A TextView in Android is a UI element to display text. It can be programmed in the layout file statically as well as in the main code dynamically. Thus, various attributes of a TextView such as the text, text color, text size, TextView background, and its size can be changed programmatically. In thi
3 min read
How to Get Current Default Language of Android Device Programmatically? Smartphones seem to be predicting and showing us results from what we think in our minds. If you think of traveling, you shall soon expect something related to it in your device's feed. This is the next level of technology, where data helps in developing businesses. The software collects personal da
2 min read
How to Change ActionBar Title Programmatically in Android? Whenever we develop an Android application and run it, we often observe that the application comes with an ActionBar with the name of the application in it. This happens by default unless explicitly changed. This text is called the title in the application. One can change the title of the applicatio
3 min read
How to Invoke Keyboard Programmatically in Android? Android System by defaults shows an on-screen keyboard when any UI element such as an Input Text element receives focus. For a better experience, a developer can explicitly specify the desired characteristics or any methods to invoke. Desired characteristics could be characters such as allowing only
2 min read