0

Other questions dealing with use of match_parent pre 2.2 have answers stating one should use fill_parent for safe backwards compatibility.

What I've noticed is when your targetsdk is set to 8 or greater and a minsdk is set to 7 or lower, use of match_parent or fill_parent is irrelevant. I'm guessing this is b/c the xml gets compiled down to a binary blob so the use of either one is irrelevant once the app is packaged into an apk.

What I find curious is why there's no runtime error for use of LayoutParams.MATCH_PARENT on 2.1 devices in java code. I'd expect to see some kind of RuntimeException for NoSuchClassMember when trying to access LayoutParams.MATCH_PARENT in code.

I'm guessing that maybe this deals with how how a class file gets compiled in java. Any insight appreciated.

dskinner
  • 10,527
  • 3
  • 34
  • 43

2 Answers2

1

it's ok to use either . both have the same value. on code , it will warn you that it's deprecated , but they are both the exact same thing.

not sure why google made such a weird change . i liked the previous name, since it means that it fills the remaining space of the parent , and not match it, but that's my opinion.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • indeed, what I find curious is - or perhaps the root of my question is, why is there no runtime error for MATCH_PARENT on 2.1 devices in java code. Perhaps this is more java related than android, but I'd expect to see some kind of RuntimeException for NoSuchClassMember when trying to access LayoutParams.MATCH_PARENT in code. – dskinner Jun 16 '12 at 15:38
  • no , it's ok since it's a constant ,which is also a known constant (exact same as fill-parent) . all "constants variable" are converted to their actual values in the bytecode , so as long as they are known there is no way to get any kind of exception . if they weren't known before , then we have a special case that should be checked. – android developer Jun 16 '12 at 15:46
  • I wasn't aware of that for constants, very helpful to know. Thanks! – dskinner Jun 16 '12 at 19:36
  • btw, if you want to always be notified if there could be a problem with older APIs , yet you use the newest APIs (as it's recemmended by google) , you can use Lint for that . it has an option to do this checking , though it was disabled by default . for example , if you call getActionBar() , it will warn/give error to you since it's not available on API 10 and below . – android developer Jun 16 '12 at 19:44
  • you're magic, nice catch - I'll have to dig that option up for sure. – dskinner Jun 16 '12 at 21:39
  • yes, Lint also has tons of other tips that can help you with coding on android. read all of its features. i really liked them . i think some should be built in for java itself. – android developer Jun 16 '12 at 22:52
0

As far as I can tell, those two are exactly the same. In the documentation , it translates to the same value (-1), I think the change is really just proper naming.

You can also check this other question: What is the difference between match_parent and fill_parent?

Note that it will work fine to run your application where you use match_parent on a pre 2.2 device, but the SDK will trigger an error...

After your comment... It does not matter what you run it on, but only what SDK you use to compile... trying to build with 1.6 or 2.1 SDK, it will give you this error:

Description Resource    Path    Location    Type
error: Error: String types not allowed (at 'layout_width' with value 'match_parent').   main.xml    /MyProject/res/layout   line 7  Android AAPT Problem

In Eclipse, you can change the SDK you are using by going in the properties of the project, go in the "Android" page and change the "Project Build Target". I can assure you, it will print an error if you try to use "match_parent" which did not exist before 2.2.

enter image description here

Community
  • 1
  • 1
Matthieu
  • 16,103
  • 10
  • 59
  • 86
  • Actually, no, the sdk does not trigger an error. Testing on 2.1 Google API in emulator, the use of match_parent in xml works fine (as it's compiled down as generally agreed upon) but also works fine if in java using `LayoutParams.MATCH_PARENT` as well. No error thrown and it functions as intended. – dskinner Jun 15 '12 at 21:56
  • I think you've overlooked the context of this question, sorry if I've some how confused you. If you set the targetsdk (as seen in your screenshot) to sdk 8 or greater, but then edit your AndroidManifest.xml and set android:minSdk="4", then this is what's being talked about. – dskinner Jun 16 '12 at 15:32