What unit of measure does LayoutParams use?
Asked Answered
K

3

24

pI am working with a linear layout and want to set the maximum height of the view. Under "normal" circumstances, I want the view to use "wrap_content." However, occasionally the circumstances may push the layout to an undesirable size. When this happens, I want to limit the height to a maximum 300dp.

I have set the size of the view using the following when the list in the layout exceeds 4 list items:

LinearLayout listLayout = (LinearLayout) dialog.findViewById(R.id.listLayout);
if(list.size() > 4){
    LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300);                           
    listLayout.setLayoutParams(params);
}

Reviewing the documentation leaves me with no clue as to the unit of measure that is applied. What are the units of measure in this situation (dp, sp, px, ...)?

Running tests, even setting the value to 100 has the list exceeding desired height.

Please advise

Kootenay answered 29/6, 2013 at 18:13 Comment(0)
J
22

According to the documentation you linked: pixels. See this function

the width, either MATCH_PARENT, WRAP_CONTENT or a fixed size in pixels

Even though the function you're using doesn't have any explicit documentation, it is implied that it uses the same documentation as the function with the most parameters. The function itself probably looks like:

LinearLayout.LayoutParams(int width, int height) {
    this(width, height, /*some default value*/);
}

i.e. it's simply calling the 3-parameter version with a default value.

Joellajoelle answered 29/6, 2013 at 18:20 Comment(2)
So in reading the documentation the methods public LinearLayout.LayoutParams (int width, int height) public LinearLayout.LayoutParams (int width, int height, float weight) use the same description: Creates a new set of layout parameters with the specified width, height and weight. Parameters width the width, either MATCH_PARENT, WRAP_CONTENT or a fixed size in pixels height the height, either MATCH_PARENT, WRAP_CONTENT or a fixed size in pixels weight the weightKootenay
Yup. The LinearLayout.LayoutParams(int width, int height) function is an overload of the 3-parameter version which likely looks something like this: (EDIT: adding to post because comments don't format code)Joellajoelle
P
3

Here's the full class with imports if your having trouble - @Pierre.Vriens

//usage: int 300dpInPixels = util.convertDpToPixel(300);

import android.content.res.Resources;
import android.util.DisplayMetrics;

public class utils {
    public static int convertDpToPixel(float dp){
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return Math.round(px);
    }
}
Peng answered 20/1, 2018 at 0:37 Comment(1)
int 300dpInPixels = convertDpToPixel(300);Peng
L
1

As David said, setting LayoutParams via code is set in pixels. You can use the code in this thread to convert the desired dp to px.

Loveridge answered 29/6, 2013 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.