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.
-
Yes it is ok to give height & width in dp. Yo can also use fill_parent in place of match_parent. – Ankita Sep 28 '17 at 07:10
-
ok thanks for your help – unqua Laraib Sep 28 '17 at 07:13
-
@Ankita `FILL_PARENT` is deprecated starting in API Level 8 and replaced by `MATCH_PARENT`. see [docs](https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT) – UltimateDevil Sep 28 '17 at 07:14
-
Yes I know @VikasTiwari I am just informing him. – Ankita Sep 28 '17 at 07:15
-
Hahaha ok @Ankita – UltimateDevil Sep 28 '17 at 07:21
4 Answers
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 a160 dpi screen
. The ratio ofdp-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
".
-
@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
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/

- 1,129
- 1
- 8
- 15
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"
.

- 149
- 12
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.

- 1
- 1