Adding a settings page to Note to Self

Now that we are armed with all this knowledge about the Android Intent class, we can add another screen (Activity) to our Note to Self app – a Settings screen.

We will first create a new Activity for our Settings screen and see what effect that has on the AndroidManifest.xml file. We will then create a very simple layout for our Settings screen and add the Java code to switch from MainActivity to the new one. We will, however, defer wiring up our Settings screen with Java until we have learned how to save the settings to disk. We will do this later this chapter and then come back to the Settings screen to make it persist.

First, let's create that new Activity. We will call it SettingsActivity.

Creating the SettingsActivity

This will be a screen where the user can turn the decorative divider between each note in the RecyclerView on or off. This will not be the most comprehensive settings screen, but it will be a useful exercise, and we will learn how to switch between activities as well as save data to disk. Follow these steps to get started:

  1. In the project explorer, right-click the folder that contains all your .java files and has the same name as your package. From the pop-up context menu, select New | Activity | Empty Activity.
  2. In the Activity Name: field, enter SettingsActivity.
  3. Leave all the other options at their defaults and left-click Finish.

Android Studio has created a new Activity for us and its associated .java file. Let's take a quick peek at some of the work that was done behind the scenes for us, because it is useful to know what is going on.

Open the AndroidManifest.xml file from within the manifests folder in the project explorer. Notice the following few lines of code near the end of this file:

<activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:theme="@style/AppTheme.NoActionBar">
</activity>

This is how an Activity is registered with the operating system. If an Activity is not registered, then an attempt to run it will crash the app. We could create an Activity simply by creating a class that extends Activity (or AppCompatActivity) in a new .java file. However, we would then have had to add the preceding code ourselves. Also, by using the new Activity wizard, we got a layout XML file (activity_settings.xml and content_settings.xml) automatically generated for us.

Designing the Settings screen layout

We will quickly build a user interface for our settings screen; the following steps and screenshot should make this straightforward:

  1. Open the activity_settings.xml file, switch to the Design tab, and we will quickly lay out our settings screen.
  2. Delete the floating action button (fab) that was put in automatically.
  3. Switch to SettingsActivity.java and remove all the code related to the just-deleted floating action button. This is to avoid errors when we run the app. For clarity, here is the code you need to delete (formatted slightly differently):
    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();
       }
       
    });
  4. Switch to the content_settings.xml file and again make sure that you are in the design view.
  5. Use the following screenshot as a guide while following the rest of these steps:
    Designing the Settings screen layout
  6. Drag and drop a Switch onto the center-top of the layout. I stretched mine by dragging the edges to make it nice and clear.
  7. Add an id attribute of switch1 (if it isn't there already) so that we can interact with it using Java.
  8. Use the constraint handles to fix the position of the switch or click the Infer Constraints button to fix it automatically.

We now have a nice (and very simple) new layout for our settings screen and the id properties are in place, ready for when we wire it up with our Java code later in this chapter.

Enabling the user to switch to the settings screen

We already know how to switch to SettingsActivity. Also, as we won't be passing any data to it or from it, we can get this working with just two lines of Java.

You might have noticed in the action bar of our app that there is the menu icon. It is shown in the next screenshot:

Enabling the user to switch to the settings screen

If you tap it, there will already be a menu option in there for Settings. This was provided by default when we first created the app. This is what you will see when you tap the menu icon:

Enabling the user to switch to the settings screen

All we need to do is place our code to switch to SettingsActivity within the onOptionsItemSelected method in the MainActivity.java file. Android Studio even provides an if block by default for us to paste our code into, on the assumption that we would one day want to add a settings menu. How thoughtful.

Switch to MainActivity.java in the editor window and find the following block of code in the onOptionsItemSelected method:

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
   return true;
}

Add this code into the if block shown previously, just before the return true statement:

Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);

Tip

You will need to import the Intent class using your preferred technique to add this line of code:

import android.content.Intent;

You can now run the app and visit the new Settings screen by tapping the Settings menu option. This screenshot shows the Settings screen running on the emulator:

Enabling the user to switch to the settings screen

To return from SettingsActivity to MainActivity, you can tap the back button on the device.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.140.108