M
M
MODifikaTOR2017-04-14 16:49:36
Java
MODifikaTOR, 2017-04-14 16:49:36

How to programmatically set the size of an element in dp?

I make a dynamic list of layers with elements inside. How can I programmatically set the size in dp and not in pixels?
Current sizing code:

LinearLayout.LayoutParams etParams = new LinearLayout.LayoutParams(156, 33);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Gamega, 2017-04-14
@MODifikaTOR18

int dip = 25
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,  getResources().getDisplayMetrics());

A
Aleksandr Koscheev, 2017-04-21
@ru-alxr

If the size in dp is known in advance, put the value into resources and retrieve as needed.
If not - use Oleg Gamega's solution

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question