70 likes | 170 Views
Lecture 8. the Preference Menu. We add some strings to the strings.xml file in the values folder. These will be used in the Settings... menu. < string name = "settings_label" > Settings... </ string > < string name = "settings_title" > Sudoku settings </ string >
E N D
Lecture 8 the Preference Menu
We add some strings to the strings.xml file in the values folder. These will be used in the Settings... menu. <stringname="settings_label">Settings...</string> <stringname="settings_title">Sudoku settings</string> <stringname="settings_shortcut">s</string> <stringname="music_title">Music</string> <stringname="music_summary">Play background music</string> <stringname="hints_title">Hints</string> <stringname="hints_summary">Show hints during play</string>
menu.xml <?xmlversion="1.0"encoding="utf-8"?> <menuxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:id="@+id/settings" android:title="@string/settings_label" android:alphabeticShortcut="@string/settings_shortcut"/> </menu> right-click the menu folder, choose New->File name the new file menu.xmland insert the following xml code...
Creating a new sub-Folder in the Resources (res) Folder <?xmlversion="1.0"encoding="utf-8"?> <menuxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:id="@+id/settings" android:title="@string/settings_label" android:alphabeticShortcut="@string/settings_shortcut"/> </menu>
Modifying the onCreateOptionsMenu( ) Method In the Sudoku.java class, there is boolean method called onCreateOptionsMenu( ) as shown here. we modify this method in the following manner... getMenuInflater( ) returns an instance of MenuInflater that we use to read the menu definition from XML and turns it into a real view. @Override publicboolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.sudoku, menu); returntrue; } @Override publicboolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); returntrue; }