Change LinearLayout height to 0 programmatically - android

I am resizign LinearLayout from it's original height to 0 with:
ViewGroup.LayoutParams params = getLayoutParams();
params.height = newHeight;
requestLayout();
Everything works except newHeight = 0 - layout's height changes back to its original height. How can I avoid it ?
Setting visibility to GONE if newHeight == 0 does not help.

Try this.....
LinearLayout layout = (LinearLayout)findViewById(R.id.yourLayoutId);
LinearLayout.LayoutParams lp = (LayoutParams) layout.getLayoutParams();
lp.height = 0;

layout = (LinearLayout)findViewById(R.id.linearTop);
layout.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
layout.setLayoutParams(params);

Wouldn't you have to set the height of params using setBaseAttributes()? The int height = ... will not have any influence on params...

Are you sure that you get the actual parameters object, not the copy of one?
I would try the following code:
ViewGroup.LayoutParams params = getLayoutParams();
params.height = newHeight;
setLayoutParams(params);
requestLayout();

Try setting a new layout params object then. This should work
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,0);
layout.setLayoutParams(lp);

Related

Changing WRAP_CONTENT to a set number using LinearLayout.LayoutParams

I have set up some LinearLayout.LayoutParams to set up some buttons just using java. But I want to change it from WRAP_CONTENT to a height and width of my choice.
//LinearLayout.LayoutParams top;
//this is declared at the top as it is used by different bits of the code.
top = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
top.topMargin = 100;
top.leftMargin = 120;
top = new LinearLayout.LayoutParams
(90, 80);
// whatever u like
this is from documentation:
public LayoutParams(int width, int height) {
super(width, height);
weight = 0;
}
According to ViewGroup.MarginLayoutParams.html#setMargins you have to use this:
LinearLayout layout = (LinearLayout)findViewById(R.id.yourLinear_Layout);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(120, 100, 0, 0); //Here your custom margin (int left, int top, int right, int bottom)
layout.setLayoutParams(params);
Have you tried?
top.getLayoutParams().height = 256;
top.getLayoutParams().width= 256;
top.requestLayout();
try like this..
LinearLayout layout = (LinearLayout)findViewById(R.id.numberPadLayout);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 100;
params.width = 100;
layout.setLayoutParams(params);

I programmatically created a TextView in TableLayout, it automatically sets the height and width

I programmatically created a TextView in TableLayout, it automatically sets the height and width.
Can I change the height and width of the TextView manually inside the TableLayout?
Use
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = HEIGHT;
params.width = WIDTH;
textView.setLayoutParams(params);
same for other parameters like margin, padding etc
Use
LayoutParams lParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
lParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
You can set the height and width programmatically using the following code.
TableRow.LayoutParams param = new TableRow.LayoutParams();
paramColumn1.height = TableRow.LayoutParams.WRAP_CONTENT;
paramColumn1.width = TableRow.LayoutParams.WRAP_CONTENT;
textView1.setLayoutParams(param);
You can also set other properties using this way.

Setting LayoutParams programmatically

I want to set my LinearLayout's parameters after all of the views' parameters are calculated.
I am trying to set LinearLayout's height related to the parent view's height. And parent view's height is calculated according to its layout_weight, so I get the height by getMeasuredHeight();
My code :
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
RelativeLayout lay = (RelativeLayout) findViewById(R.id.alt);
LinearLayout bar = (LinearLayout) findViewById(R.id.barLay);
int layH = lay.getMeasuredHeight();
int barH = layH / 4;
LinearLayout.LayoutParams params = (LayoutParams) bar.getLayoutParams();
params.height = barH;
bar.setLayoutParams(params);
}
Eclipse Debugger stops the program at this line;
LinearLayout.LayoutParams params = (LayoutParams) bar.getLayoutParams();
I can't find what could be the problem? Am I doing something wrong to set params?
The type of LayoutParams are actually set by the parent of the view, so bar.getLayoutParams() should be returning an object of type RelativeLayout.LayoutParams. With an explicit cast to LinearLayout.LayoutParams I'd expect your logcat to contain a line like this:
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
Changing it to this should do the trick:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) bar.getLayoutParams();
or, since height is in the base class, don't cast it at all:
ViewGroup.LayoutParams params = bar.getLayoutParams();
Try changing this
LinearLayout.LayoutParams params = (LayoutParams) bar.getLayoutParams();
to this
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) bar.getLayoutParams();
After #LocHa asked me the type of the layouts, I try changing the parent layout's type to LinearLayout which was RelativeLayout, and finally it worked for me.

How to change LayoutParams on change view size

I have ImageView. I change margins and size of ImageVew on set new bitmap. For example, user click button, I set bitmap to ImageView and change params (this is code from my own view):
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
p.leftMargin = (int) ((pw - lw) / 2f);
_topMargin = (int) ((ph - lh) / 2f);
lp.topMargin = _topMargin;
lp.width = lw;
lp.height = lh;
requestLayout();
All work. But also I want change params, if change size parent view of my ImageView. If I try use code on change params from this event - nothing todo (ImageView don't change params).
How I can fix this issue?
I see now one way: on change parent size, I don't change ImageView params, only set some internal boolean flag. After finish parent changing, I change my ImageView params. But I don't know, what parent event I need watch, after which I can change my ImageView params.
My solution.
I use this method.
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
lp.leftMargin = (int) ((pw - lw) / 2f);
_topMargin = (int) ((ph - lh) / 2f);
lp.topMargin = _topMargin;
lp.width = lw;
lp.height = lh;
setLayoutParams(lp);//<---set layout params
requestLayout();
After changing layout params you need to set it back.

how to change size of button dynamic in android

I try setWidth() and setheight() method but it not work
Try using LayoutParams. For example
button.setLayoutParams (new LayoutParams(50, LayoutParams.WRAP_CONTENT)
For me works perfect this:
ViewGroup.LayoutParams params = myButton.getLayoutParams();
//Button new width
params.width = 400;
myButton.setLayoutParams(params);
I found this to be the best solution because it also allows the button to be repositioned at the same time by simply changing its margin:
int buttonWidth = (int) (screenWidth / 3.0);
RelativeLayout.LayoutParams params = (LayoutParams) myButton.getLayoutParams();
params.width = buttonWidth;
params.leftMargin = buttonWidth;
myButton.setLayoutParams(params);

Categories

Resources