Skip to content

Instantly share code, notes, and snippets.

@thenishchalraj
Last active September 8, 2022 13:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thenishchalraj/a7d0f2ac48104ccf740b9b8fd3f6e0c6 to your computer and use it in GitHub Desktop.
Save thenishchalraj/a7d0f2ac48104ccf740b9b8fd3f6e0c6 to your computer and use it in GitHub Desktop.
Function to create input fields inside flexbox dynamically.
private val mTextMap: HashMap<Int, TextMapModel> = HashMap()
private val mTextIdArray: ArrayList<Int> = ArrayList()
private fun createInputFieldsInInputBox(actualText: String) {
val textLength = actualText.length
val flexboxLayout = mBinding.flexboxLayoutInput
flexboxLayout.flexDirection = FlexDirection.ROW
val filterArray = arrayOfNulls<InputFilter>(1)
filterArray[0] = InputFilter.LengthFilter(1)
for (i in actualText.indices) {
val editText = EditText(requireContext())
editText.id = generateRandomIntForViews() // a method in utils
editText.setTextColor(
ContextCompat.getColor(
requireContext(),
R.color.white
)
)
val colorStateList = ColorStateList.valueOf(
ContextCompat.getColor(
requireContext(),
R.color.white
)
)
editText.backgroundTintList = colorStateList
editText.inputType = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
editText.filters = filterArray
val lp = FlexboxLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
editText.layoutParams = lp
setTextWatcher(editText)
mTextMap[editText.id] = TextMapModel(" ", editText)
mTextIdArray.add(i, editText.id)
flexboxLayout.addView(editText)
}
}
@thenishchalraj
Copy link
Author

Blog related to the gist: Click here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment