If you need only one screen with a few settings then read this post. If you need multiple sub-screens of setting then you should use the Settings Activity Template. It divides the settings into fragments and groups. Let’s start.
- Right-click your app’s package and click New > Activity > Settings Activity. Change the title if you wish, and click the 3 dots … at the right of Hierarchical Parent to specify the parent Activity. Click Finish.
- A new resource directory will be created, xml, and 4 resource XML files.
- pref_headers.xml – is the layout for the headers, is the settings main screen. The other files are the layouts for each section.
It has 3 headers/sections, specified with the header element. Each section must have 3 child elements:
- android:fragment – specifies the xml will be loaded. first the package, the settings activity, $ that specifies a nested class, and then the nested class
- android:icon – specifies the icon
- android:title – specifies the title.
- In the java directory two class files created, AppCompatPreferenceActivity which is a helper class for the SettingsActivity. In the second class file you will find the nested classes that are used in the pref_headers.xml
- If you want to create a new section, you must :
- Create a new xml resource file that describes the section. It must have a PreferenceScreen parent element and use only preference classes for childs like CheckBoxPreference, ListPreference etc.
2. Copy one nested class from the SettingsActivity file
3. add a mew entry using the header element in the pref_headers.xml
4. update isValidFragment() method in the SettingsActivity class file.
- There have been added new strings in the strings.xml file.
You can modify those or even delete them if you change the 3 layout xml files for the sections.
- Android – Notifications
- Content Provider – Part 3 – insert, delete, update, getType
- Content Provider – Part 2 – create and query