13
mainLayout = (LinearLayout) findViewById(R.id.linearLayout);
mChart = new HorizontalBarChart(this);
mChart.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
mainLayout.addView(mChart);

I like to change the width and height to dp units like 100 dp or 200 dp,. the setLayoutParams doesn't take number units , the choices are only (wrap_content and match_content).. I'm new to Android so im confused how to change it.

Ruby
  • 241
  • 1
  • 6
  • 14
  • 1
    `float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());` this way you can convert dp to pixels – Muhammad Babar Feb 12 '16 at 10:24
  • 1
    Does this answer your question? [How to set Layoutparams height/width in dp value?](https://stackoverflow.com/questions/41659338/how-to-set-layoutparams-height-width-in-dp-value) – Tatsuya Fujisaki Jun 23 '21 at 04:44

5 Answers5

18

Another ways is you add your dimension in dimens.xml.

For example, add <dimen name="chart_width">100dp</dimen>.

Then, at your code:

float width = getResources().getDimension(R.dimen.chart_width);
mChart.setLayoutParams(new ViewGroup.LayoutParams(
            width,
            ViewGroup.LayoutParams.WRAP_CONTENT));
Joey Chong
  • 1,470
  • 15
  • 20
  • 2
    `layoutParams.width` expects and `int` so you may need to cast the `getResources...` with `(int)`. – Fat Monk Aug 18 '19 at 20:42
9

Converting dip values into pixels will let your layout build correctly, this line of code will solve it:

int width = (int) TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, 
    getResources().getDimension(R.dimen.desired_width), 
    getResources().getDisplayMetrics()
);
blueware
  • 5,205
  • 1
  • 40
  • 60
3

I think we can't set dp directly to the view. So we've to convert the dimension from pixel to dp.

To get pixel from dp, you can use TypedValue#applyDimension() method.

Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, {sizeInDp}, r.getDisplayMetrics());

So the final code will be

float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, {widthInDp}, r.getDisplayMetrics());

float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, {heightInDp}, r.getDisplayMetrics());

mChart.setLayoutParams(new ViewGroup.LayoutParams(
                width,
                height));
theapache64
  • 10,926
  • 9
  • 65
  • 108
  • where can i set the width to 100 dp for example? – Ruby Feb 12 '16 at 03:12
  • 1
    `float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics());` and apply `mChart.setLayoutParams(new ViewGroup.LayoutParams( width, ViewGroup.LayoutParams.WRAP_CONTENT));` , got it ? – theapache64 Feb 12 '16 at 03:36
  • 1
    @AxeFox, You answer also correct but I think little bit misleading. {widthInPixel} should change {withInDP} because it convert from DP to pixels. Correct me if I am wrong. – Joey Chong Feb 12 '16 at 03:46
  • 1
    @JoeyChong You're absolutely correct, I didn't noticed that mistake. I'll correct it now. and thank you for the correction Joey. :) – theapache64 Feb 12 '16 at 11:49
  • 1
    Worked like a charm! Enormous help by this! Thanks a lot theapache64! – Tomas F. Jul 24 '23 at 16:34
0
int dp1 = dip2pix(getContext(), 1);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
            dp1 * 100, ViewGroup.LayoutParams.WRAP_CONTENT);

public static int dip2pix(@NonNull Context context, int dip) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,
            context.getResources().getDisplayMetrics());
}
M Karimi
  • 1,991
  • 1
  • 17
  • 34
-3

Give it a try it will work for sure

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                200, 200);
        params.setMargins(5, 0, 5, 0);

        mChart= new HorizontalBarChart(getApplicationContext());
        mChart.setLayoutParams(params);

        mainLayout.addView(mChart);
Nitesh
  • 318
  • 3
  • 16