Assignment 1 – hints 1.

Added on - 25 Sep 2019

  • 9

    Pages

  • 4524

    Words

  • 105

    Views

  • 0

    Downloads

Trusted by +2 million users,
1000+ happy students everyday
Showing pages 1 to 3 of 9 pages
Assignment 1 – hints 1The first assignment is rather long and you will not be able to complete it if you start working on it inweek 5. Hence each week a series of instruction steps has been written to help you complete theassignment. This week you should:1In Android Studio open a new Android application project and fill in the first screen as shown.ClickNexton this and then select the Android 4.0 SDK (API 14) as the minimum SDK as shown in thenext figure. ClickNextand go to the create activity screen where you select theEmptyactivity asshown. Click next and thenFinish. YourStrawberryLogsproject will then be added to the projects listin 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 thestartup 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 startwith the first fragment, call it theHome_fragment.java. Then add the .xml file describing itsresources (in thelayoutdirectory), lets call thishome_fragment.xml. You can do this a numberof ways, the simplest is to copy and paste the existingactivity_mainfile and rename ithome_fragment. For now leave the content alone.
3Add theHome_fragmentjava class to thesrc/com.example.StrawberryLogsdirectory (thatcontainsMainActivity.java) to support the resource. This class extends theFragmentclass and has asingleonCreateView()method that inflates thehome_fragment, as discussed for other fragments inthe textbook.4You will need to modifyMainActivityto show thehome_fragmentwhen the activity is created.You use theFragmentMangerclass to do this. In theMainActivity'sonCreate() method addthe following lines of code immediately after the call tosuper.onCreate(). This will make thefragment in thereplace() method the visible fragment when thecommit() call executes.FragmentManager fragmentManager=getFragmentManager();FragmentTransactionfragmentTransaction=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 anyproblems before proceeding.5We could add the fragments for each of the strawberry's' log entries but this requires 5strawberry and 5 data xml files where their is very little difference in the code - basically just thestrawberry name changes. There should be a more efficient way to do this and there is. We willdefer discussion of this until next week, where we cover some needed theory.Assignment 1 – hints 21Last week you added a fragment calledHome_fragment.javato your project. This week we createthe UI for the fragment. Start by creating thehome_fragment.xmlxml file (by copying and editing aworking example) and edit it to correspond to the desired look. Use aRelativeLayoutfor the parentresource and add 5 buttons, one for each strawberry. They should have the text on them from theassignment spec and should have anandroid:onClicktag with a method name. We use the samemethod name for each of the strawberry's' pages' onClick callback as we are going to appear to use asingle strawberry page for each strawberry. We see how this works next week.Add anandroid:tag="#"where the#is a different integer for each button. Add thePrevious,NextandHomebuttons to the bottom of the page and place them together in a horizontalLinearLayout(enclosed in the parentRelativeLayout) so we will need to position it in its parent. Give the buttonsequal weights so they appear in line.2Before you click on any of the buttons on theHome_fragmentyou have to create the strawberry pageto go to, or your app will crash. Create aStrawberry_fragment.javafile (thatextendsthe Fragmentclass) and astrawberry_fragment.xmlfile for its UI (user interface).In thestrawberry_fragment.xmluse aRelativeLayoutso you can postion the items with respect toeach other. At the top place aTextViewwith the text "Strawberry 1" as the text and useandroid:textsize = "32dp"to create slightly larger text.Within the parentRelativeLayoutput the first labelTextView(ID:) withandroid: layout_belowto settheTextViewbelow the topTextView(Strawberry 1). TheEditTextis put below its labelTextView(ID:), again usingandroid: layout_below.
Repeat this process for the other lines shown for entering the other data (the Weight, Sunlight, Compostand Water lines) as detailed in the assignment specification. At the bottom of the fragment is the line ofthree buttons -Previous,NextandHomeas on theHome_fragment. Complete the required entries.3You need to modify theMainActivity.javaso that it displays thestrawberry_fragmentinstead ofthe default in itsonCreate()method. But before we do this we need to get some file naming issuessorted out. To get theMainActivity.javato display the new Home fragment you created locate thesetContentView(R.layout.activity_main);line in theActivityMain.javaonCreate()method andreplace it with the following lines:FragmentManager fragmentManager=getFragmentManager();FragmentTransactionfragmentTransaction=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 theFragmentManagerclass.TheStrawberry_fragment.javafile is to be run to inflate thestrawberry_fragment.xmlfile when theuser presses a strawberry name button on the home page or when thePreviousorNextbuttons are usedto navigate to the page. Let's assume theStrawberry 1's buttons click method,onClick, is called byclicking on theStrawberry 1button on the home page. We add aonClick()method to ourMainActivity's java file to handle this click. But we need to know which buttons'onClick()was called.To do this use the followingonClick()method:public void onClick(View view){//Retrieve index from view's tagcurrentPage = Integer.valueOf((String)view.getTag());showCurrentPage();}currentPageis declared as anintin the main activity java class. You use it to keep track of whatstrawberry you are entering data for.showCurrentPage()is a method you declare in your main activityto display the strawberry's page. Let's assume you use integers 0 to 5 to represent the fragments in theactivity where 0 represents theHome_fragmentand 1 to 5 represent the 5 strawberry fragments. IfcurrentPageis 0, this then means show theHome_fragmentusing: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 thestrawberry to theStrawberry_fragmentso we can customise the fragment to the strawberry. We dothis by using aBundleobject and thesetArgumentsmethod 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();
desklib-logo
You’re reading a preview
Preview Documents

To View Complete Document

Click the button to download
Subscribe to our plans

Download This Document