Behzad Gh
Behzad Gh

Reputation: 355

resizeing android linear layout at runtime not work right

i have a linear layout within a relative layout , default width and height of the linear layout in xml file is 250 and 150 and it looks good. but when i resize it at runtime and again set width and height to 250 and 150 its not look right like before and it seems width and height visually less than before. i use this code to resize at runtime :

    LinearLayout ll = (LinearLayout)findViewById(R.id.myLayout);
    ll.getLayoutParams().height=150;
    ll.getLayoutParams().width=250;
    ll.requestLayout();
    ll.invalidate();

i think the values we set at runtime are in pixel unit not in dip unit , but im not sure . if it is true , how can i change the unit of width and height at runtime ?

Upvotes: 0

Views: 366

Answers (2)

Ritaban
Ritaban

Reputation: 773

Try like this:

Layoutparams params = ll.getlayoutparams();params.height=300;params.width= 200;ll.setlayoutparams(params);

Upvotes: 0

pfairbairn
pfairbairn

Reputation: 451

Try it with DP instead, below is a way too convert to DP:

int valueInDP= (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, YOURVALUE, getResources().getDisplayMetrics());

Switch out YOURVALUE for a value in PX, such as 250

Upvotes: 1

Related Questions