Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mobile Computing With Android ACST 4550 Intro to Android, Part 3

Similar presentations


Presentation on theme: "Mobile Computing With Android ACST 4550 Intro to Android, Part 3"— Presentation transcript:

1 Mobile Computing With Android ACST 4550 Intro to Android, Part 3

2 The Context The Context in an Android App is a container of global information about an application environment that is provided by the Android OS. It allows access to application-specific resources and classes. The context must be passed to a View constructor, and it also must be passed to many other native Android class objects (like Toast). When you are in the Activity itself, you can use the “this” keyword to get the Context, but if you are in a thread created from the Activity you use the first method below, and to get it from a View thread you use the second: getApplicationContext(); // get’s context from an Activity thread getContext(); // get’s context from a View thread

3 Creating Multiple Activities
You can run multiple Activities in an Android App. When you do this, the new Activity appears on top of the previous Activity, and the previous Activity will reappear when the new Activity is ended or when it restarts the previous Activity. To start (or launch) a new Activity, first you have to notify the Android OS by creating an “Intent” object, which is passed the Context of the current Activity, and the Class object of the new Activity. And then you can start the new Activity with a call to the startActivity() method: Intent myIntent = new Intent(getApplicationContext(), NewActivity.class); startActivity(myIntent);

4 Creating Multiple Activities
Starting multiple Activities is different than switching from one View to another using setContentView(), because when you switch from one View to another, the old View is replaced in the current Activity and it is not simply placed behind the old View. If you have instantiated your own View object, you can maintain the View state from one setContentView() call to the next, but if you don’t do that, none of a previous View’s state is maintained for you.

5 The finish() method When an Activity is done and you want to close it from within your program, you should call the finish() method. This has the same effect as the user hitting the “back” or “return” button. Calling this method is somewhat similar to calling System.exit(0) in a Standard Java program. If there are multiple Activities running in an App, both finish() and System.exit(0) will only close the current one. In most cases, you should use the finish() method, but if you wanted to close an Activity from within a View, then you would use System.exit(0).

6 Drawing Units There are six basic units to use when identifying distance on the Android Screen: px, in, mm, pt, dp, and sp px - Pixels - corresponds to actual pixels on the screen. in - Inches - based on the physical size of the screen. mm - Millimeters - based on the physical size of the screen. pt - Points - 1/72 of an inch based on the physical size of the screen. dp - Density-independent Pixels - an abstract unit that is based on the physical density of the screen (see next slide) sp - Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference (see next slide).

7 Drawing Units dp - Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp". sp - Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

8 Accessing Pre-Defined Strings
In the “strings.xml” file of your Android project are string constants that you should use rather than including literal strings directly in views and code, so that you will be able to support multiple languages. Strings in the strings.xml file are formatted like so: <string name="str_name">This is a string</string> Then you can access the string in your code by loading it using the getString() method and the string’s ID path as follows: String theStr = getString(R.string.str_name);


Download ppt "Mobile Computing With Android ACST 4550 Intro to Android, Part 3"

Similar presentations


Ads by Google