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.
Mario Tomás Serrafero · Jul 29, 2015 at 12:10 pm · 1 comment

OnePlus 2 vs Moto X Style: Which is The Better Flagship?

Two big industry names have announced their newest flagship phones within the past few days. Both have also promised great performance for a cheap price, and now that we know the specifications and details about both the Moto X Style and the OnePlus 2, we can begin planning our next purchase and debating which one is better. So, judging from everything we know, which phone is more impressive?

DISCUSS
Mathew Brack · Jul 29, 2015 at 10:35 am · 2 comments

Making Your Own Xposed Modules Is Easier Than You Think

Close to the heart of XDA is the Xposed Framework by Rovo89. Most of us will have used it but you may feel that the module repository is missing something. We have the solution with several guides aimed at getting you started to build your own modules, something that may be daunting but can open an entire new field of development with a little time and effort.     Where better to start than at the beginning? Rovo89 has created a straight forward tutorial for getting started with development for Xposed....

XDA NEWS
Jimmy McGee · Jul 29, 2015 at 06:00 am · 2 comments

ZenFone 2 Lolliflash and ZenPower Giveaway!

We recently did an in-depth review of the Asus Zenfone 2 but one of the things people may not be aware of is that ASUS has actually created a line of accessories to compliment the ZenFone, or any other Android device. The Lolliflash is a Lollipop-shaped external flash and the ZenPower is a thin 10,000mAh external battery. In today's video, Jordan shows off the Lolliflash and the ZenPower Accessories. Additionally, ASUS and XDA have teamed up to give away 5...

XDA NEWS