Mike Szczys · Jun 16, 2013 at 11:30 pm

Why Devs Should Avoid ‘Null’ when Inflating Layouts

We’ve all done it at one point or another. You are hammering out some code and need one aspect of a method call to work so you just put null in for the rest of the parameters instead of actually researching how they work. I’ve actually done the very thing Dave Smith writes of in his article about inflating layouts. The second argument in a LayoutInflater inflate method calls for ‘ViewGroup root.’ I just slap ‘null’ in there and go about my business, none the wiser. There’s nothing wrong with that; it pulls the XML file passed as the first parameter into your layout so that it can be displayed on the screen. You could go years without having a real problem with it. But when it does it bite you at some point down the road, you’re going to pay a big time penalty trying to figure out what is going wrong.

Dave did a great job writing his article explaining how a LayoutInflater works. The reason the inflater is looking for a ViewGroup is to inherit any styling that may have been set at a higher lever. He shows an example of this using a ListView that should have a fixed height specified by the theme. If you specify null, it has no context from which to inherit the values and results in the smallest height possible. He goes on to assume that the developer would then create a second XML specification to fix the problem, and I think he’s spot on with that prediction. This breaks the concept of themes and makes the code much harder to maintain. He does mention an exception when inflating for an AlertDiaog, but this is a moot point because the inflater will basically ignore the ViewGroup parameter when specified for a dialog.

[via Reddit || Photo Source]


_________
Want something on the XDA Portal? Send us a tip!

Mike Szczys

szczys is an editor on XDA-Developers, the largest community for Android users. Mike Szczys is a professional musician but spends all his free time working with hobby electronics. As Contributing Editor for Hackaday.com he became interested in Android as some of the early hardware hacks started popping up on the Internet. What followed was a gradually rising addiction to all things Android. View szczys's posts and articles here.
Emil Kako · Jun 29, 2015 at 01:36 pm · 5 comments

Do You Have Insurance on Your Smartphone?

While affordable smartphones are becoming more available, the majority of high-end flagships are still upwards of $600. With these devices carrying such a hefty price, people look for different ways to protect their investment. The most popular way to protect your smartphone is just to buy a case, but many people like the added peace of mind that comes with insuring your phone. We're interesting in seeing just how many of you actually buy insurance on your smartphone. Let us know why or why not in the comments below.

DISCUSS
Jack Jennings · Jun 29, 2015 at 12:45 pm · 3 comments

Apple Music Will Increase iTunes Match to 100,000 Songs

The battle for music streaming subscription dominance is starting to really heat up, with Apple planning their next offensive in line with the release of iOS9 later in the year. A major draw for these kind of services is the ability to have your current music library uploaded and copies of the tracks available to be streamed on the go.   Today Eddy Cue, an Apple Exec has released some details on how iTunes Match will integrate with the soon to be released...

XDA NEWS
Share This