How to Disable Soft Keyboard Suggestions in Android?
In Android, we must have observed that in any application, if we are supposed to type in some input, we generally get suggestions for words and sentences from the keyboard. The keyboard helps us type incorrect words or sentences when we start typing. Similarly, it helps auto-correct if mistakenly type wrong spellings. This function is can be changed but not completely removed unless programmed explicitly. So in this article, we will show you how you could programmatically disable soft keyboard suggestions in Android. Follow the below steps once the IDE is ready.
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:
So there are two methods we can do this. In the first method, we statically declare no text suggestions in EditText. In the second method, we dynamically do this via the main code.
Method 1: 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. Add the EditText as shown below. Add inputType as shown below to disable suggestions.
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/edit_text_1" android:layout_width = "300sp" android:layout_height = "50sp" android:hint = "no suggestions..." android:inputType = "textNoSuggestions|textVisiblePassword" android:layout_centerInParent = "true" /> </ RelativeLayout > |
Method 2: 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
package org.w3wiki.disablekeyboardsuggestion import android.os.Bundle import android.view.inputmethod.EditorInfo import android.widget.EditText import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring and initializing // the EditText from the layout val mEditText = findViewById<EditText>(R.id.edit_text_1) // Disabling the suggestions mEditText.inputType = EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS } } |
Output:
In both cases, we get the same output as shown below. We can see that the suggestions have been disabled.