IT tutorials
 
Mobile
 

Android Application Development : The ApiDemos Application (part 1) - Application Setup in the Manifest File, Finding the Source to an Interesting Example

1/8/2013 5:19:34 PM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

1. Application Setup in the Manifest File

Like every other Android application, the best place to get a sense of how the application is strung together is the application’s AndroidManifest.xml file. Let’s take a look at part of the AndroidManifest.xml file for ApiDemos, near the beginning of the file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.apis">

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:name="ApiDemosApplication"
            android:label="@string/activity_sample_code"
            android:icon="@drawable/app_sample_code" >

        <uses-library android:name="com.google.android.maps" />

        <activity android:name="ApiDemos">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>    

					  

Here are some of the highlights of the code:

The ApiDemosApplication class, found in the top-level directory of the source code, extends the Application class and has two methods: onCreate and onTerminate. The onCreate method executes before any activities start. Application-level global variables should be defined and initialized by the onCreate method. It’s also a good place to set up any application-level default values.

There are several subdirectories under samples/ApiDemos/src/com/example/android/apis, each corresponding to a high-level functional area of the Android API:


App

Examples of application-level constructs such as Activities, Alarms, Dialogs, and Services.


Content

Describes how to read assets from a file, from resources, and from an XML file.


Graphics

Many types of graphics examples, such as arcs bitmap manipulation, clipping, layers, and OpenGL.


Media

Examples of the MediaPlayer and the VideoView.


OS

Examples of how to invoke operating system services. As of this writing, it shows how to use the VIBRATOR_SERVICE and SENSOR_SERVICE.


Text

Cool text tricks. The “Linkify” demo shows how to use the autoLink attribute of the TextView to automatically set up links in text: the user clicks on a URL and the browser comes up, or clicks on a phone number and the dialer appears. The “LogTextBox” demo shows how to create a simple screen log with a LogTextBox View.


Views

All of the various Android views: buttons, text boxes, autocompletion, date widgets, etc. You can find the dozens of different Android GUI elements here, along with their many options.


2. Finding the Source to an Interesting Example

The ApiDemos application has a lot of interesting examples that will help you learn how to program an Android application. However, it’s not entirely obvious how to find the source to any particular screen. The following procedure will help you find the source to any ApiDemo you’re interested in. To understand the process, we’ll trace a couple of demos: the “App/Activity/Custom Title” and the “Text/Linkify” examples.

2.1. Custom Title Demo

This technique works when the ApiDemos application stores information about the demo in the res/strings.xml resource file:

  1. After starting the ApiDemos application, find the particular demo by clicking on the menu, and remember the path you took through the menu system. In this case, you click on App, then Activity, and finally Custom Title.

  2. Open the res/values/strings.xml file in a text editor such as Eclipse (actually, any text editor that can do regular expression searches should work fine). Carry out a regular expression search (Ctrl-F Ctrl-X in Eclipse) for each of the menu words from step 1. Use the regular expression “.*” to separate the words. Thus, the search term in our example is App.*Activity.*Custom.*Title. The search should return zero or one result.

  3. Open the AndroidManifest.xml file and search it for the string you found in the previous step: activity_custom_title. The search should return only one result, which should be part of the the value of the android:label attribute within an activity element. That activity element should also contain an android:name attribute. The value of this attribute contains the path to the Activity class that implements the demo. In our example it’s .app.CustomTitle. This translates to the CustomTitle.java files in the app subdirectory of the source tree.

In the end, therefore, the source for the App → Activity → Custom Title menu item can be found in samples/ApiDemos/src/com/example/android/apis/app/CustomTitle.java.

2.2. Linkify Demo

This technique should work for demos that you can’t find with the previous method. If the ApiDemos application doesn’t store information about the demo in res/strings.xml, it gets its information directly from AndroidManifest.xml—and so will we.

  1. After starting the ApiDemos application, find the particular demo through clicking on the menu, and remember the path you took through the menu system. In this case, you click on Text and then Linkify.

  2. Open the AndroidManifest.xml file and search for the menu elements as in the previous example. But this time the menu elements must be separated by slashes instead of “.*” regular expressions. So in this case, search for the text Text/Linkify (it doesn’t have to be a regular expression search).

    The search should return only one result, which should be part of the the value of the android:label attribute within an activity element. That element should also contain an android:name attribute. The value of this attribute contains the path to the Activity class that implements the demo. In our example, the path is .text.Link. This translates to the Link.java file within the text subdirectory of the source tree.

So in this example, the source for the Text → Linkify menu item can be found in samples/ApiDemos/src/com/example/android/apis/text/Linkify.java.

 
Others
 
- iphone Programming : Adding Missing Features (part 3) - Changing the Display Name, Enabling Rotation
- iphone Programming : Adding Missing Features (part 2) - Adding a Launch Image
- iphone Programming : Adding Missing Features (part 1) - Adding an Icon
- iphone Programming : Using Sensors - Accessing the Proximity Sensor
- Mobile Web Apps : Templating - Twitter Integration with Templating
- Mobile Web Apps : Ajax - Fetching HTML, Ajaxifying Links
- IPad : Working with Contacts - Adding a New Contact Right on Your iPad
- Bluetooth on the iPad : Bluetooth Stereo, Disconnect or Forget a Bluetooth Device
- BlackBerry Bold 9700 and 9650 Series : Fixing Problems - Web Browser Running Slowly, Automatic Memory Cleaning to Keep Running Smoothly
- BlackBerry Bold 9700 and 9650 Series : Fixing Problems - BlackBerry Running Slowly?
 
25 Inspiring Game of Thrones Quotes
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS