• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Adding buttons dynamically

wige

Newbie
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:
// Check for an error
            if (feedHandler.success()) {
                // Get the data from the handler
                infoView.setText("Document Loaded. Please select a report.");
                infoView.append("\n");
                Vector<String[]> reportList = feedHandler.getReports();
                
                // Cycle through the list of reports, and output the results
                for (Enumeration<String[]> e = reportList.elements(); e.hasMoreElements();) {
                    String[] reportInfo = (String[]) e.nextElement();
                    this.layout.addView(new Button(this));
                }
                
            } else {
                infoView.setText(feedHandler.getError());
            }

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.
 
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:

PHP:
        Button btn = new Button(this);
        btn.setText("label");
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
            }
        });
        layout.addView(btn);
 
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.
 
Back
Top Bottom