Reputation: 21698
I've different layout. Some created by xml. Some others dynamically via code. When I am on xml I can set width or height with "wrap_content" value. How to get the same result dynamically? This is the snippet of my dynamic TextView. I need to remove "final int width = 440;" and get same value of "wrap_content". How?
final int width = 440;
final int height = textViewHeight;
final int top = getNewTop(height);
FrameLayout.LayoutParams layoutParams;
layoutParams = getLayoutParams(width, height, top);
TextView textView;
textView = new TextView(_registerNewMealActivity);
textView.setText(text);
textView.setLayoutParams(layoutParams);
_frameLayout.addView(textView);
Upvotes: 13
Views: 27226
Reputation: 8912
In general, you should use
textView.setLayoutParams(new FrameLayout.LayoutParams(width, height));
where width
and height
are each one of the following:
FrameLayout.LayoutParams.WRAP_CONTENT
FrameLayout.LayoutParams.MATCH_PARENT
Also, if you're using LinearLayout
, you should use LinearLayout.LayoutParams
instead, and the same for RelativeLayout
.
For example, if you want textView
to have the same behavior as if it was declared as <TextView android:layout_width="wrap_content" android:layout_height="match_parent"/>
, you would do
textView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT));
Upvotes: 2
Reputation: 23638
Try out as below:
FrameLayout.LayoutParams layoutParams;
layoutParams = getLayoutParams(LayoutParams.WRAP_CONTENT, height, top);
TextView textView;
textView = new TextView(_registerNewMealActivity);
textView.setText(text);
textView.setLayoutParams(layoutParams);
Upvotes: 8
Reputation: 213
You can do something like this.
RelativeLayout.LayoutParams flparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,(int)height);
youlayoutname.setLayoutParams(flparams);
Upvotes: 0
Reputation: 6942
You can use :
textView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, height));
Upvotes: 9