Skip to content

Instantly share code, notes, and snippets.

@shaon2016
Last active September 24, 2020 11:43
Show Gist options
  • Save shaon2016/3477bc040cfca77a05251bf14b6563dd to your computer and use it in GitHub Desktop.
Save shaon2016/3477bc040cfca77a05251bf14b6563dd to your computer and use it in GitHub Desktop.
Create a spinner dialog using Recycler view and Higher order function
# This is the Util function
````
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.sslwireless.alil.R
import com.sslwireless.alil.databinding.CustomSpinnerDialogBinding
import kotlinx.android.synthetic.main.custom_spinner_dialog.*
object D {
inline fun showSpinnerDialog(context: Context, title : String, rv : (RecyclerView, Dialog) -> Unit ) {
val newDialog = Dialog(context, R.style.customDialog)
newDialog.requestWindowFeature(android.view.Window.FEATURE_NO_TITLE)
newDialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val customDialogBinding: CustomSpinnerDialogBinding = DataBindingUtil.inflate(
LayoutInflater.from(context),
R.layout.custom_spinner_dialog, null, false
)
newDialog.setContentView(customDialogBinding.root)
rv(customDialogBinding.recyclerView2, newDialog)
customDialogBinding.textView92.text = title
customDialogBinding.done.setOnClickListener {
}
customDialogBinding.cancle.setOnClickListener {
newDialog.dismiss()
}
newDialog.show()
newDialog.setCancelable(false)
}
}
````
# Calling from fragment or activity
````
tvPrefix.setOnClickListener {
D.showSpinnerDialog(requireContext(), "Prefixes") { rv, dialog ->
initPrefixRV(rv, dialog)
}
}
````
# Recycler view
````
private fun initPrefixRV(rv: RecyclerView, dialog: Dialog) {
val prefixes = arrayListOf("Mr.", "Mrs.")
rv.layoutManager = LinearLayoutManager(requireContext())
rv.adapter = BaseRecyclerAdapter(requireContext(), object:IAdapterListener{
override fun <T> clickListener(position: Int, model: T, view: View) {
tvPrefix.text = prefixes[position]
dialog.dismiss()
}
override fun getViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
return SpinnerAdapterRecycler(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context)
, R.layout.custom_user_spinner
, parent, false
)
, requireContext(), "application_form_prefix"
)
}
}, prefixes)
}
````
# Spinner Adapter
````
import android.content.Context
import androidx.databinding.ViewDataBinding
import com.sslwireless.alil.databinding.CustomUserSpinnerBinding
import com.sslwireless.alil.view.base.BaseViewHolder
class SpinnerAdapterRecycler(
itemView: ViewDataBinding,
context: Context,
private val type: String
) :
BaseViewHolder(itemView.root) {
var binding = itemView as CustomUserSpinnerBinding
var mContext: Context = context
override fun <T> onBind(
position: Int,
itemModel: T,
listener: IAdapterListener
) {
when (type) {
"application_form_prefix" -> {
itemModel as String
binding.spinnerDataItem.text = itemModel
binding.root.setOnClickListener {
listener.clickListener(position, itemModel, binding.root)
}
}
}
}
}
````
# Custom Spinner xml
````
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="8dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="8dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="6dp" />
<com.sslwireless.alil.util.CustomTextView
android:id="@+id/spinnerDataItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:maxLines="4"
android:text="sadads"
android:padding="6sp"
android:background="?attr/selectableItemBackground"
android:paddingEnd="55dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView22"
app:layout_constraintStart_toStartOf="@+id/guideline7"
app:layout_constraintTop_toTopOf="@+id/guideline9" />
<ImageView
android:id="@+id/imageView22"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginEnd="24dp"
android:visibility="gone"
android:tint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="@+id/spinnerDataItem"
app:layout_constraintEnd_toStartOf="@+id/guideline8"
app:layout_constraintTop_toTopOf="@+id/guideline9"
android:src="@drawable/ic_tick" />
<View
android:id="@+id/view47"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_marginTop="8dp"
android:background="@color/divider_color"
app:layout_constraintEnd_toStartOf="@+id/guideline8"
app:layout_constraintStart_toStartOf="@+id/guideline7"
app:layout_constraintTop_toBottomOf="@+id/spinnerDataItem" />
<androidx.constraintlayout.widget.Group
android:id="@+id/itemSelectGroup"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="spinnerDataItem"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline8"
app:layout_constraintStart_toStartOf="@+id/spinnerDataItem"
app:layout_constraintTop_toTopOf="@+id/guideline9" >
</androidx.constraintlayout.widget.Group>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment