Attachment 1
Attachment 1
// My instructions and comments are in red text – not all sections of the tutorial are
copied/pasted/annotated in this document, so have the tutorial Web pages open.
// Remember: When you see the word “class” this means Java
// Important: At least after each major section of the tutorial, try to run your project (this
will build your project) so that you catch errors early and can fix them as you go along.
Do not wait until the end of the tutorial to do a build; it will be much harder to identify
and fix errors.
// One problem I had when running the app was seeing the Toolbar with text and icons. I fixed
this by editing my layout files in Design mode, selecting the Toolbar, then in Properties:
1. Select background
Click the … to expand and see the imports. An import statement imports Android packages,
which are sets of Android class files. You can write an import statement to import a single class
file. When you expand the import statements, you will see that the AppCompatActivity class is
already imported, which is why there is no error thrown on the class statement in Step 2.
Note: Make this change for every activity in your app that uses a Toolbar as an app bar.
4. Add a Toolbar to the activity's layout. For example, the following layout code adds a Toolbar and
gives it the appearance of floating above the activity:
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
// Add this code to activity_main.xml – this code tells your app to use a Toolbar
object. When you first put this code in, you will get the following error:
// This error appears because there is no identifier for ‘app’. Do what the blue
popup tells you: Press Alt+Enter. Android Studio adds a line of code which
defines ‘app’ as an attribute to the layout’s RelativeLayout tag:
// If you run your app and the toolbar does not stretch all the way across your
screen, then in the code aobve, remove the padding attributes(these attributes add
space between widgets).
The Material Design specification recommends that app bars have an elevation of 4 dp.
Position the toolbar at the top of the activity's layout, since you are using it as an app bar.
This means the Toolbar tag and attirbutes must be before any other elements in your layout file
but remember the root tag of the layout file is the layout type (RelativeLayout) so the Toolbar
must be second in the layout file and indented one level.
5. In the activity's onCreate() method (in MainActivity.java), call the
activity's setSupportActionBar() method, and pass the activity's toolbar. This method sets the
toolbar as the app bar for the activity. For example: (add the two lines in the red box)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
When you first add these two lines, you will get an error because there is no Java
definition for the Toolbar class:
Again, press Alt+Enter – you will get popups asking what you want to import.
Import the v7 widget Toolbar class –in your import statements you should see:
import android.support.v7.widget.Toolbar;
1. The left side of the first line declares a reference for a Toolbar object
named “myToolbar”. On the right side of the = sign, a new Toolbar object is
created (instantiated) using the Toolbar definition that you provided in
your layout file. The Toolbar object is assigned to the myToolbar
reference.
2. The second line sets the Toolbar to be your ActionBar. This means that the
Toolbar will be able to use ActionBar methods (read the first two
paragraphs under Use App Bar Utility Methods).
Your app now has a basic action bar. By default, the action bar contains just the name of the app
and an overflow menu. The options menu initially contains just the Settings item. You can add more
actions to the action bar and the overflow menu, as described in Adding and Handling Actions.
</menu>
// When you first add this code you will get the following errors because: 1) Your
project does not have a set of graphics for the favorite icon you are trying to add
to the Toolbar, and 2) you do not have string definitions for your favorite action or
your settings action.
// To fix the missing graphics problem, refer to my Week 2 Instructor’s Notes, the
section on Create Different Bitmaps, and from the Material Icons page, select the
favorite icon: When adding the icons, you must name the icons the same as
in this line: android:icon="@drawable/ic_favorite_black_48dp"
You are also going to need a search icon for later in the tutorial, so go back to
The app:showAsAction attribute specifies whether the action should be shown as a button on the
app bar. If you set app:showAsAction="ifRoom" (as in the example code's favorite action), the
action is displayed as a button if there is room in the app bar for it; if there is not enough room,
excess actions are sent to the overflow menu. If you set app:showAsAction="never" (as in the
example code's settings action), the action is always listed in the overflow menu, not displayed in the
app bar.
The system uses the action's icon as the action button if the action is displayed in the app bar. You
can find many useful icons on the Material Iconspage.
Respond to Actions
When the user selects one of the app bar items, the system calls your
activity's onOptionsItemSelected() callback method, and passes a MenuItemobject to indicate
which item was clicked. In your implementation of onOptionsItemSelected(), call
the MenuItem.getItemId() method to determine which item was pressed. The ID returned matches
the value you declared in the corresponding <item> element's android:id attribute.
For example, the following code checks to see which action the user selected. If the method does
not recognize the user's action, it invokes the superclass method:
// Add the following code to your MainActivity class below the onCreate() method but
before the closing brace } of the MainActivity class.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;
case R.id.action_favorite:
// User chose the "Favorite" action, mark the current item
// as a favorite...
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
Adding an Up Action
Your app should make it easy for users to find their way back to the app's main screen.
One simple way to do this is to provide an Up button on the app bar for all activities
except the main one. When the user selects the Up button, the app navigates to the
parent activity.
This lesson shows you how to add an Up button to an activity by declaring the activity's parent in the
manifest, and enabling the app bar's Up button.
The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To
support devices with older versions of Android, define a<meta-data> name-value pair, where the
name is "android.support.PARENT_ACTIVITY" and the value is the name of the parent activity.
For example, suppose your app has a main activity named MainActivity and a single child activity.
The following manifest code declares both activities, and specifies the parent/child relationship:
// Add the code highlighted in gray to your manifest file. You will need to make
// some changes to this code; I describe the changes below.
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
First change: In the block of code below (reference to your parent activity), your
project’s package name and project name must be in front of MainActivity. If your
package name is “com.example.myfirstapp”, then leave it. You can shorten up this
little block of code to one line of code as well. The three dots must be deleted, in
both spots.
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
In my project, I have changed the block above to what you see below. Note that my
package name and project name are different, and I shortened the code to one line.
When you edit this line of code, if you get a red wavy line under it as shown above,
this means the main activity is being declared twice, which is an error. If there is
an activity element already in your application element that declares your main
activity (see block below), then delete the two lines of code above this paragraph:
Second change: The block of code below was copied straight from the tutorial and
pated into my project – note the errors. You have to hover over the ~ to see the
popup error message:
android:name="com.john.week4assignment.MyChildActivity"
Because I have not yet added the Java class MyChildActivity to the project:
1. Navigate to app/source/main/java/<your_package_name.your_project_name>
2. Right-click, New > Java Class
3. Name the class MyChildActivity, click OK
4. You should get this template and you should see the file added under
app/source/main/java/<your_package_name.your_project_name>:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_child); // Must add layout for child class
You do not need to catch the up action in the activity's onOptionsItemSelected() method. Instead,
that method should call its superclass, as shown inRespond to Actions. The superclass method
responds to the Up selection by navigating to the parent activity, as specified in the app manifest.
For example, the following code adds a SearchView widget to the app bar:
// STOP HERE