Hey I am working to detect the custom keyboard in my application. I did this successfully and detect user is custom keyboard by using code
fun isUsingCustomKeyboard(context: Context): Boolean {
val imm: InputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val inputMethodProperties: List<InputMethodInfo> = imm.enabledInputMethodList
for (i in 0..inputMethodProperties.size) { // loop to every input method
val inputMethodInfo = inputMethodProperties[i]
val inputMethodId = Settings.Secure.getString(
context.contentResolver,
Settings.Secure.DEFAULT_INPUT_METHOD
)
if (inputMethodInfo.id.equals(inputMethodId)) {
if (inputMethodInfo.serviceInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
return true
}
break
}
}
return false
}
But I am getting this error, can someone suggest me what's going wrong in my above code
com.example.android.KeyboardHelper.isUsingCustomKeyboard (KeyboardHelper.kt:15)
com.example.android.trackKeyboardType (SignInActivity.kt:75)
com.example.android.onCreate (SignInActivity.kt:71)
android.app.Activity.performCreate (Activity.java:8207)
android.app.Activity.performCreate (Activity.java:8191)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1309)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3808)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4011)
android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:85)
android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2325)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:246)
android.app.ActivityThread.main (ActivityThread.java:8633)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
CodePudding user response:
for (i in 0..inputMethodProperties.size) {
size gives number of objects in list but index starts at 0 so when you try read the last its out of bounds. size -1 is the last index.