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));
Related
I am adding some View to the LinearLayout dynamically:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(100, 100, 100, 100);
view.setLayoutParams(params);
parent.addView(view);
However, the margins are not getting applied. The following:
view.invalidate();
view.requestLayout();
parent.invalidate();
parent.requestLayout();
didn't work. However, if I force the activity to recreate (e.g. turn off and on my mobile phone), the margins get applied. Calling activity.recreate() also works, but it's too slow.
How to force layout to recalculate margins? Probably, there's something wrong in the flow? I tried to add my views to the root after creation, add children before and after applying properties, but this didn't work for me.
UPD:
I tried to repeat the bug programmatically and got it with this code:
LinearLayout base = new LinearLayout(context);
LayoutParams params1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
base.setLayoutParams(params1);
base.setBackgroundColor(Color.CYAN);
root.addView(base);
LinearLayout another1 = new LinearLayout(context);
LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
another1.setLayoutParams(params2);
another1.setOrientation(LinearLayout.VERTICAL);
another1.setBackgroundColor(Color.BLUE);
base.addView(another1);
TextView tv1 = new TextView(context);
tv1.setText("SOME TEST TEXT 1");
tv1.setTextColor(Color.BLACK);
LayoutParams params4 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params4.setMargins(100, 100, 100, 100);
tv1.setLayoutParams(params4);
another1.addView(tv1);
TextView tv2 = new TextView(context);
tv2.setText("SOME TEST TEXT 2");
tv2.setTextColor(Color.BLACK);
LayoutParams params5 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params4.setMargins(100, 100, 100, 100);
tv2.setLayoutParams(params5);
another1.addView(tv2);
I expect margins to be applied, but they are not. What is the correct order of initializing such a view?
So, my trouble was in DP to PX converter that just wasn't initialized at that moment. However, to clarify everything, my example above is just an error (I didn't change params4 to params5 when adding the second TextView.
Also while testing everything, I found out that the order of adding layouts and their parameters doesn't matter at all.
You probably added child views to the parent view in onCreate().
In onCreate(), parent view does not have actual size.
If you have to do it in onCreate(), try following code.
final ViewTreeObserver observer = parent.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(100, 100, 100, 100);
TextView view = new TextView(context);
view.setLayoutParams(params);
parent.addView(view);
observer.removeOnGlobalLayoutListener(this);
}
};
Hope this will help!
Try this:
LinearLayout ll = findViewById(R.id.linearLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30,20,30,0);
Hope this will help!
I'm having a linear layout in which I want to insert a checkbox on left and a button on its right.
Also, it should be in for loop so as to create many number of such combination in that single linear layout.
final LinearLayout LayoutRight = (LinearLayout) findViewById(R.id.linearlayoutbars_register);
LayoutRight.setOrientation(LinearLayout.VERTICAL);
LayoutRight.setWeightSum(1f);
final LinearLayout.LayoutParams pR1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
pR1.weight = 0.3f;
final LinearLayout.LayoutParams pR2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
pR2.weight = 0.7f;
for (int i = l; i <= lastCounter; i++) {
tick = new CheckBox(RegisterActivity.this);
tick.setId(10000 + l);
tick.setOnClickListener(tick_Click);
LayoutRight.addView(tick, pR1);
pdR = new Button(RegisterActivity.this);
pdR.setText(l + ". ");
pdR.setId(l);
pdR.setWidth(800);
pdR.setTextSize(17);
pdR.setGravity(Gravity.START);
pdR.setOnClickListener(pdRclass);
LayoutRight.addView(pdR, pR2);
l++;
}
I'm setting 1f as the setWeightSum; 0.3f and 0.7f and childs weight.
With this code I'm getting output as this
But I want my output exactly like this.
You need to set Layout orientation Like this LayoutRight.setOrientation(LinearLayout.HORIZONTAL);
not VERTICAL
LayoutRight.setOrientation(LinearLayout.HORIZONTAL)?
I'm not cool enough to comment so...
Android weight doc says the effected attribute needs to be set to 0dp so try this.
final LinearLayout.LayoutParams pR1 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT);
pR1.weight = 0.3f;
final LinearLayout.LayoutParams pR2 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT);
pR2.weight = 0.7f;
don't forget to keep your orientation horizontal!
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...
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.
I want that button(child) will have 25% of full width of horizontal LinearLayout (its parent). The code:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setWeightSum (1.0f);
Button b = new Button (this);
ll.addView(b);
How can I do it? Thanks.
Try this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENTS,LayoutParams.FILL_PARENT));
lp.weight = 0.25;
b.setLayoutParams(lp);
You can pass it in as part of the LinearLayout.LayoutParams constructor:
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);