Office Hours — Today, September 22

Thursday, September 18

Sep 22
3:55 PM
Mark M.
has entered the room
Mark M.
turned on guest access
Harlan K.
has entered the room
Mark M.
hello, Harlan!
how can I help you today?
Harlan K.
Hi Mark.
Hang on a sec., gathering our question here.
4:00 PM
Harlan K.
We are writing a BroadcastReciever that needs to talk to our Application class where we have a method that needs to get called but we're not sure how to get a handle to the Application class instance.
Are we going about this the wrong way?
Mark M.
call getApplicationContext() on the Context passed into onReceive()
Harlan K.
So we don't worry about this from SO: "Although in current Android Activity and Service implementations, getApplication() and getApplicationContext() return the same object, there is no guarantee that this will always be the case ..."
Gabriele
has entered the room
Harlan K.
Gabriele
hi all
Mark M.
(hello, Gabriele -- I will be with you in a moment!)
4:05 PM
Mark M.
Harlan: well, I'd use getApplicationContext() consistently, but other than that, I think the answerer's concerns are over-blown
Harlan K.
ok, that's what we need to know on that one. I do have some UI questions but you're welcome to get Gabriele in the meantime.
Mark M.
Gabriele: your turn! do you have a question?
Gabriele
I'm extending FragmentStatePagerAdapter, I don't understand if getPageTitle is called before, after getItem (or "randomly"), because my idea was to store a String inside the fragment and get this string as a title
but if it's called before, I still don't have the fragment (and so, no String)
Mark M.
I would not rely upon the order of getItem() versus getPageTitle()
however, you can lazy-create the fragment in either of those places, and use the lazily-created fragment in the other place
IOW, if getPageTitle() is called for a position, and you don't have the fragment for that position yet, go make it
if getItem() is called for a position, and you don't have the fragment for that position yet, go make it
then the order will not matter
Gabriele
ok, so you would create fragment anywhere
thanks
Mark M.
Harlan: back to you! do you have another question?
Harlan K.
I have a UI question. In HTML we work hard to build one "layout" that works across many device sizes. In Android, it doesn't appear that this strategy works if your UI is anything beyond trivial. Is that the case?
4:10 PM
Mark M.
IMHO, it works roughly as well on Android as it does on HTML, insofar as any given UI may or may not intrinsically scale well arbitrarily
the *mechanics* of doing it in Android usually involve separate layout files, compared to CSS media queries
Harlan K.
OK, that makes sense and on that note, I have another question but maybe you get Gabriele next.
Mark M.
OK
Gabriele: back to you! do you have another question?
Gabriele
not for now, thank you
Mark M.
OK, if you come up with another question, just let me know
Harlan: over to yoU!
er, you!
Harlan K.
I guess I'll ask my next one. I see information about using different folders for different layout sizes. Most info shows using mdpi, hdpi, xhdpi etc., or else small, medium, large, xlarge but I see little info about combining these two even though I understand its possible to do so.
If you're desiging an application to work on many devices of unknown sizes and dpi, what sort of layouts should I be creating?
Mark M.
using density qualifiers (e.g., xhdpi) for anything but drawable resources is a serious code smell
4:15 PM
Harlan K.
I saw this combination on a SO post:
View paste
layout-small-ldpi 
layout-small-land-ldpi
layout-ldpi
layout-land-ldpi
layout-mdpi
layout-land-mdpi
layout-large-mdpi
layout-large-land-mdpi
layout-large
layout-large-land
layout-hdpi
layout-land-hdpi
layout-xlarge
layout-xlarge-land
Mark M.
and that person doesn't work for me
Harlan K.
:)
Mark M.
if that person did, we'd have a serious conversation
Harlan K.
So then my question is, what would you expect to see in my folder?
Mark M.
first, for new app development, I'd focus on the -swNNNdp, -wNNNdp, and -hNNNdp qualifiers, rather than the old -small, -normal, -large, and -xlarge ones
Harlan K.
small, medium, large, and xlarge, and then perhaps landscape for each?
ok
Mark M.
like CSS media queries, the -swNNNdp family allow you to define your own points at which you adopt a new layout
whereas the original set of four were fixed by the framework
and, since -swNNNdp and kin were added in Android 3.1 or 3.2, they're generally available
Harlan K.
ok, and NNN represents device width or DPI or neither.
We're only supporting 4.x
Mark M.
density-independent pixels
so -w800dp says "use this layout for screens whose current width is 800dp or larger"
-sw800dp says "use this layout for screens whose 'smallest width' is 800dp or larger"
Harlan K.
what are the sw and the h?
ok, I see you were typing.
Mark M.
where 'smallest width' is whichever is the shorter axis of the device, regardless of orientation
-h is for current height, if that makes more sense as how you'd decide which layouts to use
Harlan K.
Do I need folders for portrait mode or is the omission of "land" assumed to be portrait?
Mark M.
-wNNNdp and -hNNNdp are intrinsically orientation-dependent
Harlan K.
which means -w is for landscape?
Mark M.
no, -w means "current width"
4:20 PM
Mark M.
and the current width depends upon orientation
for example, take the master/detail pattern
when the screen size is wide enough, you want to use a list on the left and detail on the right
Harlan K.
ok, so if you go this route you don't really need to use port or land qualifiers.
Mark M.
but on smaller sizes, you want to only show one at a time
usually, it doesn't matter whether they're holding the device in portrait, landscape, at a 45-degree angle, behind their back, etc.
it only matters whether the width is big enough
and that's where -wNNNdp comes into play
similarly, CSS media selectors focus on width, not orientation
(unless I missed something when I last poked at CSS media selectors...)
Harlan K.
ok, that makes sense. And that really does clear up something that's very confusing right now. I've got about 6 android books now and none of them tell me what you've just been telling me.
Mark M.
well, you have one Android book that tells you this :-)
see my chapter on resource sets and configuration changes
and suggest improvements if you think it didn't cover this well enough!
Harlan K.
Great! Is it possible to get your book in paper? I figure at almost 3000 pages I might have to have local freight bring it in?
Mark M.
I do not publish it in print
you are welcome to print it yourself, but if a tree walks up to you and bops you on the head in response, don't blame me :-)
overall, then, you use res/layout/ and res/layout-land/ for your base layouts, then override them in res/layout-wNNNdp/ or other variations
and you leave the density qualifiers for drawables
Harlan K.
Question on a different topic. I see your book has quite a bit of information about Android Studio and Gradle. I've been reading and hearing from other people that Android Studio isn't ready for production use yet. I'm assuming you don't feel that way about it. Can you comment on that?
4:25 PM
Mark M.
sigh
at the 2013 Google I|O Conference, Google indicated that they would be supporting Eclipse indefinitely
at the 2014 Google I|O Conference, Google indicated that they may not support Eclipse all that much longer
Android Studio has its issues, though the current beta is fairly solid
2800-page books steer like battleships
Harlan K.
When we say it has issues, does it have entire pieces missing that prevent you from producing a working app?
Mark M.
oh, no
many firms have been using Android Studio for production work since last fall
Harlan K.
Because Eclipse has issues too, to be honest. Quite a number of them methinks.
Mark M.
absolutely
and right now, I'd consider them roughly tied on bug count
similarly, Android Studio has some features that Eclipse lacks, and vice versa
Harlan K.
I'm weary of things breaking and Lint screaming only to find out that several Cleans and restarts clear up the problem.
Mark M.
well, you'll have a bit of that in Android Studio too, though it seems to be less frequent
the bigger thing, from the standpoint of the book, is that I felt that I had to start covering Android Studio now, rather than wait for 1.0, given Google's waffling on Eclipse
all else being equal, I would have preferred to wait
all else being equal, I would prefer to have hair
Harlan K.
We did have another question regarding updating the UI frequently. We have a timer where we may want to display seconds on the screen. The issue is not so much ow to do this, but rather, will it cause performance issues for us to be doing this? The code just uses a local date variable so it's not making db calls.
Mark M.
um, well, that will depend a bit on how frequently you are doing things
Harlan K.
I think we have it at 500 ms right now
4:30 PM
Mark M.
that shouldn't be a problem, so long as it is only when you are in the foreground
personally, I use postDelayed() "loops" for this, as it saves a thread and related hijinks
Harlan K.
I think we are doing it from main_activity, which I thought "dies" when a screen is changed or the application is "minimized".
Mark M.
no
or, at least, not necessarily
an activity is destroyed when something calls finish()
such as the default BACK button behavior
if you start another activity, that does not destroy the original activity
if the user presses HOME, that does not destroy the activity that had been in the foreground
Harlan K.
so what sort of class are you doing the postDelayed() from, your activity or something else?
Mark M.
activity or fragment
it's good for light loose time-based UI updates, like elapsed time counters and such
there's a sample app from the book demonstrating it
Harlan K.
cool
We have an ActionBar where we'd like to have some dynamic text centered, with menu buttons on the right that are always showing (for certain screen sizes).
I've messed with adding a TextView using code but don't like the limited ability to center it.
4:35 PM
Harlan K.
Do we need to use a Custom Layout for our ActionBar to make this work?
Mark M.
personally, I wouldn't do that
in terms of how to implement it, I have no idea
Harlan K.
Any comment on why you wouldn't do it that way?
Mark M.
it goes against the design guidelines
and I try to only violate the design guidelines where it's a clear advantage for the user
Harlan K.
I'm assuming we could probably come up with a different design that doesn't need the text centered, but it seemed to me the alternative was to have it right next to the menu items.
Mark M.
have it replace your title, then
Harlan K.
This happens to be our Project Name so the user doesn't expect it to look like a menu option.
Mark M.
or don't put it in the action bar
Harlan K.
ok, I think I could make either of your suggestions work.
Mark M.
that definitely sounds like it should be the action bar title
or go with "App Name: Project Title" as the action bar title, if you feel you need to show both bits of text
(though that'll get wordy on phones in portrait mode)
Harlan K.
yeah. Can we use something to detect device width in code? I'm sure, can't say I've looked for it though.
Mark M.
DisplayMetrics will have the resolution
Configuration should have the classic screen-size buckets (small/normal/large/xlarge)
Harlan K.
If we're only supporting 4.x, do we need to use ActionBarSherlock?
Mark M.
no
4:40 PM
Mark M.
go with the native action bar
Harlan K.
ok
Gabriele
Mark, I have some difficult, sometimes, to *find* what of the features I use, are compatible with a specific version of Android
e.g. I want to check from android studio if the project I'm developing is using something not compatible with an old version
how can I do?
Mark M.
Gabriele: usually that is indicated in the JavaDocs
Gabriele
yes but I should check call by call if it is compatible
no way to check automatically the entire project? TT
Mark M.
well, Lint tells you if you are using something that is newer than the minSdkVersion
that automatically runs when you save the file
Gabriele
so changing my minSdkVersion it should me what it's wrong
tell*
thank you
Mark M.
your minSdkVersion should be set to the oldest version of Android that you are supporting
Gabriele
yes
Mark M.
your compileSdkVersion is what you are compiling against
Lint will tell you if you are using something newer than the minSdkVersion but within the compileSdkVersion
if either of you have another question, go right ahead
Harlan K.
Mark, it seems that figuring out which Layout type to use is almost as much an art as a science. Is there any good information available somewhere on how to design more advanced layouts and which containers to choose and why?
4:45 PM
Mark M.
um, well, "more advanced layouts" covers a lot of ground :-)
Harlan K.
I'm looking for a handdrawn wireframe to upload here. Hang on...
The file includes three wireframes. The one in question is the first one.
Some design changes are now underway that call for some overlapping elements. Namely something that sits in the middle on top of the two "halves", at the top.
Which I think will call for a FrameLayout
?
Mark M.
I only use FrameLayout as a placeholder for dynamic fragments
I use RelativeLayout in cases where I need Z-axis ordering
Harlan K.
ok. Should we use Linear for the child layouts within the Parent RelativeLayout?
Mark M.
possibly
Gabriele
Mark, I'm storing reference of fragments, anyway it seems that I like these reference to be inside activity (because there are life cycle calls), but I need inside the viewpager's adapter, too, where should I store them?
(ops sorry)
Mark M.
Harlan: I cannot tell you, from a glance at a wireframe, exactly how to implement it
4:50 PM
Harlan K.
ok, no problem. I think we'll be able to figure it out. We currently have it working on the target device.
Mark M.
Gabriele: personally, this is part of the reason why I wrote ArrayPagerAdapter, so I can more readily get back fragments that are the pages
Gabriele
what ArrayPagerAdapter is?
Mark M.
Gabriele
I'm also scared of memory leaks, by passing fragment reference
Mark M.
it is a more flexible PagerAdapter implementation than either FragmentPagerAdapter or FragmentStatePagerAdapte
er, FragmentStatePagerAdapter
it is covered in the Advanced ViewPager chapter in the book
Gabriele
thank you, I will read about it
Harlan K.
Mark I think you answered all the questions I had for today. Let me code a while and I'm sure I'll have more.
4:55 PM
Mark M.
OK
Harlan K.
I guess I do have one more. Is there a particular wireframing tool which you are happy to recommend?
I've been going with hand drawings which works oKI
OK
Mark M.
I do not use a wireframing tool, so I don't have a recommendation
Harlan K.
ok
Mark M.
any last questions?
Harlan K.
Not from me, thanks for you help.
5:00 PM
Mark M.
OK, that's a wrap for today's chat
the chat transcript will be published on http://commonsware.com/office-hours/ shortly
Gabriele
thank you Mark, see you next time :)
Mark M.
the next chat is tomorrow at 9am US Eastern Time
have a pleasant day!
Harlan K.
has left the room
Gabriele
has left the room
Mark M.
turned off guest access

Thursday, September 18

 

Office Hours

People in this transcript

  • Gabriele
  • Harlan Koehn
  • Mark Murphy

Files in this transcript