android - setting LayoutParams programmatically

Android

Android Problem Overview


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 ?

Android Solutions


Solution 1 - Android

Just replace from bottom and add this

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

before

llview.addView(tv);

Solution 2 - Android

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);

Solution 3 - Android

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);

Solution 4 - Android

  LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                   /*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
                   /*height*/ ViewGroup.LayoutParams.MATCH_PARENT,
                   /*weight*/ 1.0f
            );
            YOUR_VIEW.setLayoutParams(param);

Solution 5 - Android

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartinView Question on Stackoverflow
Solution 1 - AndroidKhanView Answer on Stackoverflow
Solution 2 - AndroidRajaReddy PolamReddyView Answer on Stackoverflow
Solution 3 - AndroidM KarimiView Answer on Stackoverflow
Solution 4 - AndroidKapil ParmarView Answer on Stackoverflow
Solution 5 - AndroidDevneet Singh VirdiView Answer on Stackoverflow