GlassSettings is an easy to use library for the Google Glass GDK that let's you build a simple preference Activity.
There are ready-made preferences for toggles and for multiple-choice items, but you can easily add your own by simply extending the AbstractPreference
class.
Written by Victor Kaiser-Pendergrast
You can see a demo application here. All of the interesting code takes place in MainActivity
Steps to use
- Create an Activity that extends
GlassPreferenceActivity
- In
OnCreate
, add as many preferences as you want withaddTogglePreference
,addChoicePreference
,addActivityPreference
,addHeadTiltPreference
or useaddPreference
with your own custom preference (that extendsAbstractPreference
). - Call
buildAndShowOptions()
at the end ofonCreate
to show all your preferences
GlassSettings provides three Preference types that are completely ready for use with no additional setup:
- TogglePreference
- A simple Preference that can be on or off
- ChooserPreference
- A Preference that gives the user a list to choose from
- Lists of options can be easily assembled with the
OptionsBuilder
class (see the Demo source)
- HeadTiltPreference
-
A Preference that uses the accelerometer to let the user pick the amount of up/down tilt of Glass
-
Note that this Preference does not store an angle, but rather acceleration in meters/second2 in Glass's Z axis
-
In order to use this Preference, you will have to define an Activity in AndroidManifest.xml:
<activity android:enabled="true" android:exported="true" android:name="com.victor.kaiser.pendergrast.settings.types.activity.HeadTiltPreferenceActivity" android:label="@string/title_activity_glass_preference" > </activity>
In addition to those three completely packaged Preferences, there are two additional that let custom experiences be created
- ActivityPreference
- Launches an Activity when tapped
- The Preference's key is passed in as an extra with the key
"preference_key"
, and can be retrieved withgetIntent().getExtras().getString("preference_key")
- By extending
AbstractPreferenceActivity
, there are built in methods to get and put values intoSharedPreferences
, play click/success sounds, and get the preference key (see here) HeadTiltPreferenceActivity
extendsAbstractPreferenceActivity
, so that may be helpful to look at
- AbstractPreference
- Used to create completely custom Preference functionality
- See below for how to use
There are two kinds of preference that you can make:
- A preference that handles everything when it is tapped (See TogglePreference and ActivityPreference)
- A preference that shows a series of choices when it is tapped (See ChooserPreference)
- Create a class that extends
AbstractPreference
- Create constructors
- Override
getCard()
. This is where you will create theView
for your preference.getCard()
is called every time GlassSettings thinks that your preference'sView
might have to be updated - Override
onSelect()
:- If you're building something like TogglePreference, then add all your functionality here and return
true
(indicating that everything is done). You can leavegetOptions()
andonOptionsItemSelected()
alone - If you're building something more like ChooserPreference, in
onSelect()
returnfalse
and add functionality togetOptions()
andonOptionsItemSelected(int)
- The
List
returned bygetOptions()
will be used to populate anOptionsMenu
- If an item is selected in the
OptionsMenu
, thenonOptionsItemSelected(int)
will be called with the index of the selected item
- The
- If you're building something like TogglePreference, then add all your functionality here and return
That's all there is to it!
If you have any questions, comments, or suggestions, feel free to reach me at v.kaiser.pendergrast@gmail.com
This library is still in a very early stage, so expect to see frequent updates with more kinds of preferences and additional functionality (perhaps the ability to define preference screens in XML).
Toggle Preference Unchecked:
Toggle Preference Checked:
Choice Preference:
Choice Preference Tapped:
Choice Preference After Selection: