MadWayne
MadWayne

Reputation: 49

Create dynamically n number of buttons on click and populate

hi every dynamic creation of buttons on click doesn't seem to work.

I have a list of 20 names at present, so I want to generate 20 buttons one for each name as the names in a list of strings I want to populate these twenty buttons with those names.

from these twenty buttons I will click and have a range of sub names, display as buttons again but these will vary depending on the first click.

I want to do this as the name list will expand over time, and rather than doing it from scratch each and every button, makes sense for the system to be told how many buttons and load up the names for each one.

  1. I need a loop and set the number 20 so it generates 20 buttons. id as buttonname1 to button name 20.

  2. populate the buttons generated with the associated text fro buttons 1 to 20.

  3. I need a layout with a button template so the buttons look the same when generated. very simple but none of the examples I have found make sense or work.

if someone can point me into a useful tutorial on this I would be most grateful, in the mean time I will continue searching.

If at first I can generate the 20 buttons with twenty ids it be start.

Thanks

Upvotes: 0

Views: 3357

Answers (3)

MadWayne
MadWayne

Reputation: 49

Okay for the second time today I have sort of got what I need(recovered it), just need to work out what to do next but here is my code.

my layout main .xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
</RelativeLayout>

my activity. based on DYNAMIC BUTTON IN ANDROID found on the web.http://alexanderoorayil.blogspot.co.uk/2012/05/dynamic-button-in-android.html

 package com.example.dynamic_buttons;


 import android.app.Activity;

 import android.graphics.Typeface;

 import android.os.Bundle;

 import android.view.View;

 import android.view.ViewGroup.LayoutParams;

 import android.widget.Button;

 import android.widget.CheckBox;

 import android.widget.CompoundButton;

 import android.widget.CompoundButton.OnCheckedChangeListener;

 import android.widget.LinearLayout;

 import android.widget.ScrollView;

 import android.widget.Toast;


public class MyActivity extends Activity {

Button b;

ScrollView scrollview;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

  //String Manuf;

    scrollview = new ScrollView(this);

    LinearLayout linearlayout = new LinearLayout(this);

    linearlayout.setOrientation(LinearLayout.VERTICAL);

    scrollview.addView(linearlayout);



    for(int i = 0; i<28;i++)

    {

        LinearLayout linear1 = new LinearLayout(this);

        linear1.setOrientation(LinearLayout.HORIZONTAL);

        linearlayout.addView(linear1);

        b = new Button(this);

        int id = getResources().getIdentifier("Manuf"+i, "string", getPackageName());
        String Manuf = getResources().getString(id);

        b.setText(Manuf);

        b.setId(i);

        b.setTextSize(30);

        b.setPadding(0,0,0,0);

       // b.setTypeface(Typeface.SERIF,Typeface.ITALIC);

        b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));



        linear1.addView(b);



       /* CheckBox checkbox = new CheckBox(this);

        checkbox.setId(i);

        checkbox.setText("Wow..");

        linear1.addView(checkbox);



        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {



            @Override

            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), "Checked.."+ arg0.getId(), Toast.LENGTH_SHORT).show();

            }

        });
    */


        b.setOnClickListener(new View.OnClickListener() {



            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), "Yipee.."+ v.getId(), Toast.LENGTH_SHORT).show();

            }

        });

    }

    this.setContentView(scrollview);

}

}

the strings in my strings.xml file start Manfu0 -28.

Couldn't have done it without your help and comments, hopefully I will learn how to use as now I need to create some actions for certain ids of button being pressed.

Thanks

Upvotes: 0

dovicz
dovicz

Reputation: 380

EDIT: What you need is create views dynamically. If you really need a button as your view then add them programmatically.

Button button = new Button(this); // example
  1. I need a loop and set the number 20 so it generates 20 buttons. id as buttonname1 to button name 20.
  2. populate the buttons generated with the associated text fro buttons 1 to 20.
    String[] names = {"John","Jack",...,"Someone"}; // 20 names
    Button[] buttons = new Button[20];
    for (int i = 0; i < 20; i++) {
        Button button = new Button(this);
        button.setId("buttonname"+(i+1));
        button.setText(names[i]);
        buttons[i] = button;
    }

You can then add the buttons from your array to your layout and should look something like this. Just make sure you set the layout orientation to vertical so the buttons will stack on top of each one. You can search it up.

    LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
    for (int i = 0; i < 20; i++) {
        layout.addView(buttons[i]);
    }
  1. I need a layout with a button template so the buttons look the same when generated. very simple but none of the examples I have found make sense or work.

You can refer to this tutorial to set more of the attributes you want. You can search for keywords like "programmatically adding Android views", "creating dynamic views in Android" and such. Use the word programmatically and dynamic. Hope this helps your case.

Upvotes: 1

An SO User
An SO User

Reputation: 25028

  1. I need a loop and set the number 20 so it generates 20 buttons. id as buttonname1 to button name 20.
  2. populate the buttons generated with the associated text fro buttons 1 to 20.

You need to look into using a ListView or a RecyclerView. These views take in arbitrary amounts of data and display them few items at a time. In your case, the items will be buttons with their text being the name you want to display.

Attach a listener to each of these buttons that defines what needs to be done on click. You'll need to look into inflating views and adding them dynamically.

That's all there is to it. There's really good documentation on these and a ton of SO questions.

  1. I need a layout with a button template so the buttons look the same when generated. very simple but none of the examples I have found make sense or work.

You need to read up about styling and theming. When you define the XML of your button, you can style it there. Since you are inflating the same XML again and again, all your buttons will look the same.

Upvotes: 1

Related Questions