Download Week 1 - Introduction to Android Studio

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CO5025 – Further Programming and Problem Solving: Android
Week 1 - Introduction to Android Studio
Using Android Studio
Android Studio is a dedicated IDE for android development created by Google.
Exercise: Creating your first application
1. Open Android Studio and choose Start a new Android Studio Project
2. Enter the Application Name – Week01 (capital W and no spaces)
3. Alter the company domain so it starts with a letter (any will do)
4. Choose a project location in a dedicated folder on your network area or memory stick and click Next
5. Select Phone and Tablet and API 19 and click Next
6. Choose Blank Activity from the next screen and click through to Finish.
Creating an Android Virtual Device (AVD)
Before you can run a program on the emulator, you must first create an AVD to simulate a particular device. We are
going to emulate a Nexus 7 which is a fairly standard size of tablet of which we have several in the department.
1. Select Tools > Android > AVD Manager and click Create Virtual Device in the dialog which appears
2. Select Tablet, and choose Nexus 7 for device and Marshmallow (API 23) with x86
Running your application on the emulator
1. Click the green triangle on the toolbar at the top.
2. In the Choose Device dialog, click Launch emulator and select the emulator you just
created. The emulator should (eventually) start and appear on the screen, but there will
be no immediate output and it will just be a black window.
3. Drag the emulator onto your other screen, so you can see the IDE at the same time
An Android logo with animated letters will appear eventually; this is the HOME
application.
Later still, your app should upload to and install itself on the emulator and eventually
appear with the traditional ‘Hello World’ message. You may have to drag upwards before
you see this.
You will notice how long it takes to start up the emulator – so you are strongly advised to keep the emulator open
The exercises continue on the next page, but read the notes below first
Components of an android application
Unlike a traditional java application, an android app has a number of parts:1. The java folder which contains the java code, in our case one file called MainActivity.java
2. The res (resources) folder contains XML files and other resources like images.
a) The layout subfolder of the res folder contains a screen layout called activity_main.xml
b) The values subfolder contains several files including strings.xml. This file is important as this is where you
store all the text for your app. In strings.xml you will see a list of the strings used by your app.
Creating screen layouts
You can either design layouts using the GUI or by editing the XML. You can edit either by using the GUI editor or by
editing the XML and the changes will be shown in the other.
String resources
String resources are a feature of android which allow developers to build in internationalisation options. The golden
rule is never include a string explicitly in your code. Instead:1. Create a named string in strings.xml and then use the syntax R.string.stringname to refer to it
1
CO5025 – Further Programming and Problem Solving: Android
Exercise: Enhancing your application
This exercise adds a button to your layout, set its caption and wires it up to a java method to alter the displayed text.
Edit the layout
Select the tab activity_main.xml. In Design view, change the virtual device to Nexus 7. Look in the Widgets folder,
find the button marked ‘Button’ and drag in onto the layout below the ‘Hello World’ text. You should now have a
button defined in your XML like this:<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true" />
Note that the fourth line is highlighted in yellow, warning you of a hard-coded string.
Add a string resource
1.
2.
3.
4.
Place your cursor on the highlighted line and move it around until the yellow bulb icon appears
Click to drop down the menu and select Extract string resource
In the dialog that opens add button_text as the name of the resource and press ok
Now Find the file strings.xml in res\values and open it. Edit the button_text line so it looks like this:<string name="button_text">Click here</string>
5. Run your app and make sure it works. You should now have a button which says ‘Click here’
Adding a button listener
Before we can react to the button click we have to create an event handler for it.
1. Working with the java file MainActivity.java, add some import statements:import
import
import
import
android.view.View.OnClickListener;
android.view.View;
android.widget.Button;
android.widget.TextView;
2. Now change the class header so it looks like this:public class MainActivity extends AppCompatActivity implements OnClickListener{
3. When an error appears, click on the bulb and choose Implement methods. Then select onClick(v:View):void
4.
Add a declaration below the class heading:TextView lblTextViewOne;
5. Now add this code to the end of method onCreate:lblTextViewOne = (TextView) findViewById(R.id.textView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
Each control needs a findViewById statement to link it to the layout.
6. Now add a setText statement to the OnClick method to change the text of the lblTextViewOne
lblTextViewOne.setText(R.string.button_message) ;
7. Edit the string as before to read:<string name="button_message">Button clicked!</string>
8. That should be all, test and make sure it works.
2