android - setting LayoutParams programmatically
Asked Answered
P

5

75

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 ?

Pampa answered 13/3, 2012 at 5:23 Comment(0)
P
138

Just replace from bottom and add this

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

before

llview.addView(tv);
Phi answered 13/3, 2012 at 5:50 Comment(6)
How to setLayoutParams in dp values rather than wrap_content or match_parent. I want to set it to 100dp each.Cockerel
I have exactly the same question and can't find the solution.Streak
@SagarBalyan see https://mcmap.net/q/47378/-android-setting-layoutparams-programmaticallyTrichosis
@Streak look at this: https://mcmap.net/q/47378/-android-setting-layoutparams-programmaticallyTrichosis
/*in DP */ ViewGroup.LayoutParams prm = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); prm.height=200; prm.width=200; pieChart.setLayoutParams(prm);Charcoal
Why are we using ViewGroup for TextView?Fredrika
E
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);
Elementary answered 13/3, 2012 at 5:40 Comment(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.Office
^ tv.setLayoutParams(lpt) after the lpt.setMargins() requestHeelpost
How to setLayoutParams in dp values rather than wrap_content or match_parent. I want to set it to 100dp each.Cockerel
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));Elementary
Pretty sure that cast will throw an exceptionPeeve
S
6
  LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                   /*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
                   /*height*/ ViewGroup.LayoutParams.MATCH_PARENT,
                   /*weight*/ 1.0f
            );
            YOUR_VIEW.setLayoutParams(param);
Swat answered 22/1, 2018 at 11:16 Comment(1)
Why did you use LinearLayout and ViewGroup?Fredrika
T
6
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);
Trichosis answered 29/9, 2019 at 11:8 Comment(0)
L
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;
Lovmilla answered 29/7, 2020 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.