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
.
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:
.java
files and has the same name as your package. From the pop-up context menu, select New | Activity | Empty Activity. SettingsActivity
.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.
We will quickly build a user interface for our settings screen; the following steps and screenshot should make this straightforward:
activity_settings.xml
file, switch to the Design tab, and we will quickly lay out our settings screen.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(); } });
content_settings.xml
file and again make sure that you are in the design view.id
attribute of switch1
(if it isn't there already) so that we can interact with it using Java.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.
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:
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:
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);
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:
To return from SettingsActivity
to MainActivity
, you can tap the back button on the device.
18.118.140.108