how to change size of button dynamic in android - 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);

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.

android RelativeLayout change height programmatically

I try change layout height programmatically. I have two buttons. In one button I change my layout position and second button I try to receive save position witch I was first time.
<RelativeLayout
android:id="#+id/popaplayout"
android:layout_width="fill_parent"
android:layout_height="200dp" >
This is a my layout
and in first button I wrote
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
parms.addRule(RelativeLayout.BELOW, R.id.movies_title_layout);
scrollpopap.setLayoutParams(parms);
scrollpopap.setScrollingEnabled(true);
and second button
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 300);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
In xml file I wrote height 200dp and programmatically 300 and there are different height. How I can wrote save height in programmatically?
if anyone knows solution please help me
thanks
First convert 200 dp into pixels -
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
Then set programatically,
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, pixels);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
Convert DP to PX like this.
final float scale = getContext().getResources().getDisplayMetrics().density;
int px = (int) (100 * scale + 0.5f); // replace 100 with your dimensions
Set Some layout_gravity of relative layout. and Change height and width by java code.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = px;
rl.getLayoutParams().width = px;
If you intending to change RelativeLayout height, then this help.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
Use this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
For Kotlin Users who want to set Height & Width from Dimens file, use this:
relativeLayout.layoutParams.width = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.width)
relativeLayout.layoutParams.height = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.height)

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.

Change LinearLayout height to 0 programmatically

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);

Categories

Resources