How to set Layoutparams height/width in dp value?
Asked Answered
T

4

24

I tried setting height/width manually in button but it didn't work. Then implemented Layoutparams. But size shows small and not getting required dp value.

XML

 <Button
    android:id="@+id/itemButton"
    android:layout_width="88dp"
    android:layout_height="88dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:background="#5e5789"
    android:gravity="bottom"
    android:padding="10dp"
    android:text=""
    android:textColor="#FFF"
    android:textSize="10sp" />

Constructor:

  public Item (int id, String name, String backgroundColor, String textColor, int width, int height){
    this.id = id;
    this.name = name;
    this.backgroundColor = backgroundColor;
    this.textColor = textColor;
    this.width = width;
    this.height = height;

}

Adapter:

@Override public void onBindViewHolder(final ViewHolder holder, int position) {
    final Item item = items.get(position);
    holder.itemView.setTag(item);
    holder.itemButton.setText(item.getName());
    holder.itemButton.setTextColor(Color.parseColor(item.getTextColor()));
    holder.itemButton.setBackgroundColor(Color.parseColor(item.getBackgroundColor()));
    ViewGroup.LayoutParams params = holder.itemButton.getLayoutParams();
    params.width = item.getWidth();
    params.height = item.getHeight();
    holder.itemButton.setLayoutParams(params);

}
Tellurium answered 15/1, 2017 at 8:44 Comment(0)
S
66

When you specify values programmatically in the LayoutParams, those values are expected to be pixels.

To convert between pixels and dp you have to multiply by the current density factor. That value is in the DisplayMetrics, that you can access from a Context:

float pixels =  dp * context.getResources().getDisplayMetrics().density;

So in your case you could do:

.
.
float factor = holder.itemView.getContext().getResources().getDisplayMetrics().density;
params.width = (int)(item.getWidth() * factor);
params.height = (int)(item.getHeight() * factor);
.
.
Scriber answered 15/1, 2017 at 8:51 Comment(2)
Your explanation is very clear and precise! Thanks man! It worked!Tellurium
did not work for me... Calculations are off on different devices ..Turtledove
I
7

Option 1: Use dimens.xml

view.updateLayoutParams {
    width = resources.getDimensionPixelSize(R.dimen.my_width)
    height = resources.getDimensionPixelSize(R.dimen.my_height)
}

Option 2: Dispense with dimens.xml

/** Converts dp to pixel. */
val Int.px get() = (this * Resources.getSystem().displayMetrics.density).toInt()
view.updateLayoutParams {
    width = 100.px
    height = 100.px
}
Ish answered 22/6, 2021 at 12:28 Comment(0)
D
5

I believe you should be using a dp value defined in dimens along with getDimensionPixelSize. In a custom view, the Kotlin implementation would look like:

val layoutParams = layoutParams
val width = context.resources.getDimensionPixelSize(R.dimen.width_in_dp)
layoutParams.width = width
Daglock answered 7/4, 2020 at 17:0 Comment(0)
P
2
  ViewGroup.LayoutParams params = ListView.getLayoutParams();
        params.height = (int) (50 * customsDebts.size() * (getResources().getDisplayMetrics().density));
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        ListView.setLayoutParams(params);
Parkerparkhurst answered 7/11, 2017 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.