Return to Video

07-39 Build 2-Pane Tablet UI

  • 0:00 - 0:03
    By taking the knowledge that we
    learned about overriding resources
  • 0:03 - 0:04
    in other folders,
  • 0:04 - 0:08
    we'll walk through the code together on
    how to build up the two-pane tablet UI.
  • 0:08 - 0:12
    First remove the values W820dp folder
    because we don't need to provide
  • 0:12 - 0:16
    specific logic when for when the current
    orientation is greater than 820dp.
  • 0:16 - 0:19
    Then go ahead and
    make the layout XML changes.
  • 0:19 - 0:22
    Then create a new
    layout SW 600dp folder.
  • 0:22 - 0:26
    And then add a new file
    called activity_main.
  • 0:26 - 0:31
    We use the same file name as in
    the base layout folder activity_main so
  • 0:31 - 0:35
    that this one overrides the behavior
    specifically on tablets.
  • 0:35 - 0:38
    To see the code for this file you
    can check out the gist below.
  • 0:39 - 0:43
    Essentially it's a horizontal linear
    layout that can hold a forecast fragment
  • 0:43 - 0:46
    on the left and
    a detail fragment on the right.
  • 0:46 - 0:50
    Now's a good time to talk about
    static verses dynamic fragments.
  • 0:50 - 0:53
    In our implementation the forecast
    fragment is the static fragment because
  • 0:53 - 0:57
    we're defining it in the XML layout no
    matter what orientation or device size
  • 0:57 - 0:59
    we know that we're going to need a
    forecast fragment in the main activity.
  • 0:59 - 1:03
    On the other hand, we only declare
    a container for the detail fragment, but
  • 1:03 - 1:05
    not the actual fragment.
  • 1:05 - 1:08
    It's initialized with different
    arguments each time as a dynamic
  • 1:08 - 1:11
    fragment, so it's better to
    dynamically create and add that
  • 1:11 - 1:15
    fragment in a fragment transaction
    in the main activity Java code.
  • 1:15 - 1:19
    That way the fragment manager can keep
    track of those initialization arguments
  • 1:19 - 1:23
    and pass those back to us on,
    after device rotation.
  • 1:23 - 1:26
    Then we need to update
    the one pane UI layouts so
  • 1:26 - 1:28
    that they're consistent
    with the two pane case.
  • 1:28 - 1:32
    So in the activity main file for
    the base layout folder this used to be
  • 1:32 - 1:35
    a frame layout we're going to
    declare it as a forecast fragment.
  • 1:35 - 1:37
    That way it will match the two pane UI,
  • 1:37 - 1:40
    where this also is declared
    as a fragment in the XML.
  • 1:40 - 1:43
    That way the main activity never has
    to worry about dynamically adding
  • 1:43 - 1:45
    the forecast fragment.
  • 1:45 - 1:47
    In the main activity
    on create view method.
  • 1:47 - 1:51
    Since the fragment is already inside
    this layout we can just remove this so
  • 1:51 - 1:53
    we don't dynamically add it again.
  • 1:53 - 1:58
    Similarly we modify the activity detail
    layout in the base layout folder
  • 1:58 - 2:01
    we change the frame layout ID
    to be weather detail container.
  • 2:01 - 2:06
    So that it matches the container
    view id in the two pane UI case.
  • 2:06 - 2:08
    The pattern here is that
    the detail fragment will always be
  • 2:08 - 2:11
    added to a container called
    weather_detail_container,
  • 2:11 - 2:14
    both in the two pane, and one pane case.
  • 2:14 - 2:16
    Since we changed the name
    of the container,
  • 2:16 - 2:18
    we should also update
    the detail activity.
  • 2:18 - 2:20
    This is only used in one pane mode.
  • 2:20 - 2:22
    Here's where we change
    the container name.
  • 2:23 - 2:26
    In the one pane mode the detailed
    activity will add the detail fragment
  • 2:26 - 2:28
    dynamically to this container.
  • 2:28 - 2:32
    After we modify the layout we
    should update the main activity so
  • 2:32 - 2:34
    we dynamically add the detail fragment.
  • 2:34 - 2:38
    In the main activity on create method we
    check for the presence of the weather
  • 2:38 - 2:43
    detail container in the layout to know
    whether this is a two pane UI or not.
  • 2:43 - 2:46
    We keep track of this information
    in a Boolean called mTwoPane.
  • 2:46 - 2:50
    Remember that we start with the letter
    m, because it's a member variable.
  • 2:50 - 2:52
    In this case,
    the Boolean should be true.
  • 2:52 - 2:54
    So we go ahead and
    create a new detail fragment, and
  • 2:54 - 2:57
    add it to the weather_detail_container.
  • 2:57 - 3:00
    We commit the change by using
    a fragment transaction,
  • 3:00 - 3:01
    which Rado introduced earlier.
  • 3:01 - 3:05
    Otherwise If the detail container
    is not present in the layout, then
  • 3:05 - 3:09
    the boolean should be false, meaning
    that this is a one-pane UI for phones.
  • 3:09 - 3:13
    In this case, the detail activity will
    handle showing the detail fragment.
  • 3:13 - 3:17
    Notice for the two-pane case that
    we check if the saved instant state
  • 3:17 - 3:18
    bundle is null.
  • 3:18 - 3:20
    If the save instant save
    bundle is not null,
  • 3:20 - 3:23
    then we don't create a new one,
    and here is the reason why.
  • 3:23 - 3:25
    Say you want to rotate the device.
  • 3:25 - 3:26
    Before the activity and
  • 3:26 - 3:30
    fragments get torn down, we store
    information in save state bundles.
  • 3:30 - 3:34
    Then after the orientation change,
    the system restores the activity and
  • 3:34 - 3:37
    the fragments,
    by passing back the same bundle, so
  • 3:37 - 3:40
    that it can be recreated
    with the same state.
  • 3:40 - 3:43
    That means if the bundle exists,
    then we should let the system
  • 3:43 - 3:47
    handle restoring the detail fragment,
    and we can skip this code.
  • 3:47 - 3:49
    Once a detail fragment
    is added dynamically.
  • 3:49 - 3:53
    Make it show some placeholder
    data just for testing purposes.
  • 3:53 - 3:56
    Later we'll plumb through the right
    logic, so that it can display the right
  • 3:56 - 3:58
    information for
    the selected date on the left.
  • 3:58 - 4:00
    Modify the detail fragment so
  • 4:00 - 4:04
    that it doesn't expect the incoming
    intent to have a data URI.
  • 4:04 - 4:07
    In this case,
    the detail fragment will just fall back
  • 4:07 - 4:09
    to some placeholder data
    that we have in our XML.
  • 4:09 - 4:13
    The reason the intent could be blank
    is because the detail fragment
  • 4:13 - 4:15
    can now exist within the main activity.
  • 4:15 - 4:18
    And the main activity isn't
    launched with a URI for
  • 4:18 - 4:22
    only one date like the detail
    activity is normally launched with.
  • 4:22 - 4:23
    Once you make the changes for
  • 4:23 - 4:25
    the wire frames this is what
    the app should look like.
  • 4:25 - 4:28
    The reason it doesn't show
    an icon here is because
  • 4:28 - 4:31
    we removed the icon from being
    hard-coded in the layout.
  • 4:31 - 4:34
    But once this data is populated
    dynamically in a later section,
  • 4:34 - 4:35
    it should show up again.
Title:
07-39 Build 2-Pane Tablet UI
Description:

07-39 Build 2-Pane Tablet UI

more » « less
Video Language:
English
Team:
Udacity
Project:
UD853 - Developing Android Apps
Duration:
04:36
There has been no activity on this language so far.

English subtitles

Revisions Compare revisions