0

I'm fooling around with Android and trying to make a simple application to split a bill between friends.

I'm wondering if there is a way to create buttons and textfields based on user input. For example, if the user says he needs to split the bill 5 ways, how can I generate 5 buttons and 5 textfields? Similarly, splitting 10 ways will create 10 buttons and 10 text fields.

Thanks, Ben

Edit: I should mention I have implemented the input already. I simply need to use the input to generate the buttons and textfields.

Ben
  • 9
  • 1

2 Answers2

0

What you need is to create a Linear layout that have only a button and textfield then you can dynamically add that layout into your activity main's layout programatically. You can follow this thread on how to add view dynamically to another view

Community
  • 1
  • 1
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

I've had a similar problem. You need to make a custom layout and then use LayoutInflater to "put it into another layout".You can use the return of inflater.inflate() to get the parent layout. then you just use parent.getChildAt(i) in a for loop for all "fields" and getChildAt(whatever the index of the button or text field you want is) to get the buttons and access them programatically. You can also give every Button a unique ID with setId(generateViewId(yourButton)) to access them from other functions.