Return to Video

Scrolling Along - Solution

  • 0:01 - 0:03
    Let's Google search for how to do this.
  • 0:03 - 0:05
    I'm going to type in how to
    scroll in an android app.
  • 0:06 - 0:08
    Let's check out the first result.
  • 0:08 - 0:11
    The question is, how to make
    my layout able to scroll down?
  • 0:11 - 0:14
    That's pretty similar to what we want.
  • 0:14 - 0:16
    I skip down to the answer part and
    this has a green check mark and
  • 0:16 - 0:18
    lots of up votes.
  • 0:18 - 0:20
    So this could be a good answer.
  • 0:20 - 0:23
    It says, just wrap all of
    that inside a ScrollView.
  • 0:23 - 0:26
    And then it has an example, ScrollView,
  • 0:26 - 0:29
    and then it specifies name space and
    then it says.
  • 0:29 - 0:34
    Fill_parent for width and height, and
    then it has an ending tag here, and
  • 0:34 - 0:39
    in this comment it says, put the rest
    of your current view inside of it.
  • 0:40 - 0:43
    For background knowledge, fill_parent
    is actually the same as match_parent.
  • 0:43 - 0:46
    It's just an older version of it,
    but they mean the same thing.
  • 0:46 - 0:51
    On this next line it says that
    ScrollView can contain just one item.
  • 0:51 - 0:53
    It looks like this was
    someone's original layout, and
  • 0:53 - 0:56
    then to put inside of
    scroll view then they just
  • 0:56 - 1:00
    made scroll view be the parent view and
    the linear layout be the child's view.
  • 1:00 - 1:03
    Okay that was a helpful response so
    let's go back and
  • 1:03 - 1:04
    see if there's any other solutions.
  • 1:07 - 1:09
    This person is describing
    the same thing.
  • 1:09 - 1:14
    They have XML lay out and
    I think they want to make it scroll.
  • 1:14 - 1:17
    Usually, I don't read
    the full question in detail.
  • 1:17 - 1:19
    I just get to the answer part.
  • 1:19 - 1:20
    This person says,
  • 1:20 - 1:23
    have you tried to include all of
    your XML into a parent ScrollView?
  • 1:23 - 1:26
    And then they give an example
    of ScrollView, and
  • 1:26 - 1:28
    then the child is a RelativeLayout.
  • 1:28 - 1:31
    And then they have a closing tag for
    ScrollView.
  • 1:31 - 1:34
    That's all they have in this thread.
  • 1:34 - 1:38
    But it looks like the two search results
    here gave the same advice of using
  • 1:38 - 1:39
    a ScrollView.
  • 1:39 - 1:44
    So let's change our search query
    to search for ScrollView android.
  • 1:44 - 1:48
    Awesome.
    So it is a view in Android.
  • 1:48 - 1:50
    And there's a description here.
  • 1:50 - 1:53
    There's a lot of text here, so
    I'm going to skip reading it, but
  • 1:53 - 1:54
    if you want to skim it, you can.
  • 1:54 - 1:57
    Then I'm going to hit Back.
  • 1:57 - 1:59
    I prefer looking for a code example.
  • 1:59 - 2:02
    So, the code examples that
    we had earlier were good,
  • 2:02 - 2:04
    and this one is good as well.
  • 2:04 - 2:05
    So let's go back to Android Studio.
  • 2:06 - 2:10
    All of those XML examples
    started with ScrollView.
  • 2:10 - 2:13
    Immediately there's an error that says
    I don't have the required width and
  • 2:13 - 2:14
    height of the view.
  • 2:14 - 2:18
    So, I just added those in and
    specified them to be match_parent.
  • 2:19 - 2:22
    If I add a closing bracket, then I
    immediately get the closing tag here.
  • 2:22 - 2:24
    So, I'm going to copy that and
  • 2:24 - 2:30
    then paste it at the bottom
    of this file down here.
  • 2:31 - 2:36
    That means the existing linear layout
    is now a child of this ScrollView.
  • 2:36 - 2:41
    I'm going to select all of this and
    then try to indent it by four spaces, so
  • 2:41 - 2:44
    it's easier to read as
    a child of this ScrollView.
  • 2:45 - 2:49
    In Android Studio, if I click on the
    Edit menu, I can go to Indent Selection,
  • 2:49 - 2:52
    and that will indent it
    over by four spaces.
  • 2:52 - 2:53
    There.
    Now that looks better.
  • 2:54 - 2:55
    Oh, but I still have an error here.
  • 2:55 - 2:59
    It says,
    namespace 'android' is not bound.
  • 2:59 - 3:00
    I can click for more information.
  • 3:00 - 3:06
    It says, this inspection checks for
    unbound namespace prefixes in XML.
  • 3:06 - 3:08
    If you remember way back in lesson one,
  • 3:08 - 3:12
    I said that Android colon comes
    from this name space right here.
  • 3:12 - 3:16
    So I think we need to copy this and
    then put it up here.
  • 3:16 - 3:21
    You should also move
    the tools name space up here.
  • 3:21 - 3:25
    The tools name space is to provide
    additional attributes that you can add
  • 3:25 - 3:28
    to your XML to help you debug and
    build your app.
  • 3:28 - 3:31
    Okay, I'm going to run
    this on my device now.
  • 3:31 - 3:35
    Okay, there's no visible difference
    to the app because there's not enough
  • 3:35 - 3:38
    content in the order form to
    make it need to be scrollable.
  • 3:38 - 3:41
    To really test this,
    I need to add more views to this layout.
  • 3:41 - 3:44
    I'm going to do same trick as before.
  • 3:44 - 3:47
    I'm going to copy all these views and
    then paste it into the layout so
  • 3:47 - 3:49
    that we have a really long order form.
  • 3:50 - 3:54
    Okay, now the order form is
    really long and it scrolls.
  • 3:54 - 3:56
    That's exciting.
  • 3:56 - 3:59
    If I rotate the device,
    then it also scrolls.
  • 4:01 - 4:04
    Now I can undo those changes and
    go back to our original order form.
  • 4:05 - 4:07
    I'm still going to leave
    the ScrollView in, though,
  • 4:07 - 4:09
    because our app will work
    better across all devices.
  • 4:11 - 4:13
    For users with devices that
    are shorter than this,
  • 4:13 - 4:16
    they'll still be able to scroll and
    see all the fields.
タイトル:
Scrolling Along - Solution
概説:

13-08 Scrolling Along - Solution

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
UD837 - Android for Beginners
Duration:
04:17

English subtitles

改訂 Compare revisions