Last active
September 8, 2022 13:22
-
-
Save thenishchalraj/a7d0f2ac48104ccf740b9b8fd3f6e0c6 to your computer and use it in GitHub Desktop.
Function to create input fields inside flexbox dynamically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog related to the gist: Click here