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

I have a different layout. Some of them are created using xml. Some others dynamically using code. When I'm in xml, I can set the width or height using the value "wrap_content". How to get the same result dynamically? This is a snippet of my dynamic TextView. I need to remove "final int width = 440;" and get the same value "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); 
+6
source share
5 answers

Try the following:

 FrameLayout.LayoutParams layoutParams; layoutParams = getLayoutParams(LayoutParams.WRAP_CONTENT, height, top); TextView textView; textView = new TextView(_registerNewMealActivity); textView.setText(text); textView.setLayoutParams(layoutParams); 
+4
source

You can use:

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

+8
source

You can use FrameLayout.LayoutParams.WRAP_CONTENT for width

+2
source

You can do something like this.

  RelativeLayout.LayoutParams flparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,(int)height); youlayoutname.setLayoutParams(flparams); 
0
source

try this →>

 layoutParams.getLayoutParams().width = 20; 
-1
source

All Articles