-1

I'm here because I want to be able to add buttons when some variable in my code change its value, and the only way I know to create a button is using a XML file, which is something I made previously when I know exactly all variables of the button. But I want similar buttons that do similar things but are not quite the same, and to bind variables of the button to each entry of a database. So, the question is: how can I create a button without a XML file?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    have you tried to google it? – PKlumpp Jan 29 '18 at 14:33
  • yes, and found some answers, sorry, new to stack overflow, it is my first question here, and I see that I am making a lot of mistakes, but... well, I'll try some of what I found, refine my question if needed and ask again. Thanks for your time. – Guilherme Renoldi Jan 29 '18 at 14:36

3 Answers3

0

U can use

Button btn = new Button();

and then u can use their attributes to set text and value or perform operation.

0

Try with something like this:

//the layout in which you want to add the button
LinearLayout layout = (LinearLayout) findViewById(R.id.your_lin_layout);

//create the button
Button btn = new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setText("Send");
btn.setId(the_id_with_which_you_will_access_your_button);

//add the button to your layout
layout.addView(btn);

Or try with some of these answers:

Android: programmatically adding buttons to a layout

How do I programmatically add buttons into layout one by one in several lines?

Add button to a layout programmatically

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
0

you can do it like following:

// create button dynamically
Button btn = new Button(this);
btn.setText("New Btn");

// find linerar layout from your view
LinearLayout ll = (LinearLayout)findViewById(R.id.btn_layout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

// add button in your layout
ll.addView(btn , lp);
Aj 27
  • 2,316
  • 21
  • 29