-1

I'm quite new with Android so I was wondering if it's a okay to give layout height or width in dp? Also if there is any other approach other than wrap_content/match_parent or dp than do tell. Thanks in advance.

4 Answers4

0

Ofcourse not this is the not a best approach to give height and width in dps instead wrap_content and match_parent properties. Documentation

The important thing is if you give width and heights in dp it is not in favour of supporting all different size of screens instead wrap_content and match_parent is much more favourable in adaptive designs and responsive designs where views get the provided space according to the need.

dp is defined as:

dp Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

Dharman
  • 30,962
  • 25
  • 85
  • 135
Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
  • @Ankita what do u think...i respect both of these answers but just for confirmation purposes! – unqua Laraib Sep 28 '17 at 07:41
  • dear let me try to explain it little bit more, you have to make a new project and make two buttons one with width 400 dp and one with match_parent and test them on two different devices with different screen sizes i'm sure you will get the differences of dp and match_parent thanks. – Nouman Ch Sep 28 '17 at 08:19
  • you should also review the documentation dear it will help you more. – Nouman Ch Sep 28 '17 at 08:19
0

Yes it is ok to give height & width in dp. You can also use fill_parent at place of match_parent.

Fill_parent was depreciated in API level 8. So if you are using API level 8 or above you must avoid using fill_parent.

For more information see this http://code2care.org/pages/fill_parent-vs-match_parent-vs-wrap_content/

Ankita
  • 1,129
  • 1
  • 8
  • 15
0

Yes you can specify height and width in dp.

For conversions between px, dip, dp and sp please see stackoverflow question What is the difference between “px”, “dip”, “dp” and “sp”?

For layout I've found "match_parent" to be applicable in most cases. (To give you more context, "match_parent" used to be called "fill_parent" prior to API level 8). It basically means the view is as big as its parent, just without padding.

If your intention is the make the view just big enough for its content, then use "wrap_content".

Nita
  • 149
  • 12
0

You can give layout height and width in dp. Mostly used for custom size arrangement

Wrap content provides the view size, equal to the content size.

Match parent allows the view to be the same size, as the Relative or Linear layout.