For more videos, check Dr. Bohacek's Home Page and recent ELEG454 home page

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

–      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 ->  Android SDK and AVD Manager.

•       Select the emulator, and then select start

•       It takes quite a long time for the emulator to start

•       Run on emulator

–      Click on menu “Run”

–      There is a green arrow on the toolbar that will also start

–      If an emulator is not running and there is no device, the emulator will start (assuming that one is defined)

•       (Be sure that the emulator is unlocked. It will lock like your phone if idle for a while)

 

Playing with Resources

Changing the font (Video)

•       In eclipse, open res/layout/main.xml

•       See xml and layout (select toward the bottom)

•       Note the outline view at the far right

•       Expand linear layout

•       Click on textview

•       See properties (on of the tabs along the bottom)

•       Scroll in properties to text size. Make text size 18pt

•       Save and rerun

–      If the emulator is idle, it will lock. Unlock to run app

 

Adding a button (Video)

•       In layout/main.xml

•       Select outline

•       Select LinearLayout

•       Click the green “+” to add something

•       Find button and select

•       Change id

–      Select button in outline

–      Go to properties

–      Find Id and set it to @id/ChangeActivityButton

•       Change text to “Press Here”

•       Run

 

Display short message when button is pressed (Video)

•       Open src/com.example.helloandroid.java

•       After “setContentView(…);”, add

final Button button = (Button)  findViewById(R.id.ChangeActivityButton);

–      Note that Button is red underlined (an error). Click on button. Import Button.

•       Add the following code

button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click

Toast.makeText(HelloAndroid.this, "clicked", Toast.LENGTH_SHORT).show();
                }
 });

–      Note that Button is red underlined (an error). Click on button. Import Button.

•       Run

 

Activities (Video)

•       An app is composed of tasks that have their own screen. These tasks are 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 (often) defined by an xml file and code

–      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.

•       An application can have many activities. But only one can be the entry point. The entry point is controlled by the activities intents

Intents

•       An intent defines the intent to do something.

•       A process will register that it is able to respond to a certain type of intent. This is accomplished with an intent filter.

•       Another process (or the OS) will request the intent.

•       One example is the app registering its main entry point. Then the OS invokes this intent when the app is to be started.

 

 

Make a new activity (Video)

•       Make a view for the activity

•       Text string to show in the view

•       Make a class for this activity that will call this view

•       Put this activity in the manifest

•       Change the button press code to start this activity

 

 

Make a view for the activity

•       On the left right click on /res/layout

•       Select new -> android xml file

•       Call file view2.xml

•       Leave it as Layout resource

•       Leave LinearLayout as root element

 

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

 

Edit view2

•       Open /res/layout/view2.xml

•       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)

 

 

Add a class to show this view (Video)

•       Right click on /res/com.example.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

 

Add code to SecondView

•       First open HelloAndroid.java

•       Copy @Override….

•       Paste in to the same place on Second View

•       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);

 

Change the manifest so that View2 is an activity (Video)

•       Notes

–      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

•       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 OtherView. Leave other fields empty

•       Now, under application nodes it says OtherView (Activity)

•       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 com.example.helloandroid.ShowOtherView.

–  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

 

 

Call View2 when button in main view is clicked (Video)

•       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 = "com.example.helloandroid.ShowOtherView";

                           Intent intent = new Intent(actionName);

                            startActivity(intent);

•       Now save and run