Personal Settings - McGillFoodAnalytics/SantropolRoulantAndroid GitHub Wiki
Personal Settings
If you want to extend the personal settings, there are a few steps you must do. Suppose we have a new field in the User object of Firebase, called dogname of type String. (Recall that firebase can only take objects of type String, Number, or Boolean)
Adding field to layout.
Navigate to activity_personal_settings.xml. In the Component list, you will see a bunch of Linear Layouts called pref_groups. Under each pref_group is a title, and then a linear layout for each preference. Each preference has a title (preftitle) and an EditText field (prefedit). If you to place your new preference under an existing prefgroup, simply copy an existing preference and paste it into the group of your choice. Once it is there, open the XML and change the id, text, hint of the copied preference to have the name of your choice (in this case dogname. If we copied the preference "firstname" from prefgroup "personal", the prefinput will originally have id="@+id/prefinput_personal_firstname". We need to change this to id="@+id/prefinput_personal_dogname".
Adding field to User class.
If we want to get information from the firebase, we cast the information to a class that represents the structure inside the firebase. For this instance, we have created a User class which has every field that is present in "user" in the firebase. Now that we've added a new "dogname" String to the firebase, we need to add this new field to our User class. Simply add private String dogname;
The name of the field should be exactly as it appears in the firebase. You also need to create a getter method.
Telling PersonalSettings.java
Lastly, inside the onCreate method, you should find a try/catch block which adds an InputField to an arrayList called inputFields
. You should add a new inputField. The constructor for the inputField will take a reference to the EditText (prefinput_personal_dogname), the name of the field as it appears in the firebase ("dogname"), and the User method to get the specified field ("User.class.getDeclaredMethod("getDogname")). Note that if you are getting a date, you should make a new DateField instead of a new InputField.