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")
            }

        }

1 Answer 1

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")
            }

        }
5
  • if we type in one textfield , value of all text fields will be changed. How to fix that. Commented Dec 15, 2021 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 Commented Aug 20, 2022 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<T>(), where T is the data type. Then, simple do a forEach loopover and have the Texts rendered. Commented Aug 21, 2022 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. Commented Aug 21, 2022 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. Commented Aug 23, 2022 at 13:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.