Mobile App Development: Strawberry Logs - Android Assignment 1
VerifiedAdded on  2019/09/26
|9
|4524
|331
Homework Assignment
AI Summary
The assignment involves developing an Android application called "Strawberry Logs" using Android Studio. The project requires creating multiple fragments for different screens, including a home screen with buttons for each strawberry and individual entry screens to log data. The solution guides through the creation of UI elements using XML layouts, implementing button click functionalities using the onClick method in MainActivity, and managing fragment transactions. It covers UI design, fragment management, handling button clicks, data storage, and navigation between different screens using fragments. The solution also explains how to store data, handle user input, and display information to the user. The assignment emphasizes using fragments, UI elements, and data storage techniques, requiring students to create UI designs, handle button clicks, and store data efficiently. The project also includes handling user input and displaying information. The solution also details how to save and retrieve the entered data using various methods and classes.

Assignment 1 – hints 1
The first assignment is rather long and you will not be able to complete it if you start working on it in
week 5. Hence each week a series of instruction steps has been written to help you complete the
assignment. This week you should:
1 In Android Studio open a new Android application project and fill in the first screen as shown.
Click Next on this and then select the Android 4.0 SDK (API 14) as the minimum SDK as shown in the
next figure. Click Next and go to the create activity screen where you select the Empty activity as
shown. Click next and then Finish. Your StrawberryLogs project will then be added to the projects list
in Android Studio and the project will open.
2 Then we add a few fragments to the project. In particular we are going to add a fragment for the
startup page (with the buttons for the strawberries; Strawberry 1, Strawberry 2, Strawberry 3,
Strawberry 4, Strawberry 5) and a fragment for one of the strawberry entry data. We will start
with the first fragment, call it the Home_fragment.java. Then add the .xml file describing its
resources (in the layout directory), lets call this home_fragment.xml. You can do this a number
of ways, the simplest is to copy and paste the existing activity_main file and rename it
home_fragment. For now leave the content alone.
The first assignment is rather long and you will not be able to complete it if you start working on it in
week 5. Hence each week a series of instruction steps has been written to help you complete the
assignment. This week you should:
1 In Android Studio open a new Android application project and fill in the first screen as shown.
Click Next on this and then select the Android 4.0 SDK (API 14) as the minimum SDK as shown in the
next figure. Click Next and go to the create activity screen where you select the Empty activity as
shown. Click next and then Finish. Your StrawberryLogs project will then be added to the projects list
in Android Studio and the project will open.
2 Then we add a few fragments to the project. In particular we are going to add a fragment for the
startup page (with the buttons for the strawberries; Strawberry 1, Strawberry 2, Strawberry 3,
Strawberry 4, Strawberry 5) and a fragment for one of the strawberry entry data. We will start
with the first fragment, call it the Home_fragment.java. Then add the .xml file describing its
resources (in the layout directory), lets call this home_fragment.xml. You can do this a number
of ways, the simplest is to copy and paste the existing activity_main file and rename it
home_fragment. For now leave the content alone.
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

3 Add the Home_fragment java class to the src/com.example.StrawberryLogs directory (that
contains MainActivity.java) to support the resource. This class extends the Fragment class and has a
single onCreateView() method that inflates the home_fragment, as discussed for other fragments in
the textbook.
4 You will need to modify MainActivity to show the home_fragment when the activity is created.
You use the FragmentManger class to do this. In the MainActivity's onCreate() method add
the following lines of code immediately after the call to super.onCreate(). This will make the
fragment in the replace() method the visible fragment when the commit() call executes.
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction
fragmentTransaction=fragmentManager.beginTransaction();
Home_fragment hf=new Home_fragment();
fragmentTransaction.replace(android.R.id.content, hf);
fragmentTransaction.commit();
Run the project and test the fragment appears and looks as it should in the emulator. Fix any
problems before proceeding.
5 We could add the fragments for each of the strawberry's' log entries but this requires 5
strawberry and 5 data xml files where their is very little difference in the code - basically just the
strawberry name changes. There should be a more efficient way to do this and there is. We will
defer discussion of this until next week, where we cover some needed theory.
Assignment 1 – hints 2
1 Last week you added a fragment called Home_fragment.java to your project. This week we create
the UI for the fragment. Start by creating the home_fragment.xml xml file (by copying and editing a
working example) and edit it to correspond to the desired look. Use a RelativeLayout for the parent
resource and add 5 buttons, one for each strawberry. They should have the text on them from the
assignment spec and should have an android:onClick tag with a method name. We use the same
method name for each of the strawberry's' pages' onClick callback as we are going to appear to use a
single strawberry page for each strawberry. We see how this works next week.
Add an android:tag="#" where the # is a different integer for each button. Add the Previous, Next and
Home buttons to the bottom of the page and place them together in a horizontal LinearLayout
(enclosed in the parent RelativeLayout) so we will need to position it in its parent. Give the buttons
equal weights so they appear in line.
2 Before you click on any of the buttons on the Home_fragment you have to create the strawberry page
to go to, or your app will crash. Create a Strawberry_fragment.java file (that extends the Fragment
class) and a strawberry_fragment.xml file for its UI (user interface).
In the strawberry_fragment.xml use a RelativeLayout so you can postion the items with respect to
each other. At the top place a TextView with the text "Strawberry 1" as the text and use
android:textsize = "32dp" to create slightly larger text.
Within the parent RelativeLayout put the first label TextView (ID:) with android: layout_below to set
the TextView below the top TextView (Strawberry 1). The EditText is put below its label TextView
(ID:), again using android: layout_below.
contains MainActivity.java) to support the resource. This class extends the Fragment class and has a
single onCreateView() method that inflates the home_fragment, as discussed for other fragments in
the textbook.
4 You will need to modify MainActivity to show the home_fragment when the activity is created.
You use the FragmentManger class to do this. In the MainActivity's onCreate() method add
the following lines of code immediately after the call to super.onCreate(). This will make the
fragment in the replace() method the visible fragment when the commit() call executes.
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction
fragmentTransaction=fragmentManager.beginTransaction();
Home_fragment hf=new Home_fragment();
fragmentTransaction.replace(android.R.id.content, hf);
fragmentTransaction.commit();
Run the project and test the fragment appears and looks as it should in the emulator. Fix any
problems before proceeding.
5 We could add the fragments for each of the strawberry's' log entries but this requires 5
strawberry and 5 data xml files where their is very little difference in the code - basically just the
strawberry name changes. There should be a more efficient way to do this and there is. We will
defer discussion of this until next week, where we cover some needed theory.
Assignment 1 – hints 2
1 Last week you added a fragment called Home_fragment.java to your project. This week we create
the UI for the fragment. Start by creating the home_fragment.xml xml file (by copying and editing a
working example) and edit it to correspond to the desired look. Use a RelativeLayout for the parent
resource and add 5 buttons, one for each strawberry. They should have the text on them from the
assignment spec and should have an android:onClick tag with a method name. We use the same
method name for each of the strawberry's' pages' onClick callback as we are going to appear to use a
single strawberry page for each strawberry. We see how this works next week.
Add an android:tag="#" where the # is a different integer for each button. Add the Previous, Next and
Home buttons to the bottom of the page and place them together in a horizontal LinearLayout
(enclosed in the parent RelativeLayout) so we will need to position it in its parent. Give the buttons
equal weights so they appear in line.
2 Before you click on any of the buttons on the Home_fragment you have to create the strawberry page
to go to, or your app will crash. Create a Strawberry_fragment.java file (that extends the Fragment
class) and a strawberry_fragment.xml file for its UI (user interface).
In the strawberry_fragment.xml use a RelativeLayout so you can postion the items with respect to
each other. At the top place a TextView with the text "Strawberry 1" as the text and use
android:textsize = "32dp" to create slightly larger text.
Within the parent RelativeLayout put the first label TextView (ID:) with android: layout_below to set
the TextView below the top TextView (Strawberry 1). The EditText is put below its label TextView
(ID:), again using android: layout_below.

Repeat this process for the other lines shown for entering the other data (the Weight, Sunlight, Compost
and Water lines) as detailed in the assignment specification. At the bottom of the fragment is the line of
three buttons - Previous, Next and Home as on the Home_fragment. Complete the required entries.
3 You need to modify the MainActivity.java so that it displays the strawberry_fragment instead of
the default in its onCreate() method. But before we do this we need to get some file naming issues
sorted out. To get the MainActivity.java to display the new Home fragment you created locate the
setContentView(R.layout.activity_main); line in the ActivityMain.java onCreate() method and
replace it with the following lines:
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction
fragmentTransaction=fragmentManager.beginTransaction();
Home_fragment hf=new Home_fragment();
fragmentTransaction.replace(android.R.id.content, hf);
fragmentTransaction.commit();
Do not forget to import the required libraries to use the FragmentManager class.
The Strawberry_fragment.java file is to be run to inflate the strawberry_fragment.xml file when the
user presses a strawberry name button on the home page or when the Previous or Next buttons are used
to navigate to the page. Let's assume the Strawberry 1's buttons click method, onClick, is called by
clicking on the Strawberry 1 button on the home page. We add a onClick() method to our
MainActivity's java file to handle this click. But we need to know which buttons' onClick() was called.
To do this use the following onClick() method:
public void onClick(View view)
{
//Retrieve index from view's tag
currentPage = Integer.valueOf((String)view.getTag());
showCurrentPage();
}
currentPage is declared as an int in the main activity java class. You use it to keep track of what
strawberry you are entering data for. showCurrentPage() is a method you declare in your main activity
to display the strawberry's page. Let's assume you use integers 0 to 5 to represent the fragments in the
activity where 0 represents the Home_fragment and 1 to 5 represent the 5 strawberry fragments. If
currentPage is 0, this then means show the Home_fragment using:
FragmentTransaction ft = getFragmentManager().beginTransaction();
Home_fragment hf = new Home_fragment();
ft.replace(android.R.id.content, hf);
ft.commit();
Otherwise we need to display the strawberry fragment. We will need to pass the number of the
strawberry to the Strawberry_fragment so we can customise the fragment to the strawberry. We do
this by using a Bundle object and the setArguments method of the fragment class as follows:
Fragment frag = new Chicken_fragment();
//Communicate the strawberry number to the fragment
Bundle args = new Bundle();
args.putInt("strawberry", currentPage);
frag.setArguments(args);
FragmentTransaction ft=getFragmentManager().beginTransaction();
and Water lines) as detailed in the assignment specification. At the bottom of the fragment is the line of
three buttons - Previous, Next and Home as on the Home_fragment. Complete the required entries.
3 You need to modify the MainActivity.java so that it displays the strawberry_fragment instead of
the default in its onCreate() method. But before we do this we need to get some file naming issues
sorted out. To get the MainActivity.java to display the new Home fragment you created locate the
setContentView(R.layout.activity_main); line in the ActivityMain.java onCreate() method and
replace it with the following lines:
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction
fragmentTransaction=fragmentManager.beginTransaction();
Home_fragment hf=new Home_fragment();
fragmentTransaction.replace(android.R.id.content, hf);
fragmentTransaction.commit();
Do not forget to import the required libraries to use the FragmentManager class.
The Strawberry_fragment.java file is to be run to inflate the strawberry_fragment.xml file when the
user presses a strawberry name button on the home page or when the Previous or Next buttons are used
to navigate to the page. Let's assume the Strawberry 1's buttons click method, onClick, is called by
clicking on the Strawberry 1 button on the home page. We add a onClick() method to our
MainActivity's java file to handle this click. But we need to know which buttons' onClick() was called.
To do this use the following onClick() method:
public void onClick(View view)
{
//Retrieve index from view's tag
currentPage = Integer.valueOf((String)view.getTag());
showCurrentPage();
}
currentPage is declared as an int in the main activity java class. You use it to keep track of what
strawberry you are entering data for. showCurrentPage() is a method you declare in your main activity
to display the strawberry's page. Let's assume you use integers 0 to 5 to represent the fragments in the
activity where 0 represents the Home_fragment and 1 to 5 represent the 5 strawberry fragments. If
currentPage is 0, this then means show the Home_fragment using:
FragmentTransaction ft = getFragmentManager().beginTransaction();
Home_fragment hf = new Home_fragment();
ft.replace(android.R.id.content, hf);
ft.commit();
Otherwise we need to display the strawberry fragment. We will need to pass the number of the
strawberry to the Strawberry_fragment so we can customise the fragment to the strawberry. We do
this by using a Bundle object and the setArguments method of the fragment class as follows:
Fragment frag = new Chicken_fragment();
//Communicate the strawberry number to the fragment
Bundle args = new Bundle();
args.putInt("strawberry", currentPage);
frag.setArguments(args);
FragmentTransaction ft=getFragmentManager().beginTransaction();
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

ft.replace(android.R.id.content, frag).commit();
We also add support for a click on the Home, Previous and Next buttons (each with a different
android:onClick method name) on the Home page by increment or decrementing the current
page value in these click handlers, and incorporating some conditions into the start of
showCurrentPage() to handle the wrap from 5 to 0 or 0 to 5 when current page is
incremented/decremented so the Strawberry 5 page will take as back to the Strawberry 1 and
Strawberry 1 goes back to Strawberry 5 when Previous is clicked.
4 Finally open the activity_main xml file in layout directory and change the layout to a FrameLayout.
Add Fragments for each of the fragments you have created.
5 At this stage you should be able to click on the buttons on the home page to go to the strawberry page,
but the strawberry pages will all show Strawberry 1 as the page title as this is the text in the
strawberry_fragment.xml file. We will fix this next week. You should also be able to navigate
the Next, Home and Previous buttons on the bottom of the view.
Assignment 1 – hints 3
A number of changes need to be made to the assignment.
1. In the TextView at the top of the RelativeLayout in the strawberry_fragment.xml file take
note of the android:id you use, you will need it. Also note the android:text value has to be a
string like "Strawberry 1".
2. We fix the android:text's incorrect value in the Strawberry_fragment's onCreateView()
method before the view is rendered. After the view's inflater.inflate() method has been called
we can used the argument passed to the fragment, when the fragment was created in
Main_Activity, to set the TextView's label correctly as follows:
int strawberry = getArguments().getInt("strawberry");
//Modify label
TextView strawberryLabel =
(TextView)view.findViewById(R.id.strawberryId);
strawberryLabel.setText(MainActivity.pageNames[strawberry-1]);
pageNames is an array of the strings used for the fields, i.e. "Strawberry 1", "Strawberry 2",
Strawberry 3", "Strawberry 4", "Strawberry 5". Note there is an off by one problem that is easily
fixed by subtracting 1.
3. The strawberry_list.xml file consists of a ListView and a button used to return to the
strawberry page. This resource file has a number of EditText views, add the specified
android:hint to these. The hints will appear in the field and be replaced by whatever the user
types in the field.
4. The Strawberry_fragment's resource files has two other Button views, one for handling a click
on the Save log entry and the other for Show log entries, add them. Also add the
android:onClick event labels to the xml file.
5. Add an onStart() method to the Strawberry_fragment. Use the onStart() methods
findViewById() to find the label for the Save log entries button in the pages view. Using the
reference to the button attach a setOnClickListener() method to the Save log entries button. To
We also add support for a click on the Home, Previous and Next buttons (each with a different
android:onClick method name) on the Home page by increment or decrementing the current
page value in these click handlers, and incorporating some conditions into the start of
showCurrentPage() to handle the wrap from 5 to 0 or 0 to 5 when current page is
incremented/decremented so the Strawberry 5 page will take as back to the Strawberry 1 and
Strawberry 1 goes back to Strawberry 5 when Previous is clicked.
4 Finally open the activity_main xml file in layout directory and change the layout to a FrameLayout.
Add Fragments for each of the fragments you have created.
5 At this stage you should be able to click on the buttons on the home page to go to the strawberry page,
but the strawberry pages will all show Strawberry 1 as the page title as this is the text in the
strawberry_fragment.xml file. We will fix this next week. You should also be able to navigate
the Next, Home and Previous buttons on the bottom of the view.
Assignment 1 – hints 3
A number of changes need to be made to the assignment.
1. In the TextView at the top of the RelativeLayout in the strawberry_fragment.xml file take
note of the android:id you use, you will need it. Also note the android:text value has to be a
string like "Strawberry 1".
2. We fix the android:text's incorrect value in the Strawberry_fragment's onCreateView()
method before the view is rendered. After the view's inflater.inflate() method has been called
we can used the argument passed to the fragment, when the fragment was created in
Main_Activity, to set the TextView's label correctly as follows:
int strawberry = getArguments().getInt("strawberry");
//Modify label
TextView strawberryLabel =
(TextView)view.findViewById(R.id.strawberryId);
strawberryLabel.setText(MainActivity.pageNames[strawberry-1]);
pageNames is an array of the strings used for the fields, i.e. "Strawberry 1", "Strawberry 2",
Strawberry 3", "Strawberry 4", "Strawberry 5". Note there is an off by one problem that is easily
fixed by subtracting 1.
3. The strawberry_list.xml file consists of a ListView and a button used to return to the
strawberry page. This resource file has a number of EditText views, add the specified
android:hint to these. The hints will appear in the field and be replaced by whatever the user
types in the field.
4. The Strawberry_fragment's resource files has two other Button views, one for handling a click
on the Save log entry and the other for Show log entries, add them. Also add the
android:onClick event labels to the xml file.
5. Add an onStart() method to the Strawberry_fragment. Use the onStart() methods
findViewById() to find the label for the Save log entries button in the pages view. Using the
reference to the button attach a setOnClickListener() method to the Save log entries button. To
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

do this you need to save the Strawberry's strings contained in the EditText fields on the page.
There will be many such logs so we need an array of objects. The array will need to save the
strawberry's' data such as, the Weight, Compost, Sunlight and Water values. This suggests
creating a class to hold this data.
In the class we define a constructor for the class and utility getter() and setter() methods for the
class. The constructor should contain a version that takes the six arguments (including the
strawberry number and date/time) that are required. In the assignment the class name is given as
strawberryLogs.
You will need a publicly accessible variable to store your logs, and you need a simple way of
combining the logs in a container structure. Hence declare a static ArrayList<strawberryLogs>
variable in your main activity class. In your MainActivity classes' public constructor set your
variable to a new ArrayList<strawberryLogs>() to initialise it. You may also like to set your
currentPage to its default value (that used for the Home_fragment) as well.
In the onClick() callback use findViewById() to get a reference to an EditText field and use
this to get the text of the field and save it as a local string. Use parseInt() and parseFloat() to
try to convert the strings. If the conversion fails set the android:text of the EditText to the hint
string and give a Toast message about the error. If it doesn't fail you have the values of the
correct type as a local variable. Repeat this for all four TextEdit fields. If the conversion process
does not fail you can then use getArguments().getInt("strawberry") to find the strawberry
value and then construct a new strawberryLogs() variable (eg dl) using the required data to be
logged. You need to add this new strawberryLogs value to the ArrayList variable in your
MainActivity. If your ArrayList<strawberryLogs> instance is called entries then the
reference to the variable in the other package is something like
com.example.strawberryLogs.MainActivity.entries.add(dl). Here we use the .add() method
of ArrayList to add the new strawberryLogs to the ArrayList in your setOnClickListener()
method.
6. When the Save log entries onClick() method is called you will also need to create a time string to add
to the data. To get the Time use a Calendar object (Google it) to get the current date. From the
Calendar objects' instance extract the day, month, year, hour, min and sec values as int's of the
Strawberry_fragment class. Then create a local string in onClick(). Add the day, month and
year values, converted to strings, and separated by the "/" character. Finally append the hour,
minute and seconds to the string using the ":" character, as shown in the assignment specification
for the field's appearance when a time is displayed on the Show logs page. Use
Calendar.HOUR_OF_DAY and Calendar.MINUTE etc., to get the values.
7. The Show log entries button requires its own UI and fragment. Add a click handler in the
MainActivity java file so that a click on the Save Log Entries button in the
Strawberry_fragment page causes a Strawberry_list_fragment to be made visible using the
FragmentManager. This is the same as that given for the example for the
Strawberry_Fragment page given at the end of week 3. You only need to change the fragment
name to Strawberry_list_fragment.
Create these objects in the strawberry_list xml file. Create a Strawberry_list_fragment that
extends the ListFragment class. In the onCreateView() method inflate() the strawberry_list
resource.
There will be many such logs so we need an array of objects. The array will need to save the
strawberry's' data such as, the Weight, Compost, Sunlight and Water values. This suggests
creating a class to hold this data.
In the class we define a constructor for the class and utility getter() and setter() methods for the
class. The constructor should contain a version that takes the six arguments (including the
strawberry number and date/time) that are required. In the assignment the class name is given as
strawberryLogs.
You will need a publicly accessible variable to store your logs, and you need a simple way of
combining the logs in a container structure. Hence declare a static ArrayList<strawberryLogs>
variable in your main activity class. In your MainActivity classes' public constructor set your
variable to a new ArrayList<strawberryLogs>() to initialise it. You may also like to set your
currentPage to its default value (that used for the Home_fragment) as well.
In the onClick() callback use findViewById() to get a reference to an EditText field and use
this to get the text of the field and save it as a local string. Use parseInt() and parseFloat() to
try to convert the strings. If the conversion fails set the android:text of the EditText to the hint
string and give a Toast message about the error. If it doesn't fail you have the values of the
correct type as a local variable. Repeat this for all four TextEdit fields. If the conversion process
does not fail you can then use getArguments().getInt("strawberry") to find the strawberry
value and then construct a new strawberryLogs() variable (eg dl) using the required data to be
logged. You need to add this new strawberryLogs value to the ArrayList variable in your
MainActivity. If your ArrayList<strawberryLogs> instance is called entries then the
reference to the variable in the other package is something like
com.example.strawberryLogs.MainActivity.entries.add(dl). Here we use the .add() method
of ArrayList to add the new strawberryLogs to the ArrayList in your setOnClickListener()
method.
6. When the Save log entries onClick() method is called you will also need to create a time string to add
to the data. To get the Time use a Calendar object (Google it) to get the current date. From the
Calendar objects' instance extract the day, month, year, hour, min and sec values as int's of the
Strawberry_fragment class. Then create a local string in onClick(). Add the day, month and
year values, converted to strings, and separated by the "/" character. Finally append the hour,
minute and seconds to the string using the ":" character, as shown in the assignment specification
for the field's appearance when a time is displayed on the Show logs page. Use
Calendar.HOUR_OF_DAY and Calendar.MINUTE etc., to get the values.
7. The Show log entries button requires its own UI and fragment. Add a click handler in the
MainActivity java file so that a click on the Save Log Entries button in the
Strawberry_fragment page causes a Strawberry_list_fragment to be made visible using the
FragmentManager. This is the same as that given for the example for the
Strawberry_Fragment page given at the end of week 3. You only need to change the fragment
name to Strawberry_list_fragment.
Create these objects in the strawberry_list xml file. Create a Strawberry_list_fragment that
extends the ListFragment class. In the onCreateView() method inflate() the strawberry_list
resource.

8. In the Strawberry_list_fragment's onCreateView() method you need to inflate() the View and then
change the text of the button with Return to field x with the value of x replaced by the
strawberry name.
In the onCreate() method you need to provide a single array of strings as the source for the list
in the setListAdapter() method used to fill the ListView on the page. The strings come from the
ListArray<strawberryLogs> variable in MainActivity. However, they are not in the form
required and you must unpack the strawberryLog elements to get to the required strings.
Start by declaring a iterator based on
com.example.strawberryLogs.MainActivity.entries.iterator(). While this iterator's hasNext()
value is not null, get the next value using .next(). If the values strawberry name is the same as
the current strawberry name being listed, we have found a value to display. We need to save
these values as String's and we will have many such values. Hence we declare a class variable of
type ArrayList. We can then use the ArrayList's .add() method to add the found string to the
list. For the Weight, Compost, Sunlight and Water values we convert the int/floats to string and
concatenate them to get the output shown in the assignment specification.
Finally use the ArrayList to create a String[] array as the setListAdapter() method requires a
String array not a ArrayList type. Simply iterate through the ArrayList and assign the next()
string from the ArrayList to the string array. Remember to clear the ArrayList<String> array
before using it again.
Add a click handler in MainActivity so that a click on the Return to field x button returns to the
Strawberry_fragment page.
Assignment 1 – hints 4
1. Add and implement onCreateOptionsMenu(), onOptionsItemSelected() and CreateMenu()
methods in MainActivity to implement the Profile, Save and Send menus options required in
the assignment. Next implement the MenuChoice() method that implements the users menu
choice. Complete the save and send choices next week. For this week we will concentrate on the
Profile menu.
The profile requires a fragment for its interface. Hence create a profile_fragment xml file. Use a
RelativeLayout to contain the UI elements. The "Logger username" is contained in a
TextView that is at the top of the layout. Below it is an EditText field for the user to enter their
name. Set the default text to "" and provide a hint for entering login name. Next comes the
"Password?" TextEdit field with its associated EditText field to enter the value. Next
implement the second TextView and TextExit fields for the second password. As the last items
Cancel and Save buttons are provided within a LinearLayout.
2. Create a Profile_fragment class that extends Fragment. In its onCreateView() method
inflate() the profile_fragment.
Override the profiles onStart() method to define what happens when the profile is selected from
the MenuChoice() menu on the toolbar. You need to respond to a tap on the Save or the Cancel
buttons when the fragment is displayed. Use findViewById() to get a reference to the Save
button. Then use this reference to define the setOnClickListener() for the button. In the listener
you use findViewById() to get references to the "Logger username" and the two password
EditText fields. If the two password strings are the same, and none of the fields' strings are null,
change the text of the button with Return to field x with the value of x replaced by the
strawberry name.
In the onCreate() method you need to provide a single array of strings as the source for the list
in the setListAdapter() method used to fill the ListView on the page. The strings come from the
ListArray<strawberryLogs> variable in MainActivity. However, they are not in the form
required and you must unpack the strawberryLog elements to get to the required strings.
Start by declaring a iterator based on
com.example.strawberryLogs.MainActivity.entries.iterator(). While this iterator's hasNext()
value is not null, get the next value using .next(). If the values strawberry name is the same as
the current strawberry name being listed, we have found a value to display. We need to save
these values as String's and we will have many such values. Hence we declare a class variable of
type ArrayList. We can then use the ArrayList's .add() method to add the found string to the
list. For the Weight, Compost, Sunlight and Water values we convert the int/floats to string and
concatenate them to get the output shown in the assignment specification.
Finally use the ArrayList to create a String[] array as the setListAdapter() method requires a
String array not a ArrayList type. Simply iterate through the ArrayList and assign the next()
string from the ArrayList to the string array. Remember to clear the ArrayList<String> array
before using it again.
Add a click handler in MainActivity so that a click on the Return to field x button returns to the
Strawberry_fragment page.
Assignment 1 – hints 4
1. Add and implement onCreateOptionsMenu(), onOptionsItemSelected() and CreateMenu()
methods in MainActivity to implement the Profile, Save and Send menus options required in
the assignment. Next implement the MenuChoice() method that implements the users menu
choice. Complete the save and send choices next week. For this week we will concentrate on the
Profile menu.
The profile requires a fragment for its interface. Hence create a profile_fragment xml file. Use a
RelativeLayout to contain the UI elements. The "Logger username" is contained in a
TextView that is at the top of the layout. Below it is an EditText field for the user to enter their
name. Set the default text to "" and provide a hint for entering login name. Next comes the
"Password?" TextEdit field with its associated EditText field to enter the value. Next
implement the second TextView and TextExit fields for the second password. As the last items
Cancel and Save buttons are provided within a LinearLayout.
2. Create a Profile_fragment class that extends Fragment. In its onCreateView() method
inflate() the profile_fragment.
Override the profiles onStart() method to define what happens when the profile is selected from
the MenuChoice() menu on the toolbar. You need to respond to a tap on the Save or the Cancel
buttons when the fragment is displayed. Use findViewById() to get a reference to the Save
button. Then use this reference to define the setOnClickListener() for the button. In the listener
you use findViewById() to get references to the "Logger username" and the two password
EditText fields. If the two password strings are the same, and none of the fields' strings are null,
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

save the data, and then use the FragmentManager to return to the Home_fragment. Otherwise,
use Toast to display a message to the user as to what is required and take no other action. When
the Cancel button is selected, just override its onClickListener() method to use the
FragmentManager to display the Home_fragment.
3. Create the DBHelper class given in the Lee text as a file in your project. Then download the
sqlitebrowser from sqlitebrowser (using Zip link above table) and use it to create a database table with
(for example) a numeric primary key called _id, a numeric field called Strawberry, a text field called
DateString and text fields for the Weight, Compost, Sunlight and Water values. These fields will be
used to store the log entries into the database. Save the database and copy it to your projects assets
folder.
In your MainActivities' onCreate() method create and copy the database if it does not exist (see page
287 of the Lee text for help). Next you need to write some code to copy the elements of the database to
your static ArrayList variables that are used to display the strawberry entries on the list pages. To do
this open the db and use getAllEntries() in the DBHelper class to return a Cursor object. Then loop
through the cursor objects, one element at a time, and set each strawberry's entry. Setting an entry
requires the use of the Strawberry value to pick the strawberry name, and the using c.getString()
(where c is the cursor instance) and your strawberryLogs constructor to instantiate the strawberryLog
entry (assuming your array class for strawberry values is called strawberryLog). You then add() the
strawberryLog to the relevant ArrayList<fieldLogs> object. In this manner you initialise the
strawberryLog entries from the data stored in the database. This implies you must save the
strawberryLogs to the database somewhere.
4. Inserting the ArrayList <strawberryLogs> objects into the database occurs using the Save options
menu. When saving is chosen in the MenuChoice() method create a new DBAdapter instance, open the
db using the instance, and then remove all existing entities in the database. This saves us having to sort
through determining what's changed in the database and only adding the changed elements. Instead we
can just write each strawberry's log elements to the database. As the database is small this is not
inefficient.
Create a strawberryLogs Iterator instance object that points to a strawberry's
ArrayList<strawberryLogs> instance. You will then use the DBHelper method, insertEntry() to
insert entries from the ArrayList instance, one strawberryLogs element at a time, into the db. Repeat
this for each strawberry's ArrayList and when finished close the db.
In Android Studio open the Tools->Android->Android Device monitor item from the menu, when you
have your Device connected (phone or AVD). Then expand the data tab, and the enclosed data
directory so you can see the enclosed directories. Locate your running package from the list (something
like com.example.strawberryLogs) and open it, and its Databases sub-folder. Select the database file
you want to check and then click on the Disk icon (circled in Red in the figure below) to get the device
file. Then open the file using the SQLite Database Browser you downloaded, to create the database in
the first place. You open the table and can then view the data in the table.
use Toast to display a message to the user as to what is required and take no other action. When
the Cancel button is selected, just override its onClickListener() method to use the
FragmentManager to display the Home_fragment.
3. Create the DBHelper class given in the Lee text as a file in your project. Then download the
sqlitebrowser from sqlitebrowser (using Zip link above table) and use it to create a database table with
(for example) a numeric primary key called _id, a numeric field called Strawberry, a text field called
DateString and text fields for the Weight, Compost, Sunlight and Water values. These fields will be
used to store the log entries into the database. Save the database and copy it to your projects assets
folder.
In your MainActivities' onCreate() method create and copy the database if it does not exist (see page
287 of the Lee text for help). Next you need to write some code to copy the elements of the database to
your static ArrayList variables that are used to display the strawberry entries on the list pages. To do
this open the db and use getAllEntries() in the DBHelper class to return a Cursor object. Then loop
through the cursor objects, one element at a time, and set each strawberry's entry. Setting an entry
requires the use of the Strawberry value to pick the strawberry name, and the using c.getString()
(where c is the cursor instance) and your strawberryLogs constructor to instantiate the strawberryLog
entry (assuming your array class for strawberry values is called strawberryLog). You then add() the
strawberryLog to the relevant ArrayList<fieldLogs> object. In this manner you initialise the
strawberryLog entries from the data stored in the database. This implies you must save the
strawberryLogs to the database somewhere.
4. Inserting the ArrayList <strawberryLogs> objects into the database occurs using the Save options
menu. When saving is chosen in the MenuChoice() method create a new DBAdapter instance, open the
db using the instance, and then remove all existing entities in the database. This saves us having to sort
through determining what's changed in the database and only adding the changed elements. Instead we
can just write each strawberry's log elements to the database. As the database is small this is not
inefficient.
Create a strawberryLogs Iterator instance object that points to a strawberry's
ArrayList<strawberryLogs> instance. You will then use the DBHelper method, insertEntry() to
insert entries from the ArrayList instance, one strawberryLogs element at a time, into the db. Repeat
this for each strawberry's ArrayList and when finished close the db.
In Android Studio open the Tools->Android->Android Device monitor item from the menu, when you
have your Device connected (phone or AVD). Then expand the data tab, and the enclosed data
directory so you can see the enclosed directories. Locate your running package from the list (something
like com.example.strawberryLogs) and open it, and its Databases sub-folder. Select the database file
you want to check and then click on the Disk icon (circled in Red in the figure below) to get the device
file. Then open the file using the SQLite Database Browser you downloaded, to create the database in
the first place. You open the table and can then view the data in the table.
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

5. Generally tidy up your code and remove redundant code to date in preparation for Assignment 1
submission next week.
Assignment 1 - final hints
Complete all steps from the previous weeks Assignment 1 sections and then there are only a few steps to
complete, before the assignment code base meets all the required specifications (if you got the steps all
correct!). The following steps are required for completion:
1. You need to add the workings of the Send all entries menu option. This section will be extended in
Assignment 2, but for now it needs to present a yes/no dialog that either deletes all entries in the
db's table or does nothing. Use an AlertDialog Builder method to create the dialog and the
"OK" and "Cancel" buttons. Use an onClickListener() to add an onClick() method to each
button. Use the code base from the previous week to remove all the database entries from the db.
Also call the clear() method on your static instances of your ArrayList variables so that the
local strawberry strawberryLog's are cleared as well, or the strawberry items will still appear on
the Strawberry_fragment pages when you go to the fragment to view them. Do not forget to
call show() as the last method call on your AlertDialog object or you will not see the dialog.
2. If the user presses the BackButton on an Android device then any changes to strawberry entries made
since the user last saved the entries to the db will be lost. This is not desirable and so the
assignment specification requires a dialog be presented asking whether the user wants to save the
current strawberry data to the database or not. To get this to work you have to be able to detect a
press on the BackButton. The recommended way to to this is to override the activities'
onBackPressed() method. So override the public void onBackPressed() method in your
MainActivity.java file. You will need a final Context local instance that points to this in
onBackPressed() so that you can use it in your onClickListener()'s onClick() method for the
required context, as it is not available in the button callback, where you will use it to create your
DBHelper instance. In the "OK" button callback you open the database, delete all entries and
then add the new entries. The code is the same as used in the menu Save entries option, so
review the code and add it to the "OK" callback. To actually get the application to quit you have
to call the finish() method in the callback as the last step. The finish() method is an Android
method so you do not write it. The "No" callback simply calls finish(). Do not forget to call
show() as the last method call on your AlertDialog object or you will not see the dialog.
submission next week.
Assignment 1 - final hints
Complete all steps from the previous weeks Assignment 1 sections and then there are only a few steps to
complete, before the assignment code base meets all the required specifications (if you got the steps all
correct!). The following steps are required for completion:
1. You need to add the workings of the Send all entries menu option. This section will be extended in
Assignment 2, but for now it needs to present a yes/no dialog that either deletes all entries in the
db's table or does nothing. Use an AlertDialog Builder method to create the dialog and the
"OK" and "Cancel" buttons. Use an onClickListener() to add an onClick() method to each
button. Use the code base from the previous week to remove all the database entries from the db.
Also call the clear() method on your static instances of your ArrayList variables so that the
local strawberry strawberryLog's are cleared as well, or the strawberry items will still appear on
the Strawberry_fragment pages when you go to the fragment to view them. Do not forget to
call show() as the last method call on your AlertDialog object or you will not see the dialog.
2. If the user presses the BackButton on an Android device then any changes to strawberry entries made
since the user last saved the entries to the db will be lost. This is not desirable and so the
assignment specification requires a dialog be presented asking whether the user wants to save the
current strawberry data to the database or not. To get this to work you have to be able to detect a
press on the BackButton. The recommended way to to this is to override the activities'
onBackPressed() method. So override the public void onBackPressed() method in your
MainActivity.java file. You will need a final Context local instance that points to this in
onBackPressed() so that you can use it in your onClickListener()'s onClick() method for the
required context, as it is not available in the button callback, where you will use it to create your
DBHelper instance. In the "OK" button callback you open the database, delete all entries and
then add the new entries. The code is the same as used in the menu Save entries option, so
review the code and add it to the "OK" callback. To actually get the application to quit you have
to call the finish() method in the callback as the last step. The finish() method is an Android
method so you do not write it. The "No" callback simply calls finish(). Do not forget to call
show() as the last method call on your AlertDialog object or you will not see the dialog.

3. Complete outstanding sections and documentation of your assignment and submit it when ready using
the Moodle course website.
the Moodle course website.
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 9

Your All-in-One AI-Powered Toolkit for Academic Success.
 +13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
© 2024  |  Zucol Services PVT LTD  |  All rights reserved.