How to Handle IME Options on Action Button Click in Android?
Last Updated :
19 Oct, 2021
We often observe that a keyboard pops up when we try to enter input in editable fields. These inputs are generally accepted by the application for performing specific functions and display desired results. One of the most common editable fields, that we can see in most of the applications in daily use is the SearchBar.
YouTube search query results
When the keyboard is invoked, you see a button with a magnifier icon at the bottom. This lets the user assume that clicking this button would help them search their query. However, the developers of this application have explicitly set the magnifier icon, which by default could be something else. This can be done by explicitly specifying the type of icon required using the IME Options. To learn about the list of available icons and how to change them, refer to the below article. Invoking Search Button in Keyboard While Typing in EditText in Android. In this article, we will show you how to handle the on-click events on these buttons.
Step by Step Implementation
Step 1: Create a New Project
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.
XML
<?xml version="1.0" encoding="utf-8"?>
<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/et1"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_centerHorizontal="true"
android:inputType="text"
android:autofillHints="Type something"
tools:ignore="LabelFor" />
</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. 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.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring the EditText from the layout file
val myEditText = findViewById<EditText>(R.id.et1)
// Calling the doSomething function
doSomething(myEditText)
}
private fun doSomething(search: EditText){
search.setOnEditorActionListener(TextView.OnEditorActionListener{ _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do something of your interest.
// We in this examples created the following Toasts
if(search.text.toString() == "geeksforgeeks"){
Toast.makeText(applicationContext, "Welcome to GFG", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(applicationContext, "Invalid Input", Toast.LENGTH_SHORT).show()
}
return@OnEditorActionListener true
}
false
})
}
}
So basically, in the function doSomething, you can observe IME_ACTION_DONE in the outer if condition. This is because when we invoked the keyboard by pressing on the EditText, we saw the Done button. This is when no IME Options is set in attributes of the Edit Text in the Layout or is a default condition. But if you set it to search, go, etc, then you may like to refer below to make the application work. Replace with:
- EditorInfo.IME_ACTION_DONE = When no IME options is specified (default)
- EditorInfo.IME_ACTION_SEARCH = When IME options is actionSearch
- EditorInfo.IME_ACTION_GO = When IME options is actionGo
- EditorInfo.IME_ACTION_NEXT = When IME options is actionNext
- EditorInfo.IME_ACTION_PREVIOUS = When IME options is actionPrevious
- EditorInfo.IME_ACTION_SEND = When IME options is actionSend
Input:
Click on EditText and type something. Now press the Done button at the bottom and observe if a Toast appears. Similarly, type in "geeksforgeeks" and observe the Toast.
Output:
You can see that we observe different Toasts for different inputs. This shows that our function correctly responds to the keyboard action button (Done button) in the form of Toasts.
Similar Reads
How to change Input Method Action Button in Android?
In this article, IME(Input Method Action) Option is changed in android according to our requirement. Input Method Action Button is located in the bottom right corner of the soft keyboard. By default, the system uses this button for either a Next or Done action unless your text field allows multi-lin
2 min read
How to Add Image on Floating Action Button in Android?
A floating action button (FAB) is a user interface element in the mobile application that is typically circular and floats above the main content. It usually has a prominent color and icon, and it is used to provide quick access to the most commonly used action within the app. Step-by-Step Implement
1 min read
How to Create New ImageView Dynamically on Button Click in Android?
In this article, we are going to implement a very important feature related to ImageView. Here we are going to add ImageView dynamically. We will be just changing the background color. Whenever we click on a button a new ImageView will be created. So here we are going to learn how to implement that
3 min read
How to Change Colors of a Floating Action Button in Android?
Android applications use the Floating Action Button for prompting the user to perform some important action within the android application. Floating Action Buttons in android applications are used to perform some important functionality within android applications. Many times in the android applicat
3 min read
How to Combine Text and Image on a Button or ImageButton in Android?
Image Buttons are used in many android applications for performing some actions. Some of the users of mobile applications sometimes are not aware of the images which are present on that specific buttons. So for informing the user about that button we also add a simple text to our button so that the
4 min read
Android Floating Action Button in Kotlin
Floating action buttons are used in android applications to indicate the user for some priority-based task. Generally floating action buttons in the android applications are found aligned to the bottom end of the application. In this article, we will take a look at How to implement the Floating Acti
4 min read
Floating Action Button using Fab Option Library in Android
Floating Action Button using Fab Options is another unique way of displaying various options. With the help of this, we can Navigate to different screens easily. This Floating Action button display various menu with Animation. So it increases user experience. In this article, we are going to learn h
3 min read
How to Set Buttons Inside an Alert Dialog in Android?
In this article, we are going to see how can we set two buttons inside an Alert Dialog. For example, we can see when an alert dialog is pop up then their two types of buttons available one is the Ok or Positive Button another one is Cancel or Negative Button. Alert dialogs are most frequently used i
4 min read
Theming Floating Action Buttons in Android with Example
Prerequisite: Floating Action Button (FAB) in Android with ExampleExtended Floating Action Button in Android with Example Android application developers want to seek the attention of the users by customizing and theming the android application widgets and keep more traffic of customers only by the d
12 min read
How to Change the Position of AlertDialog in Android?
AlertDialog in android is one of the UI widgets which immediately pops up to confirm the user interaction or to confirm the action which is done by the user. In most of the applications, the position of the alert dialog is in the center. In this article, it's been discussed how to change the positio
4 min read