Reputation: 292
I want to update position and size of view programmatically, but I am unable to do this, if i work on one of these two, it works fine. First I update the size
oldLayout = (android.widget.RelativeLayout.LayoutParams) view.getLayoutParams();
changeInWidth = oldLayout.width*ratioWidth;
changeInHeight = oldLayout.height*ratioHeight;
newLayout = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
if (standardWidth>screenWidth)
newLayout.width = (int) (oldLayout.width-changeInWidth);
else if (standardWidth<screenWidth)
newLayout.width = (int) (oldLayout.width+changeInWidth);
else
newLayout.width=oldLayout.width;
if (standardHeight>screenHeight)
newLayout.height = (int) (oldLayout.height-changeInHeight);
else if (standardHeight<screenHeight)
newLayout.height = (int) (oldLayout.height+changeInHeight);
else
newLayout.height=oldLayout.height;
//newLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//newLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Now this code starts for updating the position
if (standardWidth>screenWidth)
newLayout.leftMargin = (int) (oldLayout.leftMargin-changeInWidth);
else if (standardWidth<screenWidth)
newLayout.leftMargin = (int) (oldLayout.leftMargin+changeInWidth);
else
newLayout.leftMargin=oldLayout.leftMargin;
if (standardHeight>screenHeight)
newLayout.topMargin = (int) (oldLayout.topMargin-changeInHeight);
else if (standardHeight<screenHeight)
newLayout.topMargin = (int) (oldLayout.topMargin+changeInHeight);
else
newLayout.topMargin=oldLayout.topMargin;
view.setLayoutParams(newLayout);
Upvotes: 0
Views: 1469
Reputation: 4162
TextView Status = (TextView) findViewById(R.id.Status);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)Status.getLayoutParams();
Context context = getApplicationContext();
float d = context.getResources().getDisplayMetrics().density;//get density
int p = (int)(80 * d);//size 80 is in pixel so multiply by d to get it in dp
params.setMargins(0 , p , 0 , 0 ); //substitute parameters for left, top, right, bottom
Status.setLayoutParams(params);
Upvotes: 1