User Tools

Site Tools


android_learning:headfirst_android_development_notes:chapter_5

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
android_learning:headfirst_android_development_notes:chapter_5 [2016/01/24 00:31] mithatandroid_learning:headfirst_android_development_notes:chapter_5 [2016/01/24 04:45] (current) – [Additional notes] mithat
Line 1: Line 1:
-<WRAP center round info 60%> 
-Under development. 
-</WRAP> 
- 
 ====== Chapter 5 notes ====== ====== Chapter 5 notes ======
  
Line 32: Line 28:
 </file> </file>
  
-===== pp. 174-186 ===== +===== pp. 174 ===== 
-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:+Create a new project called "Linear Layout Demo" to experiment with the changes described in this section. Call the main activity, "SendMessageActivity". After creating the default project, you will need to convert the main RelativeLayout to a LinearLayout. One way to do this is:
  
   - Open ''content_send_message.xml'' in Text mode.   - Open ''content_send_message.xml'' in Text mode.
Line 39: Line 35:
   - Within the LinearLayout item, add the following attribute: <code xml>android:orientation="vertical"</code> just after ''android:layout_height="match_parent"''.   - Within the LinearLayout item, add the following attribute: <code xml>android:orientation="vertical"</code> just after ''android:layout_height="match_parent"''.
  
-Now you can add the new Views and other code presented upto p.186+Now you can add the new Views and other code presented through p.185
  
 ===== p. 181 ===== ===== p. 181 =====
Line 47: Line 43:
 Make sure the attribute you add to the Button is **''android:layout_gravity''** and not ''android:gravity''. Make sure the attribute you add to the Button is **''android:layout_gravity''** and not ''android:gravity''.
  
-So, what's the difference between ''android:gravity'' and ''android:layout_gravity''?+<WRAP center round tip 90%> 
 +**What's the difference between\\ ''android:gravity'' and ''android:layout_gravity''?**
   * ''android:gravity'' specifies where the //View's contents// should be placed within the View.   * ''android:gravity'' specifies where the //View's contents// should be placed within the View.
-  * ''android:layout_gravity'' specifies where the //View itself// should be placed inside its parent.+  * ''android:layout_gravity'' specifies where the //View itself// should be placed within its parent.
 A Subtle, but important, distinction. A Subtle, but important, distinction.
 +</WRAP>
  
-===== Additional notes ===== +But now we have a new problem: the email icon, which is actually part of a FloatingActionBar that Android Studio adds to all default projectsis covering the Send button[[http://code2care.org/2015/how-to-remove-floating-action-button-android-studio-blank-activity-template/|This page]] at Code2care.org shows you how to get rid of the FloatingActionBar altogether by following the procedureI'm duplicating that content here in case the original goes away:
-After all thisyou might be wondering why you don't just use the drag-and-drop UI builder on the "Design" tab for everything rather than code by handThe answer is twofold: +
-  Crawl before you walk. To use the UI builder effectively, you need to know what it is doing. +
-  The UI builder only gets you so farTypically you need to change the code that the UI builder generated for youSo you need to know what the code does.+
  
 +  - In the ''{Whatever}Activity.java'' file, remove the code that creates the FloatingActionBar inside the ''onCreate'' method:<code java>
 +FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
 +fab.setOnClickListener(new View.OnClickListener() {
 + @Override
 + public void onClick(View view) {
 + Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
 + .setAction("Action", null).show();
 + }
 +});</code>
 +  - In the ''activity_{whatever}.xml'' layout file, remove the ''android.support.design.widget.FloatingActionButton'' item:<code xml>
 +<android.support.design.widget.FloatingActionButton
 + android:id="@+id/fab"
 + android:layout_width="wrap_content"
 + android:layout_height="wrap_content"
 + android:layout_gravity="bottom|end"
 + android:layout_margin="@dimen/fab_margin"
 + android:src="@android:drawable/ic_dialog_email" /></code>
 +
 +===== p. 191 =====
 +Create a new project to work with the GridLayout example. The process of converting the default RelativeLayout to a GridLayout is similar to changing it to a LinearLayout. Be sure to add an ''android:columnCount'' attribute.
 +
 +===== p. 193 =====
 +When you create the EditText for the GridLayout example, add and remove the ''android:layout_gravity'' attribute to gain an understanding of what it does.
 +
 +===== p. 205 =====
 +Create a throwaway project for this final section of the chapter and experiment with as many of the Views covered as possible.
 +
 +===== p. 215 =====
 +A good source of images that you can use without worrying about licensing is [[https://openclipart.org/|openclipart.org]]. Don't expect production-quality images though.
 +
 +Another good resource is, [[https://www.iconfinder.com/|iconfinder.com]], but you have to take care to meet the licensing requirements of each image.
 +
 +<WRAP center round tip 90%>
 +**Be careful about file names.**\\ Image file names can only have lowercase letters, numbers, and the underscore in them.
 +</WRAP>
 +
 +===== p. 219 =====
 +Your text suggest a ScrollView can be used as a root ViewGroup:
 +<file xml content_main.xml>
 +<?xml version="1.0" encoding="utf-8"?>
 +<ScrollView 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"
 +    tools:context="com.hfad.scrollviewdemo.MainActivity"
 +    tools:showIn="@layout/activity_main">
 +
 +    <LinearLayout
 +        android:layout_width="match_parent"
 +        android:layout_height="wrap_content"
 +        android:orientation="vertical">
 +
 +        <TextView
 +            android:layout_width="wrap_content"
 +            android:layout_height="wrap_content"
 +            android:text="Top!" />
 +        
 +        <TextView
 +            android:layout_width="wrap_content"
 +            android:layout_height="wrap_content"
 +            android:text="Hello World!" />
 +        ...
 +
 +        <TextView
 +            android:layout_width="wrap_content"
 +            android:layout_height="wrap_content"
 +            android:text="Bottom!" />
 +    </LinearLayout>
 +
 +</ScrollView>
 +
 +</file>
 +
 +However, in my experience this leads to unpredictable results (top of the ScrollView being obscured, etc.). A more reliable strategy seems to be nesting a ScrollView inside a root ViewGroups:
 +<file xml content_main.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"
 +    app:layout_behavior="@string/appbar_scrolling_view_behavior"
 +    tools:context="com.hfad.scrollviewdemo.MainActivity"
 +    tools:showIn="@layout/activity_main">
 +
 +    <TextView
 +        android:layout_width="wrap_content"
 +        android:layout_height="wrap_content"
 +        android:text="Outside ScrollView"
 +        android:id="@+id/textView" />
 +
 +    <ScrollView
 +        android:layout_width="match_parent"
 +        android:layout_height="match_parent"
 +        android:id="@+id/scrollView"
 +        android:layout_below="@+id/textView">
 +
 +        <LinearLayout
 +            android:layout_width="match_parent"
 +            android:layout_height="wrap_content"
 +            android:orientation="vertical">
 +            
 +            <TextView
 +                android:layout_width="wrap_content"
 +                android:layout_height="wrap_content"
 +                android:text="Top" />
 +
 +            <TextView
 +                android:layout_width="wrap_content"
 +                android:layout_height="wrap_content"
 +                android:text="Hello World!" />
 +            ...
 +
 +            <TextView
 +                android:layout_width="wrap_content"
 +                android:layout_height="wrap_content"
 +                android:text="Bottom" />
 +        </LinearLayout>
 +    </ScrollView>
 +
 +</RelativeLayout>
 +</file>
 +
 +Android Studio produces a warning stating that the ''android:layout_height'' attribute of the LinearLayout inside the ScrollView should be set to ''wrap_content'' rather than ''match_parent''.
 +
 +<WRAP center round info 90%>
 +In the examples above, I am using string literals for the text attributes---only because this is throwaway, quick-n-dirty, prove-a-concept code.
 +</WRAP>
 +
 +
 +===== Additional notes on the UI builder =====
 +If you examine the UI builder on a layout's "Design" tab, you will see that in addition to using it to add Views to your layout, you can also edit your Views' attributes.
 +
 +Given that for a lot of people pointing, clicking, dragging, and dropping is more fun than writing code, you might be wondering why we didn't just use the drag-and-drop UI builder on the "Design" tab for everything rather than coding by hand. The answer is twofold:
 +  - Crawl before you walk. To use the UI builder effectively, you need to know what it is doing.
 +  - The UI builder only gets you so far. Typically you need to change the code that the UI builder generates for you. So you need to know what the code does.
android_learning/headfirst_android_development_notes/chapter_5.1453595464.txt.gz · Last modified: 2016/01/24 00:31 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki