Make SearchView clearable

pull/121/head
Koitharu 4 years ago
parent 0f2f1b439e
commit 659ff9d55a
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -1,14 +1,19 @@
package org.koitharu.kotatsu.search.ui.widget package org.koitharu.kotatsu.search.ui.widget
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.KeyEvent import android.view.KeyEvent
import android.view.MotionEvent
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import androidx.annotation.AttrRes import androidx.annotation.AttrRes
import androidx.appcompat.widget.AppCompatEditText import androidx.appcompat.widget.AppCompatEditText
import androidx.core.content.ContextCompat
import com.google.android.material.R import com.google.android.material.R
import org.koitharu.kotatsu.search.ui.suggestion.SearchSuggestionListener import org.koitharu.kotatsu.search.ui.suggestion.SearchSuggestionListener
private const val DRAWABLE_END = 2
class SearchEditText @JvmOverloads constructor( class SearchEditText @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
@ -16,6 +21,7 @@ class SearchEditText @JvmOverloads constructor(
) : AppCompatEditText(context, attrs, defStyleAttr) { ) : AppCompatEditText(context, attrs, defStyleAttr) {
var searchSuggestionListener: SearchSuggestionListener? = null var searchSuggestionListener: SearchSuggestionListener? = null
private val clearIcon = ContextCompat.getDrawable(context, R.drawable.abc_ic_clear_material)
var query: String var query: String
get() = text?.trim()?.toString().orEmpty() get() = text?.trim()?.toString().orEmpty()
@ -50,9 +56,32 @@ class SearchEditText @JvmOverloads constructor(
lengthAfter: Int, lengthAfter: Int,
) { ) {
super.onTextChanged(text, start, lengthBefore, lengthAfter) super.onTextChanged(text, start, lengthBefore, lengthAfter)
setCompoundDrawablesRelativeWithIntrinsicBounds(
null,
null,
if (text.isNullOrEmpty()) null else clearIcon,
null,
)
searchSuggestionListener?.onQueryChanged(query) searchSuggestionListener?.onQueryChanged(query)
} }
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_UP) {
val drawable = compoundDrawablesRelative[DRAWABLE_END] ?: return super.onTouchEvent(event)
val isOnDrawable = drawable.isVisible && if (layoutDirection == LAYOUT_DIRECTION_RTL) {
event.x.toInt() in paddingLeft..(drawable.bounds.width() + paddingLeft)
} else {
event.x.toInt() in (width - drawable.bounds.width() - paddingRight)..(width - paddingRight)
}
if (isOnDrawable) {
text?.clear()
return true
}
}
return super.onTouchEvent(event)
}
override fun clearFocus() { override fun clearFocus() {
super.clearFocus() super.clearFocus()
text?.clear() text?.clear()

@ -58,7 +58,8 @@
android:imeOptions="actionSearch" android:imeOptions="actionSearch"
android:importantForAutofill="no" android:importantForAutofill="no"
android:paddingBottom="1dp" android:paddingBottom="1dp"
android:singleLine="true" /> android:singleLine="true"
tools:drawableEnd="@drawable/ic_clear" />
</com.google.android.material.appbar.MaterialToolbar> </com.google.android.material.appbar.MaterialToolbar>

Loading…
Cancel
Save