Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var amount by remember { mutableStateOf("") }
- AmountTextField(
- amount = amount,
- onAmountChange = { amount = it }
- )
- @Composable
- fun AmountTextField(
- amount: String,
- onAmountChange: (String) -> Unit
- ) {
- Row(
- horizontalArrangement = Arrangement.SpaceAround,
- verticalAlignment = Alignment.CenterVertically
- ) {
- Text(
- text = stringResource(R.string.rupee_symbol),
- style = MaterialTheme.typography.h2.copy(
- color = Color(0xFF191919),
- fontWeight = FontWeight.ExtraBold
- )
- )
- val titleField = buildAmountEditText(
- context = LocalContext.current,
- amount = amount,
- onAmountChange = onAmountChange,
- placeholder = "0",
- focusChangeListener = { _, hasFocus ->
- // do something with focus
- },
- paddingValues = PaddingValues(0)
- )
- AndroidView(factory = { titleField })
- }
- }
- @Composable
- private fun buildAmountEditText(
- context: Context,
- amount: String = "",
- onAmountChange: (String) -> Unit,
- placeholder: String,
- focusChangeListener: View.OnFocusChangeListener,
- paddingValues: Paddings,
- layoutParams: ViewGroup.LayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
- ): EditText {
- return EditText(context).apply {
- tag = "amount"
- this.layoutParams = layoutParams
- setPadding(paddingValues.left, paddingValues.top, paddingValues.right, paddingValues.bottom)
- setBackgroundColor(0xFFFFEAEA.toInt())
- typeface = Typeface.DEFAULT_BOLD
- textSize = with(LocalDensity.current) { 24.sp.roundToPx() }.toFloat()
- letterSpacing = 0f
- gravity = Gravity.START
- background = null
- maxLines = 1
- inputType = EditorInfo.TYPE_CLASS_NUMBER or EditorInfo.TYPE_NUMBER_FLAG_DECIMAL
- imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
- hint = placeholder
- setText(amount)
- onFocusChangeListener = focusChangeListener
- addTextChangedListener { editableText ->
- if (editableText != null) {
- onAmountChange(editableText.toString())
- }
- }
- }
- }
- data class Paddings(val left: Int = 0, val top: Int = 0, val right: Int = 0, val bottom: Int = 0)
- fun PaddingValues(left: Int = 0, top: Int = 0, right: Int = 0, bottom: Int = 0) = Paddings(left, top, right, bottom)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement