LinearLayout won't fill the width of the screen - android

I've been working through a bunch of examples on StackOverflow to create a dynamic layout.
I've made some headway but have now stalled on a particular bug.
How do I make this code fill the width of the parent?
LinearLayout masterLayout = (LinearLayout) findViewById(R.id.button_views);
for(int i=0; i<(launchers.length/3+1); i++){
layout[i] = new LinearLayout(getBaseContext());
layout[i].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout[i].setWeightSum(3);
for(int i1=0; i1<(3); i1++){
launchers[i1] = new Button(this);
launchers[i1].setText(titles[i1]);
launchers[i1].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
launchers[i1].setOnClickListener(this);
layout[i].addView(launchers[i1]);
}
masterLayout.addView(layout[i]);
The code works MOSTLY as desired, the part i'm missing is the buttons being an even width and being spaced evenly across the full length of the screen.

Figured out that LinearLayout.LayoutParams has an option for weight at the end of it. Here is the final code for a dynamic button layout system that fits the width of the screen.
LinearLayout masterLayout = (LinearLayout) findViewById(R.id.button_views);
for(int i=0; i<(launchers.length/3+1); i++){
layout[i] = new LinearLayout(getBaseContext());
layout[i].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout[i].setWeightSum(3);
for(int i1=0; i1<(3); i1++){
launchers[i1] = new Button(this);
launchers[i1].setText(titles[i1]);
launchers[i1].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
launchers[i1].setOnClickListener(this);
layout[i].addView(launchers[i1]);
}
masterLayout.addView(layout[i]);
What would help would be a way to allow the buttons to always be square if anyone wants to have a go at that...

Related

Not able to add margins on buttons through code

Here is my code, I am trying to add few buttons in a row. Its working fine, But these buttons appear too close to each other, I want to apply horizontal margins to buttons. But not able to add. To achieve this I tried to keep button inside a Linearlayout and applied margins to it. But it somehow don't show any buttons, And when I comment out this line- ll.setLayoutParams(lp); Buttons can be seen again, But without any margin. Please let me know how can I make buttons at some distance from each other.
maintable = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
for (int z = 0; z < 3; z++) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(5, 5, 5, 5);
LinearLayout ll = new LinearLayout(this);
//ll.setLayoutParams(lp);
Button b = new Button(savedLists.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
params.setMargins(0, 20, 0, 20);
b.setBackgroundResource(R.drawable.circular);
b.setText(Integer.toString(c));
b.setPadding(10,10,10,10);
ll.addView(b,params);
tr.addView(ll);
c++;
}
maintable.addView(tr);
R.drawable.circular is just creating a simple circular button. Please let me know, if I should post that too.
You haven't set any margin for the button here. You are setting it's padding! Here is how you can set the margin for a button!
Button b = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
params.setMargins(top, left, bottom, right);
yourLinearLayout.addView(b,params);

Change button width and height through code

I am trying to change the width and height of Buttons I dynamically create but my code below is not working. The Buttons stays the same default size. Anyone know why this is not working?
I believe it might have to do with this line:
myButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
The problem may be that I am not supposed to create new LinearLayout params each time but rather add it to an existing LayoutParam?
What do you guys think?
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++){
Button myButton = new Button(view.getContext());
myButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
myButton.setWidth(100);
myButton.setHeight(100);
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
}
Change this code :
myButton.setWidth(100);
myButton.setHeight(100);
To this :
myButton.getLayoutParams().width = 100;
myButton.getLayoutParams().height = 100;
And yes, in your case you can replace :
myButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
To :
myButton.setLayoutParams(rlp);
You can use other constructor of the LinearLayout.LayoutParams, which receiving width and height:
myButton.setLayoutParams(new LinearLayout.LayoutParams(100, 100));

Programmatically LinearLayout Weight

I'm trying to develop similar to grid but using LinearLayout. I would like to have 3 images and exact bottom text after images in single row.
What I have tried:
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
ImageView icon = new ImageView(context);
Item item = getItem(page, index);
Log.e("tag", item.getDrawable());
imageLoader.displayImage(item.getDrawable(), icon, R.drawable.ic_launcher);
icon.setPadding(15, 15, 15, 15);
layout.addView(icon);
TextView label = new TextView(context);
label.setTag("text");
label.setText(item.getName());
label.setTextColor(Color.BLACK);
label.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
label.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.setWeightSum(3f);
layout.addView(label);
I've a view method which returns Viewso I return return layout; at the end of the method.
Here I 've given weight 3 button this is not working for me. And code show more then 3 images in row with text but would like to have weight 3 images and bottom text .
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
or
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
I would define a TableLayout with as many TableRows as you need. On each of those TableRows, I'd add a LinearLayout with VERTICAL orientation consisting of the two Views you need: ImageView and TextView.
This is the LinearLayout where you should set a weight of 1 (to all of them). You will have to get the screen's width and see whether the new LinearLayout to be added still fits the current row. If not, simply start a new TableRow.
To get the width of the screen you can use this:
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
final WindowManager.LayoutParams params = getWindow().getAttributes();
The screen's width might be accessed via the params.width attribute, just compare it with your LinearLayout's .getWidth() method and keep an incremental account (a float) of the current TableRow's width to help you determine if the current item should be placed in an existing row or a new one.
Programmtically setting Linear Layout weight
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);

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.

Android: Programmatically Setting width of Child View using Percentage

I've read this article:
Setting width of Child View using Percentage
So far it works great if we set the weight and width in XML but I want to do it programmatically. I've a table in my layout (XML) and I am adding rows to it programmatically. In each row, I am adding 2 Text Views. I tried doing like this:
TableLayout tl = (TableLayout)findViewById(R.id.myTable);
for(int i = 0; i < nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
TableRow trDetails = new TableRow(this);
trDetails.setBackgroundColor(Color.BLACK);
TextView tvInfo = new TextView(this);
tvInfo.setTextColor(Color.WHITE);
tvInfo.setGravity(Gravity.LEFT);
tvInfo.setPadding(3, 3, 3, 3);
tvInfo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvInfo.setText("info");
TextView tvDetails = new TextView(this);
tvDetails.setTextColor(Color.WHITE);
tvDetails.setGravity(Gravity.LEFT);
tvDetails.setPadding(3, 3, 3, 3);
tvDetails.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvDetails.setText(np.getNamedItem("d").getNodeValue());
trDetails.addView(tvInfo, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
trDetails.addView(tvDetails, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
tl.addView(trDetails,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
but with no luck, it shows blank screen but if I do it in XML then it shows perfectly. Please help me sorting out this issue where I am doing wrong?
Thanks
You should use TableRow.LayoutParams and not Linearlayout.LayoutParams
trDetails.addView(tvInfo, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));
trDetails.addView(tvDetails, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));

Categories

Resources