1

so the question is, how can i make a buttonGenerator in android studio (with a unique Text, unique ID and unique name [for exanple unsig a for loop, to pass their names]). I have been tested some coding, but I've got always some errors (for example: where on the layer should the created Button appear, ...) and every time that the user clicks on the ButtonGenerator, the created button should appear underneath the last created button ( for example in a LinearLayout). I've tried it with visible and invisible, but i assume it is not the right way to do it. I have already a Button and everytime that the user clicks on that button, another Button will be created.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //buttons
    btnCreate= (ImageButton)findViewById(R.id.btnCreateID);
    btnDelete= (ImageButton)findViewById(R.id.btnDeleteID);
    btnCheck=(ImageButton)findViewById(R.id.btnCheckID);
    getTime = (ImageButton) findViewById(R.id.getTimeID);


    //Textviews
    txt= (TextView)findViewById(R.id.txtID);
    timeText= (TextView)findViewById(R.id.timeTextID);

    //image
    numImage = (ImageView)findViewById(R.id.numImageID);

    //Edittexts
    getTxt= (EditText)findViewById(R.id.getTxtID);

    //change visibility
    btnDelete.setVisibility(View.INVISIBLE);
    getTime.setVisibility(View.INVISIBLE);
    btnCheck.setVisibility(View.INVISIBLE);
    txt.setVisibility(View.INVISIBLE);
    timeText.setVisibility(View.INVISIBLE);
    getTxt.setVisibility(View.INVISIBLE);
    numImage.setVisibility(View.INVISIBLE);



    btnCreateClick();
}

//hit Add
public void btnCreateClick() {

    btnCreate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            getTxt.setVisibility(View.VISIBLE);
            getTime.setVisibility(View.VISIBLE);
            btnCheck.setVisibility(View.VISIBLE);

            btnCreate.setVisibility(View.INVISIBLE);

            onGetTimeClick();
            btnCheckClick();
            btnDeleteClick();

        }
    });
}
CodeMiner
  • 11
  • 5

3 Answers3

0

You can create a Button and add it to a Layout like the following:

Button myButton = new Button(this);
myButton.setText(name);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
ll.addView(myButton);

If the layout is a vertical layout the new buttons will be added underneath the existing Buttons.

J.Doe
  • 82
  • 1
  • 10
0

You can create a Button and add it to a Layout like the following:

Button myButton = new Button(this);
myButton.setText(name);
myButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
myButton.setId(generateButtonId());
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
ll.addView(myButton);

If the layout is a vertical layout the new buttons will be added underneath the existing Buttons.

These are functions for generating unique Id

private int generateButtonId(){
    int id = getRandomNumber();
   try {
       String name = getResources().getResourceName(id);
   }catch (Resources.NotFoundException ex) {
       return id;
   }
    return generateButtonId();

}

private int getRandomNumber() {
    int min = 10;
    int max = 3000;

    Random r = new Random();
    int id = r.nextInt(max - min + 1) + min;
    return id;
}

Below are references I utilized

Checking to see if an ID exists in resources (R.id.something)

How can I generate random number in specific range in Android?

0

So, this worked for me andd thanks 4 ur answers:

Button btn;
private Button[] newBTN;
LinearLayout linearLayout;
static int i=1;
static LinearLayout.LayoutParams params;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn= (Button)findViewById(R.id.addBTN);
    btn.setText("Add");
    linearLayout = (LinearLayout) findViewById(R.id.linear);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);



    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            newBTN = new Button[99];
            newBTN[i] = new Button(MainActivity.this);

            //params.addRule(LinearLayout.BELOW, R.id.i);


            newBTN[i].setLayoutParams(params);
            newBTN[i].setText("Button "+i);
            newBTN[i].setId(i);

            //add button to the layout
            linearLayout.addView(newBTN[i]);


            Toast.makeText(MainActivity.this, "Button"+ +i+"/ ID "+String.valueOf(newBTN[i].getId()), Toast.LENGTH_SHORT).show();
            i++;
        }
    });

}
CodeMiner
  • 11
  • 5