74

I putting an in-game chat module into an app. I am adding text messages as they are received into a LinearLayout view. I want to set the layout params to the TextView but the following code is crashing and the error messages befuddle me.

private void addChat(String chat, String when,  Boolean mine) {
    int leftMargin;

    TextView tv = new TextView(this);
    llview.addView(tv);
    tv.setTextColor(Color.WHITE);
    tv.setTextSize(2,25);
    tv.setText(chat);
    if (mine) {
        leftMargin = 5;
        tv.setBackgroundColor(0x7C5B77);
    }
    else {
        leftMargin = 50;
        tv.setBackgroundColor(0x778F6E);
    }
    final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)tv.getLayoutParams();
    lpt.setMargins(leftMargin,lpt.topMargin,lpt.rightMargin,lpt.bottomMargin);

    tv.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

}

when it runs, all of the above code executes but it crashes in android runtime as:

03-13 14:15:38.513: E/AndroidRuntime(12985): java.lang.ClassCastException:      android.view.ViewGroup$LayoutParams

and stepping through with the debugger, it actually processes all of these lines

but then barfs when trying to render with an equally cryptic exception detailed message:

android.view.ViewGroup$LayoutParams

So, what have done to get to this state? What should I be doing to have alternating left/right indented messages ?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Martin
  • 4,711
  • 4
  • 29
  • 37

5 Answers5

137

Just replace from bottom and add this

tv.setLayoutParams(new ViewGroup.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT));

before

llview.addView(tv);
Ziem
  • 6,579
  • 8
  • 53
  • 86
Khan
  • 7,585
  • 3
  • 27
  • 44
32

after creating the view we have to add layout parameters .

change like this

TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));

llview.addView(tv);
tv.setTextColor(Color.WHITE);
tv.setTextSize(2,25);
tv.setText(chat);
if (mine) {
    leftMargin = 5;
    tv.setBackgroundColor(0x7C5B77);
}
else {
    leftMargin = 50;
    tv.setBackgroundColor(0x778F6E);
}
final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)tv.getLayoutParams();
lpt.setMargins(leftMargin,lpt.topMargin,lpt.rightMargin,lpt.bottomMargin);
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • 5
    `lpt.setMargins(..)` does not seem to have any effect. Whether i set it to 2 or 20, it looks exactly the same. I see no space created for the margins at all. I tried it for a `LinearLayout`, not `TextView`. – faizal Nov 09 '14 at 06:38
  • 4
    ^ tv.setLayoutParams(lpt) after the lpt.setMargins() request – worked Mar 30 '16 at 13:04
  • How to setLayoutParams in `dp` values rather than `wrap_content` or `match_parent`. I want to set it to `100dp` each. – Sagar Balyan Aug 28 '17 at 06:56
  • You can try like this : float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, {widthInDp}, r.getDisplayMetrics()); float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, {heightInDp}, r.getDisplayMetrics()); mChart.setLayoutParams(new ViewGroup.LayoutParams( width, height)); – RajaReddy PolamReddy Aug 29 '17 at 06:29
  • Pretty sure that cast will throw an exception – Barry Fruitman Oct 31 '21 at 22:40
5
  LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                   /*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
                   /*height*/ ViewGroup.LayoutParams.MATCH_PARENT,
                   /*weight*/ 1.0f
            );
            YOUR_VIEW.setLayoutParams(param);
Kapil Parmar
  • 881
  • 8
  • 19
5
int dp1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,
            context.getResources().getDisplayMetrics());

tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
dp1 * 100)); // if you want to set layout height to 100dp

llview.addView(tv);
M Karimi
  • 1,991
  • 1
  • 17
  • 34
0

For Xamarin Android align to the left of an object

int dp24 = (int)TypedValue.ApplyDimension( ComplexUnitType.Dip, 24, Resources.System.DisplayMetrics );
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( dp24, dp24 );
            lp.AddRule( LayoutRules.CenterInParent, 1 );
            lp.AddRule( LayoutRules.LeftOf, //Id of the field Eg m_Button.Id ); 
            m_Button.LayoutParameters = lp;