+ // Get a handle for the input method manager.
+ inputMethodManager = (getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager)
+
+ // Search for the string on the page whenever a character changes in the search edit text.
+ searchEditText.addTextChangedListener(object : TextWatcher {
+ override fun beforeTextChanged(charSequence: CharSequence, start: Int, count: Int, after: Int) {
+ // Do nothing.
+ }
+
+ override fun onTextChanged(charSequence: CharSequence, start: Int, before: Int, count: Int) {
+ // Do nothing.
+ }
+
+ override fun afterTextChanged(editable: Editable) {
+ // Search for the text in the WebView.
+ logcatWebView.findAllAsync(searchEditText.text.toString())
+ }
+ })
+
+ // Set the `check mark` button for the search edit text keyboard to close the soft keyboard.
+ searchEditText.setOnKeyListener { _: View?, keyCode: Int, keyEvent: KeyEvent ->
+ if ((keyEvent.action == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // The `enter` key was pressed.
+ // Search for the text in the WebView.
+ logcatWebView.findAllAsync(searchEditText.text.toString())
+
+ // Hide the soft keyboard.
+ inputMethodManager.hideSoftInputFromWindow(logcatWebView.windowToken, 0)
+
+ // Consume the event.
+ return@setOnKeyListener true
+ } else { // A different key was pressed.
+ // Do not consume the event.
+ return@setOnKeyListener false
+ }
+ }
+
+ // Update the find on page count.
+ logcatWebView.setFindListener { activeMatchOrdinal, numberOfMatches, isDoneCounting ->
+ if (isDoneCounting && (numberOfMatches == 0)) { // There are no matches.
+ // Set the search count text view to be `0/0`.
+ searchCountTextView.setText(R.string.zero_of_zero)
+ } else if (isDoneCounting) { // There are matches.
+ // The active match ordinal is zero-based.
+ val activeMatch = activeMatchOrdinal + 1
+
+ // Build the match string.
+ val matchString = "$activeMatch/$numberOfMatches"
+
+ // Update the search count text view.
+ searchCountTextView.text = matchString
+ }
+ }
+