Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set android:layout_width="match_parent" from code?

Tags:

android

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);
like image 996
sensorario Avatar asked Dec 11 '13 07:12

sensorario


1 Answers

You can use :

textView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, height));

like image 172
SilentKiller Avatar answered Sep 27 '22 20:09

SilentKiller