To set the layout params (width, height and margins) or to set the LayoutParams it self to a specific Layout programatically, the layout type of the parent must be known.
Example 1
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
someLayout.setLayoutParams(lp);
Here, the someLayout type is not really need to be known. LinearLayout.LayoutParams means that the parent of someLayout is a LinearLayout.
Example 2
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)someLayout.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
lp.height = LinearLayout.LayoutParams.MATCH_PARENT;
The problems come when the parent type is unknown. implementing a customisable listview row layout will led to a situation with an unknown row parent. the parent that holds list item.
Example Layout for list view item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal|top">
.
.
</LinearLayout>
This go inside View returned in the getView method in Adapter class.
How to set LayoutParams to that layout with id @+id/country
???
What I am really concerned about is to set margins to that layout.