Skip to content Skip to sidebar Skip to footer

Setting Params For An Inflated View Inside A Relativelayout, But Getting Classcastexception

Hi i'm trying to add a layout programmatically using the inflater (it's a simple TextView). The parent of the inflated view is a RelativeLayout which is in a ScrollView. My problem

Solution 1:

If you use a non null View as the root with the inflate() method the View returned will actually be the View passed as the root in the method. So, in your case view is the _innerLayoutRelativeLayout which has as a parent a ScrollView, which is an extension of FrameLayout(which will explain the LayoutParams).

So either use the current inflate method, but look for the view in the returned view:

View view = inflater.inflate(R.layout.comments, _innerLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.commentsHeader);
(view.findViewByid(R.id.theIdOfTextView)).setLayoutParams(params);

or use another version of inflate()(which doesn't attach he inflated view and manually add it along with proper LayoutParams):

View view = inflater.inflate(R.layout.comments, _innerLayout, false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.commentsHeader);
view.setLayoutParams(params);
_innerLayout.addView(view);

Post a Comment for "Setting Params For An Inflated View Inside A Relativelayout, But Getting Classcastexception"