Hints for completing Assignment 1 for an Android application project


Added on  2019-09-26

9 Pages4524 Words331 Views
Assignment 1 – hints 1The 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:1In 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.2Then 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.

3Add 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.4You 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.5We 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 21Last 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 andHome 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. 2Before you click on any of the buttons on the Home_fragment you have to create the strawberry pageto 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 setthe 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.3You 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.javaonCreate() 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 tagcurrentPage = 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 fragmentBundle args = new Bundle(); args.putInt("strawberry", currentPage); frag.setArguments(args); FragmentTransaction ft=getFragmentManager().beginTransaction();

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
ASP.NET Master Page Lab | Algonquin College

Design and Develop an Assembler