Overridden Activity
method onCreate
(Fig. 5.19) calls setContentView
(line 36) to set MainActivity
’s GUI. Android chooses the activity_main.xml
file from the res/layout
folder if the app is running in portrait orientation or res/layout-large-land
if the app is running on a tablet in landscape orientation.
32 @Override
33 protected void onCreate(Bundle savedInstanceState)
34 {
35 super.onCreate(savedInstanceState);
36 setContentView(R.layout.activity_main);
37
38 // set default values in the app's SharedPreferences
39 PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
40
41 // register listener for SharedPreferences changes
42 PreferenceManager.getDefaultSharedPreferences(this).
43 registerOnSharedPreferenceChangeListener(
44 preferenceChangeListener);
45
46 // determine screen size
47 int screenSize = getResources().getConfiguration().screenLayout &
48 Configuration.SCREENLAYOUT_SIZE_MASK;
49
50 // if device is a tablet, set phoneDevice to false
51 if (screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE ||
52 screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE )
53 phoneDevice = false; // not a phone-sized device
54
55 // if running on phone-sized device, allow only portrait orientation
56 if (phoneDevice)
57 setRequestedOrientation(
58 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
59 } // end method onCreate
60
When you install and launch the app for the first time, line 39 sets the app’s default preferences by calling PreferenceManager
method setDefaultValues—this creates and initializes the app’s SharedPreferences
file using the default values that you specified in preferences.xml
. The method requires three arguments:
• the preferences’ Context
—the Activity
(this
) for which you are setting the default preferences
• the resource ID for the preferences XML file (R.xml.preferences
) that you created in Section 5.4.10
• a boolean
indicating whether the default values should be reset each time method setDefaultValues
is called—false
indicates that the default preference values should be set only the first time this method is called.
Each time the user changes the app’s preferences, MainActivity
should call QuizFragment
’s methods updateGuessRows
or updateRegions
(based on which preference changed) to reconfigure the quiz. MainActivity
registers an OnSharedPreferenceChangedListener
(lines 42–44) so that it will be notified each time a preference changes. PreferenceManager
method getDefaultSharedPreferences returns a reference to the SharedPreferences
object representing the app’s preferences, and SharedPreferences
method registerOnSharedPreferenceChangeListener registers the listener, which is defined in Section 5.5.6.
Lines 47–53 determine whether the app is running on a tablet or a phone. Inherited method getResources returns the app’s Resources object (package android.content.res
) that can be used to access an app’s resources and determine information about the app’s environment. Resources
method getConfiguration returns a Configuration object (package android.content.res
) that contains public
instance variable screenLayout
, which you can use to determine the device’s screen-size category. To do so, first you combine the value of screenLayout
with Configuration.SCREENLAYOUT_SIZE_MASK
using the bitwise AND (&
) operator. Then, you compare the result to the Configuration
constants SCREENLAYOUT_SIZE_LARGE
and SCREENLAYOUT_SIZE_XLARGE
(lines 51–52). If either is a match, the app is running on a tablet-sized device. Finally, if the device is a phone, lines 57–58 call inherited Activity
method setRequestedOrientation to force the app to display MainActivity
in only portrait orientation.