Advertisement
Guest User

Compose Amount TextField

a guest
Nov 5th, 2022
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.52 KB | None | 0 0
  1.     var amount by remember { mutableStateOf("") }
  2.     AmountTextField(
  3.         amount = amount,
  4.         onAmountChange = { amount = it }
  5.     )
  6.  
  7. @Composable
  8. fun AmountTextField(
  9.     amount: String,
  10.     onAmountChange: (String) -> Unit
  11. ) {
  12.     Row(
  13.         horizontalArrangement = Arrangement.SpaceAround,
  14.         verticalAlignment = Alignment.CenterVertically
  15.     ) {
  16.         Text(
  17.             text = stringResource(R.string.rupee_symbol),
  18.             style = MaterialTheme.typography.h2.copy(
  19.                 color = Color(0xFF191919),
  20.                 fontWeight = FontWeight.ExtraBold
  21.             )
  22.         )
  23.         val titleField = buildAmountEditText(
  24.             context = LocalContext.current,
  25.             amount = amount,
  26.             onAmountChange = onAmountChange,
  27.             placeholder = "0",
  28.             focusChangeListener = { _, hasFocus ->
  29.                 // do something with focus
  30.             },
  31.             paddingValues = PaddingValues(0)
  32.         )
  33.         AndroidView(factory = { titleField })
  34.     }
  35. }
  36.  
  37. @Composable
  38. private fun buildAmountEditText(
  39.     context: Context,
  40.     amount: String = "",
  41.     onAmountChange: (String) -> Unit,
  42.     placeholder: String,
  43.     focusChangeListener: View.OnFocusChangeListener,
  44.     paddingValues: Paddings,
  45.     layoutParams: ViewGroup.LayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
  46. ): EditText {
  47.     return EditText(context).apply {
  48.         tag = "amount"
  49.         this.layoutParams = layoutParams
  50.         setPadding(paddingValues.left, paddingValues.top, paddingValues.right, paddingValues.bottom)
  51.         setBackgroundColor(0xFFFFEAEA.toInt())
  52.         typeface = Typeface.DEFAULT_BOLD
  53.         textSize = with(LocalDensity.current) { 24.sp.roundToPx() }.toFloat()
  54.         letterSpacing = 0f
  55.         gravity = Gravity.START
  56.         background = null
  57.         maxLines = 1
  58.         inputType = EditorInfo.TYPE_CLASS_NUMBER or EditorInfo.TYPE_NUMBER_FLAG_DECIMAL
  59.         imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
  60.         hint = placeholder
  61.         setText(amount)
  62.         onFocusChangeListener = focusChangeListener
  63.         addTextChangedListener { editableText ->
  64.             if (editableText != null) {
  65.                 onAmountChange(editableText.toString())
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. data class Paddings(val left: Int = 0, val top: Int = 0, val right: Int = 0, val bottom: Int = 0)
  72.  
  73. 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