Recall that you want GuestbooksActivity
to display Guestbook List Screenlet
and Entry List Screenlet. Before using these Screenlets, however, you must
prepare GuestbooksActivity
as follows:
-
Refactor the action bar so you can later set its title to the selected guestbook’s name.
-
Refactor the navigation drawer so you can later close it when a guestbook is selected in Guestbook List Screenlet.
-
Delete the
NavigationView.OnNavigationItemSelectedListener
implementation. Since Guestbook List Screenlet handles guestbook selections, you don’t needNavigationView
.
When you finish, you’ll be ready to use Guestbook List Screenlet. Note that you won’t always have to take steps like these before using Screenlets. You only do so here to fit this particular app’s design.
First, you’ll refactor the action bar.
Refactoring the Action Bar
By default, the action bar displays the activity’s name. When you use Guestbook
List Screenlet, you want the action bar to display the selected guestbook’s name
instead. You’ll enable this by modifying the code that creates the action bar.
Android Studio created this code for you in the GuestbooksActivity
class’s
onCreate
method:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
-
Remove this code. Although you could edit it, you’ll instead create a separate method that creates the action bar. Note that you don’t need to worry about the now missing
toolbar
variable inonCreate
; you’ll fix it shortly. -
Create
ActionBar
andToolbar
instance variables. This lets you refer to them anywhere in the activity. Add these variables to theGuestbooksActivity
class:Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
This requires that you import
android.support.v7.app.ActionBar
. -
Add the following
initActionBar()
method toGuestbooksActivity
:private void initActionBar() { toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); actionBar = getSupportActionBar(); actionBar.setTitle(""); }
Like the code you removed from
onCreate
, this method also creates aToolbar
and sets it as the action bar. It also sets the action bar’s title to an empty string. This prevents the activity’s title from showing in the action bar before the app can retrieve guestbooks from the portlet. -
Call
initActionBar()
inonCreate
. Place the call immediately below thesetContentView
call. The first few lines ofonCreate
should now look like this:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guestbooks); initActionBar(); ... }
Next, you’ll modify the code that controls the navigation drawer.
Refactoring the Navigation Drawer
Before you can use Guestbook List Screenlet in the navigation drawer, you must refactor the drawer’s existing code. Do so now by following these steps:
-
Currently, the navigation drawer initialization code is in the
onCreate
method. Android Studio created this code for you when you used the Navigation Drawer Activity template to createGuestbooksActivity
. Delete this code fromonCreate
:DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState();
Instead, you’ll initialize the navigation drawer in a separate method that you’ll call in
onCreate
. You’ll create this method shortly. -
You’ll also change the
drawer
variable to be an instance variable that you can refer to throughout the class. This lets you use this variable to close the drawer when a guestbook is selected in Guestbook List Screenlet. Add this variable toGuestbooksActivity
:private DrawerLayout drawer;
-
Add the following
initDrawer
method. This method’s contents match the drawer initialization code you deleted inonCreate
, except thatdrawer
is now an instance variable:private void initDrawer() { // drawer initialization drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); }
-
In the
onCreate
method, place the call toinitDrawer()
immediately below theinitActionBar
call. The first few lines ofonCreate
should now look like this:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guestbooks); initActionBar(); initDrawer(); ... }
-
Also, because you want to use the same
DrawerLayout
instance throughout the class, delete the line of code that creates a newDrawerLayout
in theonBackPressed
method. YouronBackPressed
method should now look like this:@Override public void onBackPressed() { if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } }
Now you’re ready to delete the NavigationView.OnNavigationItemSelectedListener
implementation. The next section walks you through this.
Deleting the NavigationView.OnNavigationItemSelectedListener Implementation
Since Guestbook List Screenlet handles navigation drawer item selections, you
don’t need to implement NavigationView.OnNavigationItemSelectedListener
in
GuestbooksActivity
. Follow these steps to remove this implementation:
-
Delete the implementation from the class declaration. The class declaration should now look like this:
public class GuestbooksActivity extends AppCompatActivity {...
-
Remove the code in
GuestbooksActivity
that implementsNavigationView.OnNavigationItemSelectedListener
. To do this, first delete the following code at the end of theonCreate
method:NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);
Your
onCreate
method should now look like this:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guestbooks); initActionBar(); initDrawer(); 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(); } }); }
-
Delete the
onNavigationItemSelected
method, along with its@Override
and@SuppressWarnings("StatementWithEmptyBody")
statements. -
Finally, remove the
android.support.design.widget.NavigationView
import.
Great job! Now you’re ready to insert Guestbook List Screenlet in
GuestbooksActivity
.