5

I want to dynamically add Text Fields to my layout everytime user click "Add" button.Added Text Field should be added above "Add" button .i.e.between Step 1 TextField and Add Button.How can this be achieved through Jetpack Compose?Below is screenshot followed by my current code..

enter image description here

Code-

Column(modifier = Modifier.padding(16.dp)) {

            OutlinedTextField(
                modifier = Modifier.fillMaxWidth(),
                value = step1,
                onValueChange = {
                    viewModel.onStep1Changed(it)
                },
                label = {
                    Text(text = "Step 1...")
                },
                shape = RoundedCornerShape(8.dp),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color.Transparent),
                trailingIcon = {
                    Icon(
                        modifier = Modifier.padding(start=10.dp),
                        imageVector = Icons.Filled.Image,
                        tint= Color.Blue,
                        contentDescription = "Select Image"
                    )
                }
            )
            Spacer(modifier = Modifier.height(16.dp))
            Button(onClick = {
             //TODO Dynamically add text fields
            }){
                Text("Add")
            }

        }
Android Developer
  • 9,157
  • 18
  • 82
  • 139

1 Answers1

3
Column(modifier = Modifier.padding(16.dp)) {
val textFieldCount by remember { mutableStateOf (1) }
            repeat(textFieldCount) {
                OutlinedTextField(
                modifier = Modifier.fillMaxWidth(),
                value = step1,
                onValueChange = {
                    viewModel.onStep1Changed(it)
                },
                label = {
                    Text(text = "Step 1...")
                },
                shape = RoundedCornerShape(8.dp),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color.Transparent),
                trailingIcon = {
                    Icon(
                        modifier = Modifier.padding(start=10.dp),
                        imageVector = Icons.Filled.Image,
                        tint= Color.Blue,
                        contentDescription = "Select Image"
                    )
                }
            )
            Spacer(modifier = Modifier.height(16.dp))
        }
            Button(onClick = {
             textFieldCount++
            }){
                Text("Add")
            }

        }
Richard Onslow Roper
  • 5,477
  • 2
  • 11
  • 42
  • if we type in one textfield , value of all text fields will be changed. How to fix that. – mrtechmaker Dec 15 '21 at 21:31
  • I have a similar case, but with Row. I add Text() into Row by going through an array. Every time the array changes I need to remove added Text() and start adding again. How Do I achieve that? Is there any removeAllViews() for Row? Thanks – Mark Delphi Aug 20 '22 at 11:57
  • @MarkDelphi If you just need to display stuff from an array, change the array to a mutable state list object. `val list = mutableStateListOf()`, where `T` is the data type. Then, simple do a `forEach` loopover and have the Texts rendered. – Richard Onslow Roper Aug 21 '22 at 20:05
  • 1
    @KaranAhuja define the value inside the loop, it won't change that way. Better still, create a mutable state list as described in the comment above to hold the values for the text, store it in a viewmodel, and use state-hoisting to read it from your text composables. – Richard Onslow Roper Aug 21 '22 at 20:07
  • @RichardOnslowRoper thanks. currently using xml views . need to figure out how to store state in viewmodel and use it in a composable... will look into the docs for next project. – mrtechmaker Aug 23 '22 at 13:03