Skip to content

Instantly share code, notes, and snippets.

View MasoudFallahpour's full-sized avatar

Masoud Fallahpour MasoudFallahpour

View GitHub Profile
@MasoudFallahpour
MasoudFallahpour / SpaceItemDecoration.kt
Last active September 19, 2020 12:14
A RecyclerView ItemDecoration that puts a uniform space between items. Supports both LinearLayoutManger (horizontal and vertical) and GridLayoutManager
import android.content.Context
import android.graphics.Rect
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class SpaceItemDecoration() : RecyclerView.ItemDecoration() {
enum class Orientation {
HORIZONTAL,
@MasoudFallahpour
MasoudFallahpour / styles.xml
Last active July 15, 2020 05:16
Defining a shape appearance style in styles.xml
<resources>
...
<style name="ShapeAppearance.Button.CutCorners" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerFamily">cut</item>
<item name="cornerSize">8dp</item>
</style>
</resources>
@MasoudFallahpour
MasoudFallahpour / layout.xml
Created July 15, 2020 05:23
Using shapeAppearance attribute to set a custom shape for a button
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@MasoudFallahpour
MasoudFallahpour / BottomNavigationViewCustomShape.kt
Created July 15, 2020 06:22
Setting a custom shape for BottomNavigationView
val radius = resources.getDimension(R.dimen.radius_small)
val bottomNavigationViewBackground = bottomNavigationView.background as MaterialShapeDrawable
bottomNavigationViewBackground.shapeAppearanceModel =
bottomNavigationViewBackground.shapeAppearanceModel.toBuilder()
.setTopRightCorner(CornerFamily.ROUNDED, radius)
.setTopLeftCorner(CornerFamily.ROUNDED, radius)
.build()
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View.OnTouchListener
import android.view.inputmethod.EditorInfo
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.widget.doAfterTextChanged
class SearchEditText
@JvmOverloads constructor(
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
private const val DRAWABLE_LEFT_INDEX = 0
private const val DRAWABLE_TOP_INDEX = 1
private const val DRAWABLE_RIGHT_INDEX = 2
private const val DRAWABLE_BOTTOM_INDEX = 3
fun TextView.setLeftDrawable(@DrawableRes drawableResId: Int) {
class SearchEditText
@JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyle: Int = androidx.appcompat.R.attr.editTextStyle
) : AppCompatEditText(context, attributeSet, defStyle)
private fun setTextChangeListener() {
doAfterTextChanged {
if (it.isNullOrBlank()) {
setRightDrawable(0)
} else {
setRightDrawable(android.R.drawable.ic_menu_close_clear_cancel)
}
queryTextListener?.onQueryTextChange(it.toString())
}
}
private fun setOnEditorActionListener() {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
queryTextListener?.onQueryTextSubmit(text.toString())
true
} else {
false
}
}
}
private fun setDrawablesListener() {
setOnTouchListener(OnTouchListener { view, event ->
view.performClick()
if (event.action == MotionEvent.ACTION_UP) {
when {
rightDrawableClicked(event) -> {
setText("")
return@OnTouchListener true
}
leftDrawableClicked(event) -> {