User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_5

This is an old revision of the document!


Under development.

Chapter 5 notes

p. 167

If you fire up the Stopwatch app from the previous chapter, you'll see that Android Studio created a dimens.xml file is app/src/main/res/values for you:

dimens.xml
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="fab_margin">16dp</dimen>
</resources>

However, it appears that the padding values used in the RelativeLayout of content_stopwatch.xml uses literals rather than these resource values. Don't be misled: once again it's Android Studio trying to be helpful by showing you the value that the resource resolves to. Hover over each of the values and you should see something that matches the following:

content_stopwatch.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    ...

pp. 174-186

Create a new project called “Linear Layout Demo” to experiment with the changes described in this section. Call the main activity, “Send Message”. After creating the default project, you will need to convert the main RelativeLayout to a LinearLayout. One way to do this is:

  1. Open in Text mode.
  2. Replace the “Relative” text in the first tag with “Linear”. The closing tag should automatically change, but if it doesn't, change it manually.
  3. Within the LinearLayout item, add the following attribute:
    android:orientation="vertical"

    just after android:layout_height=“match_parent”.

Now you can add the new Views and other code presented upto p.186.

Additional note

After all this, you might be wondering why you don't just use the drag-and-drop UI builder on the “Design” tab. The answer is twofold:

  1. Crawl before you walk. To use the UI builder effectively, you need to know what it is doing.
  2. The UI builder only gets you so far. Typically you need to change the code that the UI builder generated for you. So you need to know what the code does.
android_learning/headfirst_android_development_notes/chapter_5.1453593776.txt.gz · Last modified: 2016/01/24 00:02 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki