Regarding understanding the layout units
Asked Answered
S

3

1

I am calculating the height of some items in a ListView and then I assign the calculated height to the ListView programmatically to ensure that the ListView will expand to accommodate its contents. I calculated the height of the items in the ListView as shown below in the code.

As shown in the code, the following line returns a value equal to 533:

height += listView.getDividerHeight() * listView.getCount();// 

The problem I have is, when the height of the ListView is set to the value returned "533" as shown in the code, the ListView has exactly the same heights as its contents. But when I set the same value "533" to the ListView throughout "android:layout_height" in the XML file, then the ListView expands too much beyond its contents "higher than its contents"

So my questions are:

  1. Please explain why that is happening?

  2. As for the posted code, when I change the 'TypedValue.COMPLEX_UNIT_DIP' to any other unit like 'TypedValue.COMPLEX_UNIT_PX' there no change at all. Why there is no effect when changing the unit of the LayoutParameter?

code:

int height = 0;
for (int i = 0; i < listView.getCount(); i++) {
    View childView = listView.getAdapter().getView(i, null, listView);
    childView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    height+= childView.getMeasuredHeight();
}
//dividers height
height += listView.getDividerHeight() * listView.getCount();// returns 533

param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, this.getResources().getDisplayMetrics()));
param.addRule(RelativeLayout.BELOW, ruleSubject);
container.setLayoutParams(param);
listView.requestLayout();
Stickybeak answered 10/12, 2016 at 10:42 Comment(0)
D
0

1-please explain why that is happening?

in .xml layout files, You are using the values in dp. And the calculated value 533 is 533px.So in layout xml file use

android:layout_height="533px"

you will get the same result.

Didactics answered 10/12, 2016 at 10:54 Comment(3)
fine..but when i convert the calculated value "522" to dpi get 266...and when i set the claculated dp value programmatically as shown in the code..i get a very small listview height even if i specified the right unit in the code programmaticallyStickybeak
In programmatically you have to give it in px.convert the value to px.You will get the same result.for conversion, check the above answerDidactics
I think your conversion was wrong.Check the last answer.hope it helps !!!Didactics
M
0

Firstly, you need to read about sizes here. It says

The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

1-please explain why that is happening?

That's because how those pixels are rendered. When you do it in XML it's statically rendered and when you do it in Java it is dynamically rendered.

Maupin answered 10/12, 2016 at 10:57 Comment(0)
D
0

2-as for the posted code, when i change the 'TypedValue.COMPLEX_UNIT_DIP' to any other unit like 'TypedValue.COMPLEX_UNIT_PX' there no change at all. why there is no effect of changing the unit of the layoutParameter

ans: The return value will be in px.

enter image description here

enter image description here

enter image description here

Didactics answered 10/12, 2016 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.