Mobile App Development Mobile App Development: Todo List Toolbar App Todo List Toolbar App
Mobile App Development Mobile App Development: Todo List Toolbar App Todo List Toolbar App
The Todo List App exercise implemented a base App with the TodoListActivity and TodoActivity
controllers and their respective fragments. This adhered to the principle of dynamically building
the view with fragment transations. It also demonstrated good practice by using a Bundle for
the todoId in the fragment rather than accessing the intent directly in the Activity; hence,
decoupling the fragment with its own argument bundle and therby making it reusable.
You could either replicate the TodoListApp or create a new git branch to the existing
TodoListApp implementation. If in doubt, then recreating the App may be good practice.
The toolbar will add an up button and a plus sign for adding new Todos as depicted in the
following images.
The Toolbar has been ported to the AppCompat library, the project default. To display
views,the AppCompat requires a theme and it provides three such themes:
Theme.AppCompat
Theme.AppCompat.light
Theme.AppCompat.light.DarkActionBar
Menu
Menu items can be defined to perform an action such as create a new todo. Similar to
creating any view, first add the string resources
Menus are a resource type and defined in XML files which reide in the res/menu
directory.
Right-mouse click on the res directory and select New > Android resource file
Change the Resource type to Menu
and the name to, fragment_todo_list
Click OK
First inflate the view and check it displays the plus icon
setHasOptionsMenu(true);
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_todo_list, menu);
}
Edit TodoListFragment java class and insert the following override method.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.new_todo:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Notice, true is returned to indicate no further processing is necessary. Run the App
and add a few new Todos.
The up button will navigate one level up in the App heirarchy to the the
TodoListActivity.
Edit the manifests/AndroidManifest.xml file and update the activity definition for the
TodoActivity with the following defintion:
<activity
android:name=".TodoActivity"
android:parentActivityName=".TodoListActivity">
</activity>
git add .
git commit -am "Toolbar complete"