Adding buttons dynamically

Last Updated:

  1. wige

    wige Well-Known Member This Topic's Starter

    Oct 28, 2009
    Likes Received:
    Hi everyone. I am creating an Android app that will act as a front end for viewing reports. I have created an Activity that requests a list of available reports from the server. What I am trying to do is get that activity to add a button for each feed listed in the list of reports (contained in a Vector). This is the code I have so far:

    Code (Text):
    1. // Check for an error
    2.             if (feedHandler.success()) {
    3.                 // Get the data from the handler
    4.                 infoView.setText("Document Loaded. Please select a report.");
    5.                 infoView.append("\n");
    6.                 Vector<String[]> reportList = feedHandler.getReports();
    8.                 // Cycle through the list of reports, and output the results
    9.                 for (Enumeration<String[]> e = reportList.elements(); e.hasMoreElements();) {
    10.                     String[] reportInfo = (String[]) e.nextElement();
    11.                     this.layout.addView(new Button(this));
    12.                 }
    14.             } else {
    15.                 infoView.setText(feedHandler.getError());
    16.             }
    this.layout.addview is where I am trying to add the new buttons. The information for the buttons is stored in reportList, a string array containing the label for the button, and the id code for the report that needs to be handled by the click handler.

    I have been able to find ways to add finite buttons through XML and hard coding, but not arbitrary buttons. Any advice or suggestions would be greatly appreciated.

  2. Jasp182

    Jasp182 Member

    Jan 9, 2010
    Likes Received:
    Sounds like you need to add text and a click handler (if I'm understanding you correctly). So, instead of "newing" up the Button inline in the call to layout.addView, instantiate the button and call setText([buton label]) and setOnClickListener(new OnClickListener() {});

    Something like this inside your loop:

    2.         Button btn = new Button(this);
    3.         btn.setText("label");
    4.         btn.setOnClickListener(new OnClickListener() {
    6.             @Override
    7.             public void onClick(View v) {
    8.                 // TODO Auto-generated method stub
    10.             }
    11.         });
    12.         layout.addView(btn);
  3. wige

    wige Well-Known Member This Topic's Starter

    Oct 28, 2009
    Likes Received:
    Thanks Jasp, I tried this this morning. Unfortunately, the new OnClickListener can only take static variables, so for example, I can't pass a string to be used by the onClick method. I did finally come up with a solution, but I think it is a bit hackish.

    The first thing I do is have my activity extend OnClickListener and change btn.setOnClickListener to (this) instead of creating a new listener. I then add each Button I create to a Vector after adding it to the layout. I also made both the Button Vector and the String[] Vector global.

    Now, when a button is clicked, the onClick method searches the Button vector for the correct button and grabs the element ID. I then retrieve the data from the String[] vector to create the Intent and load the next Activity. So far, it seems to work, but it just feels to me like I am cheating a bit.

Share This Page