0

I want to add buttons (based on the received info) in the main activity, so the number of buttons is not fixed.

I think the main code should be like:

for (int i=0; i<num; i++){
    addButton(num);    // The function for adding a button in layout and set all the parameters(id, text)

The parameters I want to change everytime and should be custom are:

  • id name
  • text

The following is the basic format I want to use for a single button (in activity_main.xml):

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:background="@drawable/bg"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="2dp"
    android:layout_weight="3"/>

Does anyone know how to do it? Thanks.

hsw8906
  • 31
  • 9
  • Please share more details where u want to add Buttons – ρяσѕρєя K Aug 08 '18 at 05:35
  • What do you mean by "based on the received info" Please provide details of it. Creating buttons in for loop is not the solution and should be avoided – sandhya sasane Aug 08 '18 at 05:37
  • @sandhyasasane It'll get "num", "id name", and "text", so each time they may be different. (different number of buttons, different name of id, and different text of buttons) – hsw8906 Aug 08 '18 at 05:53
  • Tell me what are you going to do with retrieved "num", "id name", and "text". ? Does it coming from database? Or does it gonna to be entered by end user? And then what you wanted to do with it? – sandhya sasane Aug 08 '18 at 06:03
  • @sandhyasasane "num" is the total number of buttons(for the loop I wrote). For each button, when user click it, it gonna connect to another activity. That's why I need the "id". – hsw8906 Aug 08 '18 at 06:20
  • Do you want to add buttons in layout file? Or do you want to open different activities after clicking those buttons...? And Please delete these all threads and edit your question with what exactly you want... Do not use any code... Just edit what you want , which can be understandable by another community... So that they can help – sandhya sasane Aug 08 '18 at 06:28

0 Answers0