Skip to content Skip to sidebar Skip to footer

Add Layout With Parameters Programmatically Android

Hello I have a main screen. Copy

This will throw error because the LinearLayout is the child of Relative layout, so you have to set RelativeLayoutParams for that.

Instead use this

RelativeLayout.LayoutParamslp=newRelativeLayout.LayoutParams(
    (w*2)/3, 
    RelativeLayout.LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);

or Instead of creating new LayoutParams you can reuse the existing LayoutParams of the view as shown below.

RelativeLayout.LayoutParamslp= linLayout.getLayoutParams();
lp.width  = (w*2)/3;
lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
linLayout.setLayoutParams(lp);

Solution 2:

Instead of making a new LayoutParams, you should modify the existing one.

Change your code to:

button.setImageResource(R.drawable.plus_yellow);                                                                   
m = myMap.addMarker(newMarkerOptions().position(myMap.getCameraPosition().target).draggable(true));                     
LinearLayoutlinLayout= (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParamslp= linLayout.getLayoutParams();
finalintleft_margin= whatever;
finalinttop_margin=0;
finalintright_margin= whatevermore;
finalintbottom_margin=0;
lp.setMargins(left_margin, top_margin, right_margin, bottom_margin);
lp.width  = (w*2)/3;
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);                      
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);              
bIcon = true;

Post a Comment for "Add Layout With Parameters Programmatically Android"