12

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);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
sensorario
  • 20,262
  • 30
  • 97
  • 159

6 Answers6

9

You can use :

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

SilentKiller
  • 6,944
  • 6
  • 40
  • 75
  • 1
    +1 for mentioning FrameLayout and not RelativeLayout – Arunkumar Dec 11 '13 at 07:23
  • @ArunkumarSharma : can you pls tell how it is different, actually always really confused when using LayoutParams, that which one should i import, or if you can please suggest any link or post. – DroidDev Dec 11 '13 at 07:27
  • 1
    @VishwasSharma `LayoutParams are used by views to tell their parents how they want to be laid out.` [Check This](http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html) – SilentKiller Dec 11 '13 at 07:32
  • So we use the FrameLayout variation of LayoutParams because the TextView is being added to a FrameLayout? And if it were being added to a LinearLayout should we use LinearLayout.LayourParams, for example? – Chris Dec 04 '15 at 21:30
7

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);
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
2

You can use FrameLayout.LayoutParams.WRAP_CONTENT for width

Apoorv
  • 13,470
  • 4
  • 27
  • 33
0

You can do something like this.

    RelativeLayout.LayoutParams flparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,(int)height);
            youlayoutname.setLayoutParams(flparams);
Ravneet Singh
  • 213
  • 1
  • 13
0

In general, you should use

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

where width and height are each one of the following:

  • A number, to make the view exactly that many pixels wide or tall (to specify a number in dp instead of pixels, see here)
  • 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));
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
-3

try this>>>

layoutParams.getLayoutParams().width = 20;
Muthuraja
  • 551
  • 5
  • 13