11 March 2013

What are the difference between MATCH_PARENT and FILL_PARENT property in Android?

When you trying creating a layout for android,you can find that set  xml attributes for android:layout_height and android:layout_width contains 2 constants ( FILL_PARENT and  MATCH_PARENT  )and they have  same function . Confusing ? No worries,this post will help you to understand.

Let's start from basics. Some basics there are 3-4  types of xml attributes for android:layout_height and android:layout_width
  •  an exact number (you can using units like: px , dp , sp  , in or mm.  
  • WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding).  
  • FILL_PARENT,which means that the view wants to be as big as its parent (minus padding)  until API Level 8.  
  • MATCH_PARENT which means that the view wants to be as big as its parent (minus padding)  ...  in API Level 8 and higher.
As you discover that:
  1.  FILL_PARENT and   MATCH_PARENT has the same function!
  2. There is one difference.  MATCH_PARENT  is in use  since API level 8 and second one  FILL_PARENT  was used until API level 8,but you can still use,  this constant is deprecated.
So which value You should use?

SOLUTION:
this is my suggestions .
  1.  If you planning set  android:minSdkVersion to 7 and lower ,then use  FILL_PARENT. If you planning  set  android:minSdkVersion = 8 and higher  MATCH_PARENT. I  using  MATCH_PARENT  and i am not supporting Android 2.2 and lower anymore. 
  2. Universal solution is to use value : -1.As constant value of  FILL_PARENT and  MATCH_PARENT is -1. :)

See  official documentation for details:
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

BTW. Google renamed  FILL_PARENT  to   MATCH_PARENT  as  FILL_PARENT  was confusing for many developers.
Result?
There is even more confusion now as  readability of many examples is decreased and cause misunderstanding for beginners..



No comments:

Post a Comment