Download presentation
Presentation is loading. Please wait.
Published byEdwina Robbins Modified over 9 years ago
1
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at http://developer.android.com/resources/tutorials/hello-world.html http://developer.android.com/resources/tutorials/hello-world.html – Don’t do anything under the section “Construct the UI” Alternatively, you can do the step “Construct the UI.” But then, redo the “Create an AVD” and “Create a New Android Project.” After Creating the AVD, start the AVD – In eclipse, menu Window -> AVD Manager. – Select the emulator, and then select start – It takes quite along time for the emulator to start Run App – Right click on HelloAndroid in the package Explorer – Select Run As -> Android Application – A dialog box opens showing a list of places where the app can run. Select the device or emulator – If an emulator is not running and there is no device, the emulator will start (assuming that one is defined) – If using the emulator, bbe sure that the emulator is unlocked. It will lock like your phone if idle for a while
2
Adjust font size open res/layout/main.xml See xml and layout (select toward the bottom) (Note the outline view at the far right – Might need to click on button on right edge) Expand linear layout On graphic, right on “Hello World…” – Other properties -> inherited from textView -> text size set text size to 18pt Save and rerun – If the emulator is idle, it will lock. Unlock to run app – Run by pressing the green arrow near the top of eclipse Note that when you hover over the green arrow, it says Run HelloAndroid ((Does logcat appear in java: Window -> Preferences -> Android -> Logcat - > Display logcat view when there are messages from an application in the workspace))
3
Add button In layout/main.xml Along left is Palette Play with palette views with down arrow – I like show icons only Find button and drag to graphical view Change id – Right click and select Edit Id – Set it to ChangeActivityButton Change text – The right way is to define a string. – The fast way Switch to main.xml Locate <Button … Locate android:text=“button” Change to – android:text=“Change View” Run
4
Display short message when button is pressed Open src/com.example.helloandroid.java Let’s understand what is already there – HelloAndroid extends Activity: We’ll learn more about activities later. But all activities must be extend Android.app.Activity – setContentView(R.layout.main); will make the view. Note that R.layout.main refers to the main.xml layout where we changed the font size and added the button. But R is a class that the android plugin automatically creates when anything under /res is saved (so you might need to save before r is valid). So R is a provides an interface between the resources (e.g., xml files) and your program After “setContentView(…);”, add – final Button button = (Button) findViewById(R.id.ChangeActivityButton); – Note that Button is red underlined (an error). Click on button. Import Button. Expand import.. To see that button has been included Add – button.setOnClickListener(new View.OnClickListener() { – public void onClick(View v) { – // Perform action on click – Toast.makeText(HelloAndroid.this, "clicked", Toast.LENGTH_SHORT).show(); – } – }); Run Instead of Toast…, put Log.e("DebugInfo","button pressed"); Why in Toast… do we have HelloAndroid.this. What is this?, why not just this?
5
Make a new activity (so we can flip between activities)
6
Activities An app is composed of tasks each with its own screen. These tasks referred to as activities. – Activity is composed to some actions (code), and a view – The code is defined by a java class, while the view is defined by an xml file. – The java code controls the screen, and hence which xml file is used The system and your activity – How does an activity become active? The OS can start it Another activity can start it – How does the activity end The OS can end it It can end itself The first activity (the one that is active when the app starts) is the one that is given in the app’s manifest as follows <activity android:name=".HelloAndroid" android:label="@string/app_name"> These say that this class is the entry point into the app. That is, the OS can call this activity by using the name and category. This particular name and category means it is the entry point This is a class name in the package
7
Make a new view Add layout of view – On the left right click on /res/layout. – Select new -> other-> Android->Android XML Layout file – Call file view2 (name must be all smalls, no caps) – Leave it as Layout resource – Leave LinearLayout as root element – Next/finish
8
layout view Drag a large textview – Change text to View2 Drag a button – Change text to “go to other view” – Change ID to GoToOtherView
9
Add a class to show this view Right click on edu.udel.eleg454.HelloAndroid Select new -> class Set Name to SecondView Set superclass to.. – Check what helloAndroid is extended from – android.app.Activity – Set super class to android.app.Activity Finish
10
Add text to SecondView First open HelloAndroid Copy @Override…. Paste in to the same place on SecondView Delete the button stuff Change – setContentView(R.layout.main); – To – setContentView(R.layout.???); Go back to /res/layout. What is the name of the xml file we just added? view2 setContentView(R.layout.view2); Recall that the layout is an xml file. But R is a class that is automatically generated from the resources (including xml files) R.layout.mmmm “uses” res/layout/mmmm.xml R.id.joe is the id of resource with id called joe. – The xml file has … android:id=“@+id/joe” …. – Note that we used the for getting the button object
11
Make View2 into an activity that can be called The OS will call (e.g., start) activities The app must advertise the activities that can be called and must detail how they are referenced (what name to use to call them) This is done in the Manifest (the manifest does many other things as well) Edit manifest to allow View2 to be started Open androidManifest.xml – Explore all the things – Check out Permission tab. This is where an app can request permission to use various resources, like GPS or the network. Select the Application tab – Go to Application Nodes (lower left area) – Select add Select radio button “Create a new element at the top level, in Application” Select Activity Highlight the newly added activity On the right, change name to SecondView. Or select browse to find the view. Leave other fields empty Now, under application nodes it says SecondView (Activity) (maybe after saving) Under application nodes click add Select radio button “Create new element in the selected element, Application > OtherView (Activity) Select Intent Filter Now Intent Filter has been added. Leave the fields on the right empty Under the application nodes highlight the newly added Intent Filter Click Add Select Action In the attributes for Action (on the right), set name to edu.udel.eleg454.helloandroid.ShowSecondView – This is the name that we made up. This app (and other) can call the activity by this name Under Application Nodes select newly added Intent Click Add Select Category In drop down list, select android.intent.category.DEFAULT Save
12
Call View2 when button in main view is clicked Open HelloAndroid.java under src/com.example.helloandroid Recall that Toast is executed on the button press. Now let’s add something to run the other view. Just below Toast.makeText(…); add String actionName = "edu.udel.eleg454.helloandroid.ShowSecondView"; Intent intent = new Intent(actionName); startActivity(intent); Since Intent is underlined in red, hover over and select import Now save and run Press button
13
Closing view2 You can close view2 with the “go back” button Or, add a “close” button, Button closeViewButton = (Button)findViewById(R.id.closeViewButton); closeViewButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); }});
14
add a new string to /res/strings.xml – Open strings.xml – View the resources view (as oppose to the xml view) – Select Add – Select String – Set name otherString – Set Value This is the second view
15
Edit view2 Open /res/layout/view2.xml (if it is not already open) Go to layout view (not xml) On far right, select LinearLayout Click green + Select TextView, ok Highlight TextView on far right Open properties (tab toward the bottom) Scroll through textView to Text. Select browse and select otherString (the one we just added)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.