Adding button dynamically to linear layout with id's? - android

Is there any way to add new buttons dynamically to linear layout with their unique id's. Considering a predefined button to perform this action.

LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button 1");
btnTag.setId(1);
row.addView(btnTag);
layout.addView(row);

Yes,you can dynamically add button to linear layout.
For Eg:
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,AbsListView.LayoutParams.WRAP_CONTENT));
layout.setId(5000);
Random random = new Random();
//Five button
for(int i=0;i<5;i++)
{
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
button.setText("Button");
button.setId(random.nextInt(5)); //generate unique number between 0 to 4 and set it as id
layout.addView(button);
}

Related

Width column tablelayout dynamically

I have a problem regarding not using XML in the TableLayout.
Someone has already asked the same as I do but the answer provided has been unable to help me.
how to set width of column dynamically in android tablelayout?
Right now I have this
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
Button a = new Button(this);
a.setText("Dynamic Button");
a.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
a.setGravity(2);
tr.addView(a);
But I fail to see how changing
tr.setLayoutParams
Will do the job of making for example the first button column 70% and the other button the 30%
You can create an linear layout with weight and keep those two buttons inside linear layout and then set linear to your table row.
Check below code:
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
params = new TableRow.LayoutParams(0,
TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
params.weight = 1;
layout.setLayoutParams(params);
layout.setBackgroundColor(Color.WHITE);
layout.setWeightSum(1);
/* Create a Button to be the row-content. */
LinearLayout.LayoutParams chiledParams = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams.weight = (float) 0.7;
Button b = new Button(this);
b.setText("Button");
b.setLayoutParams(chiledParams);
/* Add Button to row. */
LinearLayout.LayoutParams chiledParams1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams1.weight = (float) 0.3;
Button a = new Button(this);
a.setText("Button");
a.setLayoutParams(chiledParams1);
layout.addView(b);
layout.addView(a);
tr.addView(layout);
tl.addView(tr);
you need setLayoutParams to your widget like following code:
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width,heigh);
b.setLayoutParams(tlp);
TableRow.LayoutParams tlp1 = new TableRow.LayoutParams(width/3,heigh/3);
a.setLayoutParams(tlp1);

How to place table layout below Linear layout programmatically in android

Here is my code to display table layout below linear layout.here i am setting scroll view as contentview.but only table layout displaying on screen.here any problem in layout.please help me thanks.
ScrollView scrollView = new ScrollView(this);
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Button btn1 = new Button(this);
btn1.setText("Button1");
ll.addView(btn1);
btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
Here tried by placing the linear layout above the table layout now only linearlayout displaying
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Set the TextView for the Question
TextView tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setText("HI");
ll.addView(tv1);
ScrollView scrollView = new ScrollView(this);
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
//Here i have table rows code
ll.addView(scrollView);
setContentView(ll);
now setting linear layout as contentview as per your answers scroll view contains only table layout.but why it is not displaying in the screen
ScrollViews in android should contain only a single child. So might I suggest you add a LinearLayout inside the ScrollView and then add the other components in this LinearLayout
check this:\
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Set the TextView for the Question
TextView tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setText("HI");
ll.addView(tv1);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TableLayout resultLayout = new TableLayout(this);
resultLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
scrollView.addView(resultLayout);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
ll.addView(scrollView);

Dynamically align TextView to next line in horizontal LinearLayout

I am adding TextView dynamically using for loop to LinearLayout which orientation is horizontal. Is it possible to align the TextView to second line if it reach end of screen width?
You can do it in the following manner:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
LinearLayout layout1 = new LinearLayout(this);
LinearLayout layout2 = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout1.setOrientation(LinearLayout.HORIZONTAL);
layout2.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 3; i++) {
TextView lblView = new TextView(this);
TextView lblView2 = new TextView(this);
lblView.setLayoutParams(params);
lblView.setText("hello");
lblView.setEms(5);
lblView.setGravity(Gravity.CENTER);
lblView2.setLayoutParams(params);
lblView2.setText("hello");
lblView2.setEms(5);
lblView2.setGravity(Gravity.CENTER);
layout1.addView(lblView);
layout2.addView(lblView2);
}
layout.addView(layout1);
layout.addView(layout2);
setContentView(layout);
The following will do the trick,
textview.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));

How to put buttons like that? [duplicate]

This question already has an answer here:
How can i put my buttons like that?
(1 answer)
Closed 9 years ago.
Hello i want to put my number buttons(1 to 9) like that
but its now like that:
This is my code:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
layout2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView titleView = new TextView(this);
titleView.setText("Table Layout");
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(titleView);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
btnConnect.setLayoutParams(param2);
layout2.addView(btnConnect);
TextView titleViewSpace = new TextView(this);
titleViewSpace.setLayoutParams(param2);
layout2.addView(titleViewSpace);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
btnDisconnect.setLayoutParams(param2);
layout2.addView(btnDisconnect);
layout.addView(layout2);
TableLayout tblLayout = new TableLayout(this);
tblLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));
TableRow tblrow = null;
for (int i = 1; i <= 9; i++) {
if (i % 3 == 1) {
tblrow = new TableRow(this);
tblLayout.addView(tblrow);
}
Button b = new Button(this);
b.setText("" + i);
tblrow.addView(b);
}
TableRow tr = new TableRow(this);
Button btnZero = new Button(this);
btnZero.setText("0");
Button btnHash = new Button(this);
btnHash.setText("#");
Button btnStar = new Button(this);
btnStar.setText("*");
tr.addView(btnZero);
tr.addView(btnHash);
tr.addView(btnStar);
tblLayout.addView(tr);
layout.addView(tblLayout);
setContentView(layout);
In order to have above view, i put buttons this layout
new LayoutParams(0,LayoutParams.WRAP_CONTENT,1)
However, when i put this layout to buttons(1 to 9), buttons in loop disappear. What might be the solution?
You can look up weight sum and layout weight in google. These two properties will make the buttons take up the whole space of the screen.

How to Create multiple buttons in android

I tried the below code to create multiple buttons through programatically but it creates a single button as per my application i need to create a button based on input. For example if the input is 3 means i need to create three buttons in a layout. For your reference i attached the sample image and my code.
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
if in your example, your global container (main_layer) is a relativelayout or a frame layout, you have placed them on top of eachother. So you can't see the one on the back order.
Try this pls,
LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer);
for (int i = 0; i < 10; i++)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
You are creating multiple new linear layouts within the for loop. Linear layout need to be taken out of the for loop and just try adding the buttons in it.
1st run of for loop creates a new linear layout and adds a button
2nd run of for loop creates a new linear layout and adds a button on the top of previous added layout
Here you are creating a new LinearLayout per button... Try something more like...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
}
main_layer.addView(layout);
Try this
LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);
llParent.removeAllViews();
for (int i = 0; i < array_of_btn_input.size(); i++) {
BSView b = new BSView(this, new SimpleThumbBean(i + 1, bsList
.get(i).getLabel(), bsList.get(i).getThumbUrl()));
LinearLayout.LayoutParams llDynamic = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
llDynamic.weight = 1f;
llParent.addView(b, llDynamic);
}
llParent is a defined Layout and make sure you call removeAllViews() before adding layouts dynamically into it.
BSView is my own custom View. You can set BSView into ordinary Button as you need it.

Categories

Resources