Subscribe:

Ads 468x60px

dropdown

Social Icons

Android Resources


 Resources

Android supports that resources, like images and certain XML configuration files, can be keep separate from the source code.
These resources must be defined in the res directory in a special folder dependent on their purpose. You can also append additional qualifiers to the folder name to indicate that the related resources should be used for special configurations, e.g. you can specify that a resource is only valid for a certain screen size.
The following table give an overview of the supported resources and their standard folder prefix.

Table 1. Resources

Resource
Folder
Description
Simple Values
/res/values
Used to define strings, colors, dimensions, styles and static arrays of strings or integers. By convention each type is stored in a separate file, e.g. strings are defined in theres/values/strings.xml file.
Layouts
/res/values
XML file with layout description files used to define the user interface for activities and Fragments.
Styles and Themes
/res/values
Files which define the appearance of your Android application.
Animations
/res/animator
Define animations in XML for the property animation API which allows to animate arbitrary properties of objects over time.
Menus
/res/menu
Define the properties of entries for a menu.

The gen directory in an Android project contains generated values. R.java is a generated class which contains references to certain resources of the project.
If you create a new resource, the corresponding reference is automatically created in R.java via the Eclipse ADT tools. These references are static integer values and define IDs for the resources.
The Android system provides methods to access the corresponding resource via these IDs.

For example to access a String with the R.string.yourString ID, you would use thegetString(R.string.yourString)) method.

R.java is automatically created by the Eclipse development environment, manual changes are not necessary and will be overridden by the tooling.


Assets

While the res directory contains structured values which are known to the Android platform, the assets directory can be used to store any kind of data. You access this data via the AssetsManager which you can access the getAssets()method.
AssetsManager allows to read an assets as InputStream with the open() method.

// Get the AssetManager
    AssetManager manager = getAssets();

    // Read a Bitmap from Assets
    InputStream open = null;
    try {
      open = manager.open("logo.png");
      Bitmap bitmap = BitmapFactory.decodeStream(open);
      // Assign the bitmap to an ImageView in this layout
      ImageView view = (ImageView) findViewById(R.id.imageView1);
      view.setImageBitmap(bitmap);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (open != null) {
        try {
          open.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }


Defining IDs

Android allows that you define ID of user interface components dynamically in the layout files, via the @+id/your_idnotation.
To control your IDs you can also create a file called ids.xml in your /res/values folder and define all IDs in this file.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <item name="button1" type="id"/>

</resources>

This allow you to use the ID directly in your layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="27dp"
        android:text="Button" />

</RelativeLayout>


Android Application Architecture: - Android App Outlook


 AndroidManifest.xml


The components and settings of an Android application are described in the AndroidManifest.xml file. For example allactivities and services of the application must be declared in this file.
It must also contain the required permissions for the application. For example if the application requires network access it must be specified here.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.vogella.android.temperature"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Convert"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="9" />

</manifest>

The package attribute defines the base package for the Java objects referred to in this file. If a Java object lies within a different package, it must be declared with the full qualified package name.

Google Play requires that every Android application uses its own unique package. Therefore it is a good habit to use your reverse domain name as package name. This will avoid collisions with other Android applications.

android:versionName and android:versionCode specify the version of your application. versionName is what the user sees and can be any String.
versionCode must be an integer. The Android Market determine based on the versionCode, if it should perform an update of the applications for the existing installations. You typically start with "1" and increase this value by one, if you roll-out a new version of your application.

The <activity> tag defines an activity, in this example pointing to the Convert class in thede.vogella.android.temperature package. An intent filter is registered for this class which defines that thisactivity is started once the application starts (action android:name="android.intent.action.MAIN" ). The category definition category android:name="android.intent.category.LAUNCHER" defines that this application is added to the application directory on the Android device.

The @string/app_name value refers to resource files which contain the actual value of the application name. The usage of resource file makes it easy to provide different resources, e.g. strings, colors, icons, for different devices and makes it easy to translate applications.

The uses-sdk part of the AndroidManifest.xml file defines the minimal SDK version for which your application is valid. This will prevent your application being installed on unsupported devices.


 Activities and Lifecycle

The Android system controls the lifecycle of your application. At any time the Android system may stop or destroy your application, e.g. because of an incoming call. The Android system defines a lifecycle for activities via predefined methods. The most important methods are:
·     onSaveInstanceState() - called after the Activity is stopped. Used to save data so that the Activitycan restore its states if re-started
·     onPause() - always called if the Activity ends, can be used to release resource or save data
·     onResume() - called if the Activity is re-started, can be used to initialize fields


Configuration Change

An Activity will also be restarted, if a so called "configuration change" happens. A configuration change happens if an event is triggered which may be relevant for the application. For example if the user changes the orientation of the device (vertically or horizontally). Android assumes that an Activity might want to use different resources for these orientations and restarts the Activity.

In the emulator you can simulate the change of the orientation via Ctrl+F11.
You can avoid a restart of your application for certain configuration changes via the configChanges attribute on yourActivity definition in your AndroidManifest.xml. The following Activity will not be restarted in case of orientation changes or position of the physical keyboard (hidden / visible).

<activity android:name=".ProgressTestActivity"
     android:label="@string/app_name"
     android:configChanges="orientation|keyboardHidden|keyboard">
</activity>


Context

The class android.content.Context provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.

The Context also provides access to Android services, e.g. the Location Service.
activities and services extend the Context class.


Android Development Tools: - How to built a Android App ........


 Android SDK


The Android Software Development Kit (SDK) contains the necessary tools to create, compile and package Android application. Most of these tools are command line based.
The Android SDK also provides an Android device emulator, so that Android applications can be tested without a real Android phone. You can create Android virtual devices (AVD) via the Android SDK, which run in this emulator.
The Android SDK contains the Android debug bridge (adb) tool which allows to connect to an virtual or real Android device.


Android Development Tools


Google provides the Android Development Tools (ADT) to develop Android applications with Eclipse. ADT is a set of components (plug-ins) which extend the Eclipse IDE with Android development capabilities.
ADT contains all required functionalities to create, compile, debug and deploy Android applications from the Eclipse IDE. ADT also allows to create and start AVDs.
The Android Development Tools (ADT) provides specialized editors for resources files, e.g. layout files. These editors allow to switch between the XML representation of the file and a richer user interface via tabs on the bottom of the editor.


Dalvik Virtual Machine


The Android system uses a special virtual machine, i.e. the Dalvik Virtual Machine to run Java based applications. Dalvik uses an own bytecode format which is different from Java bytecode.
Therefore you cannot directly run Java class files on Android, they need to get converted in the Dalvik bytecode format.


How to develop Android Applications


Android applications are primarily written in the Java programming language. The Java source files are converted to Java class files by the Java compiler.
The Android SDK contains a tool called dx which converts Java class files into a .dex (Dalvik Executable) file. All class files of one application are placed in one compressed .dex file. During this conversion process redundant information in the class files are optimized in the .dex file. For example if the same String is found in different class files, the .dex file contains only once reference of this String.
These dex files are therefore much smaller in size than the corresponding class files.
The .dex file and the resources of an Android project, e.g. the images and XML files, are packed into an .apk (Android Package) file. The program aapt (Android Asset Packaging Tool) performs this packaging.
The resulting .apk file contains all necessary data to run the Android application and can be deployed to an Android device via the adb tool.
The Android Development Tools (ADT) performs these steps transparently to the user.
If you use the ADT tooling you press a button the whole Android application (.apk file) will be created and deployed.


Resource editors


The ADT allows the developer to define certain artifacts, e.g. Strings and layout files, in two ways: via a rich editor, and directly via XML. This is done via multi-page editors in Eclipse. In these editors you can switch between both representations by clicking on the tab on the lower part of the screen.
For example if you open the res/layout/main.xml file in the Package Explorer View of Eclipse, you can switch between the two representations as depicted in the following screenshot.




What is Android? - Question of the Era ........


1.1. Android Operation System


Android is an operating system based on Linux with a Java programming interface.
The Android Software Development Kit (Android SDK) provides all necessary tools to develop Android applications. This includes a compiler, debugger and a device emulator, as well as its own virtual machine to run Android programs.
Android is currently primarily developed by Google.
Android allows background processing, provides a rich user interface library, supports 2-D and 3-D graphics using the OpenGL libraries, access to the file system and provides an embedded SQLite database.
Android applications consist of different components and can re-use components of other applications. This leads to the concept of a task in Android; an application can re-use other Android components to archive a task. For example you can trigger from your application another application which has itself registered with the Android system to handle photos. In this other application you select a photo and return to your application to use the selected photo.

Android is the newest mobile device operating system, we will be able to confidently create your own mobile device programs after learning android. Android applications are developed in Java and run on the Linux 2.6 kernel.

                                              

Open Handset Alliance and Android


The Open Handset Alliance is a group of hardware and software developers, including Google, NTT DoCoMo,
Sprint Nextel, and HTC, whose goal is to create a more open cell phone environment.
The first product to be released under the alliance is the mobile device operating
system, Android.
With the release of Android, Google made available a host of development tools
and tutorials to aid would-be developers onto the new system. Help files, the platform
software development kit (SDK), and even a developers’ community can be found at
Google’s Android website, http://code.google.com/android. This site should be your
starting point, and I highly encourage you to visit the site.
hardware platforms have been announced for Android to run on. HTC, LG
Electronics, Motorola, and Samsung are members of the Open Handset Alliance, under
which Android has been released, so we can only hope that they have plans for a few
Android-based devices in the near future. With its release in November 2007, the system
itself is still in a software-only beta.

Introduction to Android
Android, as a system, is a Java-based operating system that runs on the Linux 2.6 kernel.
The system is very lightweight and full featured. Figure 1-1 shows the unmodified
Android home screen.

Figure



Android applications are developed using Java and can be ported rather easily to the
new platform.


1.2. Google Play (Android Market)


Google offers the Google Play service in which programmers can offer their Android application to Android users. Google phones include the Google Play application which allows to install applications.
Google Play also offers an update service, e.g. if a programmer uploads a new version of his application to Google Play, this service will notify existing users that an update is available and allow to install it.
Google Play used to be called Android Market.


1.3. Security and permissions

During deployment on an Android device, the Android system will create a unique user and group ID for every Android application. Each application file is private to this generated user, e.g. other applications cannot access these files.
In addition each Android application will be started in its own process.
Therefore by means of the underlying Linux operating system, every Android application is isolated from other running applications.
If data should be shared, the application must do this explicitly, e.g. via a service or a ContentProvider.
Android also contains a permission system. Android predefines permissions for certain tasks but every application can define additional permissions.
An Android application declare its required permissions in its AndroidManifest.xml configuration file. For example an application may declare that it requires access to the Internet.
Permissions have different levels. Some permissions are automatically granted by the Android system, some are automatically rejected.
In most cases the requested permissions will be presented to the user before installation of the application. The user needs to decide if these permissions are given to the application.
If the user denies a permission required by the application, this application cannot be installed. The check of the permission is only performed during installation, permissions cannot be denied or granted after the installation.

Not all users pay attention to the required permissions during installation. But some users do and they write negative reviews on Google Play.