Wednesday 20 April 2011

How to make Custom Menu Bar –Tabs in Android Application and How to Hook the menu button to show/hide a custom tab bar

    I find this Example from the Website and share At the Blogs Which I will update time to time .By using this example one can we use it as tab Activity in android or it will be become more useful to Menu Bar.

The subject of this tutorial is how to create your own custom menu bar that will be shown and hidden when user clicked the menu button on his/her device.

There are many other uses for this example and it just isn’t limited to a menu or tab bar. For example, you can use this to show and hide a panel that contains whatever content you want to be shown. It could be used for contextual help or Media Player controls, etc.

Custom Menu Panel

The basis of this example is the layout. I have found that the RelativeLayout is superior for creating great layouts that perform well across devices. If you want to learn more about what a Relative Layout can do for you I highly recommend that you check out Working with Multiple Android Screens in the Motorola Android Technical Library. For the purpose of this tutorial… I haven’t followed everything that I should have as far as creating a good cross device application by defining my layouts using dip (Density Independent Pixels)… but this is just an example. Now let’s get to it.

The main layout for the application contains 2 Relative Layouts, a Text View, a Tab Host, aTabWidget and a View. In particular, I want to point out the layout_align ParentBottom attribute of the panel Relative Layout. This attribute is one of 4 separate attributes that allow you to place a Relative Layout on any side of the parent layout. I also want to bring your attentions to the visibility attribute which allows us to set the layout as hidden. Using “gone” makes the layout to be completely hidden, as if the view had not been added.

/res/layout/main.xml
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:background="#000000"
        android:id="@+id/content"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="">
    </TextView>    
    <RelativeLayout
        android:layout_alignParentBottom="true"
        android:id="@+id/menuPopup"
        android:background="@drawable/tl_bg"
        android:layout_width="fill_parent"
        android:layout_height="90px"
        android:visibility="gone">
            <TabHost
                android:id="@android:id/tabhost" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <LinearLayout 
                android:orientation="vertical"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent">
                <View 
                    android:layout_width="fill_parent" 
                    android:layout_height="0.5dip"
                    android:background="#000">
                </View>
                <TabWidget 
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent"
                    android:layout_marginLeft="0dip" 
                    android:layout_marginRight="0dip">
                </TabWidget>
                <View 
                    android:layout_width="fill_parent" 
                    android:layout_height="2dip"
                    android:background="#696969">
                </View>
                <View 
                    android:layout_width="fill_parent" 
                    android:layout_height="2dip"
                    android:background="#000">
                </View>
                <FrameLayout 
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent">
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    </RelativeLayout>
</RelativeLayout>

The second layout is for each individual tab. I am not going to go into how to customize the tabs in this tutorial.

/res/layout/tabs_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/tab_bg_selector"
    android:padding="5px" 
    android:gravity="center" 
    android:orientation="vertical">
    <ImageView 
        android:id="@+id/tabsIcon"
        android:layout_width="36px"
        android:layout_height="36px">
    </ImageView>
    <TextView 
        android:id="@+id/tabsText" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5px"
        android:text=""
        android:textSize="12px"
        android:textColor="@drawable/tab_text_selector">
    </TextView>
</LinearLayout>

The first activity is called CustomMenuBar.java

package com.androidvogue.example.custommenubar;



import android.app.TabActivity;
import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.TabHost;

import android.widget.TabHost.TabContentFactory;

import android.widget.TabHost.TabSpec;

import android.widget.TextView;



public class CustomMenuBar extends TabActivity {

               

                private TabHost mTabHost;

                private static RelativeLayout mMenuPanel;

                private TextView content;

private final static String WEBSITE = "http://www.android-vogue.blogspot.com";

               

                // This isn't necessary but it makes it nice to determine which tab I am on in         //the switch statement below

                private static class TabItem {

                                public final static int SEARCH = 0;

                                public final static int SHARE = 1;

                                public final static int WEBSITE = 2;

                                public final static int SETTINGS = 3;

                                public final static int QUIT = 4;

                }

               

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        setupViews();

    }

   

    private void setupViews() {

               

                content = (TextView) findViewById(R.id.content);

                content.setText(getString(R.string.search));

               

                mMenuPanel = ((RelativeLayout) findViewById(R.id.menuPopup));

                mMenuPanel.setVisibility(View.GONE);

                               

                mTabHost = (TabHost) findViewById(android.R.id.tabhost);

                                mTabHost.setup();

                               

                mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);            

               

                addActivityTab(new TextView(this), "Search", R.drawable.search, new Intent(CustomMenuBar.this, Search.class));

                                addActivityTab(new TextView(this), "Share", R.drawable.heart, new Intent(CustomMenuBar.this, Share.class));

                                addMethodTab(new TextView(this), "Website", R.drawable.globe);

                                addActivityTab(new TextView(this), "Settings", R.drawable.tools, new Intent(CustomMenuBar.this, Settings.class));

                                addMethodTab(new TextView(this), "Quit", R.drawable.power);



                                mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

                                                @Override

                                                public void onTabChanged(String arg0) {

                                                                if (mMenuPanel != null) {

                                                                                if (mMenuPanel.getVisibility() == View.VISIBLE) {                                                                   

                                                                                                toggleMenu();

                                                                                }

                                                                                switch (mTabHost.getCurrentTab()) {

                                                                                                case TabItem.SEARCH:

                                                                                                                content.setText(getString(R.string.search));

                                                                                                                break;

                                                                                                case TabItem.SHARE:

                                                                                                                content.setText(getString(R.string.share));

                                                                                                                break;

                                                                                                case TabItem.WEBSITE:

                                                                                                                content.setText(getString(R.string.website));

                                                                                                                final Intent visit = new Intent(Intent.ACTION_VIEW);

                                                                                                                visit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                                                                                                                visit.setData(android.net.Uri.parse(WEBSITE));

                                                                                                               

                                                                                                                // Use a thread so that the menu is responsive when clicked

                                                                                                                new Thread(new Runnable() {

                                                                                                                                public void run() {

                                                                                                                                                startActivity(visit);

                                                                                                                                }

                                                                                                                }).start();                                                                               

                                                                                                                break;

                                                                                                case TabItem.SETTINGS:

                                                                                                                content.setText(getString(R.string.settings));

                                                                                                                break;

                                                                                                case TabItem.QUIT:

                                                                                                                content.setText(getString(R.string.quit));

                                                                                                                new Thread(new Runnable() {

                                                                                                                                public void run() {                                                                                                              

                                                                                                                                                CustomMenuBar.this.finish();

                                                                                                                                               

                                                                                                                                                // The following makes the Android Gods frown upon me

                                                                                                                                                android.os.Process.killProcess(android.os.Process.myPid());

                                                                                                                                                System.exit(0);

                                                                                                                                }

                                                                                                                }).start();

                                                                                                                break;

                                                                                                default:

                                                                                                                break;

                                                                                }

                                                                               

// Handle click on currently selected tab - hide menu bar

// IMPORTANT: This listener has to appear AFTER the tabs are added

// Unfortunately, This doesn't work when the current tab contains an activity (except for tab 0)

// If you only have method tabs then it works perfect



mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setOnClickListener(new View.OnClickListener() {

                                                                                               

                                                @Override

                                                public void onClick(View v)

{



                                                toggleMenu();

                                                                }

                                                                                });

                                                                               

                                                                                //if you want to reset the current tab

                                                                                // mTabHost.setCurrentTab(0);

                                                                }

                                                }

                                });

    }

   

    // Use this method to add an activity or intent to the tab bar

    private void addActivityTab(final View view, final String tag, int iconResource, Intent intent) {

                                View tabview = createTabView(mTabHost.getContext(), tag, iconResource);

                                 

                                TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)

                                                                .setContent(intent);

                                mTabHost.addTab(setContent);



                }

   

    // Use this method if you only want the tab to execute a method

    private void addMethodTab(final View view, final String tag, int iconResource) {

                                View tabview = createTabView(mTabHost.getContext(), tag, iconResource);



                                TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)

                                                                .setContent(new TabContentFactory() {

                                                                                public View createTabContent(String tag) {

                                                                                                return view;

                                                                                }

                                                                });

                                mTabHost.addTab(setContent);



                }

   

    private static View createTabView(final Context context, final String text,

                                                int iconResource) {

                                View view = LayoutInflater.from(context)

                                                                .inflate(R.layout.tabs_layout, null);

                                TextView tv = (TextView) view.findViewById(R.id.tabsText);

                                tv.setText(text);



                                ImageView icon = (ImageView) view.findViewById(R.id.tabsIcon);

                                icon.setImageResource(iconResource);



                                return view;

                }

   

    @Override

                public boolean onKeyDown(int keyCode, KeyEvent event) {

                                if (keyCode == KeyEvent.KEYCODE_MENU) {

                                                toggleMenu();

                                                return true;

                                } else {

                                                return super.onKeyDown(keyCode, event);

                                }

                               

    }

   

    public static void toggleMenu() {

                                if (mMenuPanel.getVisibility() == View.GONE) {

                                                mMenuPanel.setVisibility(View.VISIBLE);

                                } else {

                                                mMenuPanel.setVisibility(View.GONE);

                                }                             

                }
}
Here is one of the example activities that are set as content for the activity tabs. I will only publish this one because they are identical.

Search.java



package com.androidvogue.example.custommenubar;



import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.widget.TextView;



public class Search extends Activity {



                @Override

                public void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

        setupViews();                               

                }

               

                private void setupViews() {

                                /* Search Tab Content */

                                TextView textView = new TextView(this);

                                textView.setText(getString(R.string.search));

                                setContentView(textView);

                }

               

                @Override

                public boolean onKeyDown(int keyCode, KeyEvent event) {

                                if (keyCode == KeyEvent.KEYCODE_MENU) {

                                                CustomMenuBar.toggleMenu();

                                                return true;

                                } else {

                                                return super.onKeyDown(keyCode, event);

                                }

                               

    }

}



The code in CustomMenuBar.java is commented and you should be able to follow what it does… I am going to concentrate on the primary subject of this tutorial which is hooking the menu button and displaying and hiding the menu panel. First, it is very easy to hook the menu button. This is accomplished by overriding the onKeyDown method of the activity and catching the KeyEvent for the menu button. If you look at the KeyEvent page in the SDK documentation you can see that there are many KeyEvent constants that allow you to hook almost any key.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        toggleMenu(); // show or hide the menu as required
        return true;
    } else {
        return super.onKeyDown(keyCode, event);
    }   
}

The next important part is the toggleMenu()_ method which just sets the visibility of the panel. Notice that the modifier of the method is static. This allows us to call this method from another activity. I will go into more detail below.

public static void toggleMenu() {
    if (mMenuPanel.getVisibility() == View.GONE) 
{
mMenuPanel.setVisibility(View.VISIBLE);
    } else {
            mMenuPanel.setVisibility(View.GONE);
    }       
}

Now look at Search.java above. You will notice that it also has the Overriden onKeyDown method. This is because only the currently active Activity can catch KeyEvents. So we have to Override that method in each of the activities that we are using. The difference in these activities is that they don’t have access to the panel… only CustomMenuBar has access to it… so the workaround to be able to toggle the menu is to call the method directly in CustomMenuBar. This is the static call I mentioned above. Notice that in Search.java I am calling toggleMenu() like CustomMenuBar.toggleMenu();.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        CustomMenuBar.toggleMenu();
        return true;
    } else {
        return super.onKeyDown(keyCode, event);
    }       
}





 









Tuesday 19 April 2011

Introduction to Android development

Introduction

The BlackBerry and iPhone, which have appealing and high-volume mobile platforms, are addressing opposite ends of a spectrum. The BlackBerry is rock-solid for the enterprise business user. For a consumer device, it's hard to compete with the iPhone for ease of use and the "cool factor." Android, a young and yet-unproven platform, has the potential to play at both ends of the mobile-phone spectrum and perhaps even bridge the gulf between work and play.

Today, many network-based or network-capable appliances run a flavor of the Linux kernel. It's a solid platform: cost-effective to deploy and support and readily accepted as a good design approach for deployment. The UI for such devices is often HTML-based and viewable with a PC or Mac browser. But not every appliance needs to be controlled by a general computing device. Consider a conventional appliance, such as a stove, microwave or bread maker. What if your household appliances were controlled by Android and boasted a color touch screen? With an Android UI on the stove-top, the author might even be able to cook something.

In this article, learn about the Android platform and how it can be used for mobile and nonmobile applications. Install the Android SDK and build a simple application. Download the source code for the example application in this article.


A brief history of Android

The Android platform is the product of the Open Handset Alliance, a group of organizations collaborating to build a better mobile phone. The group, led by Google, includes mobile operators, device handset manufacturers, component manufacturers, software solution and platform providers, and marketing companies. From a software development standpoint, Android sits smack in the middle of the open source world.
The first Android-capable handset on the market was the G1 device manufactured by HTC and provisioned on T-Mobile. The device became available after almost a year of speculation, where the only software development tools available were some incrementally improving SDK releases. As the G1 release date neared, the Android team released SDK V1.0 and applications began surfacing for the new platform.
To spur innovation, Google sponsored two rounds of "Android Developer Challenges," where millions of dollars were given to top contest submissions. A few months after the G1, the Android Market was released, allowing users to browse and download applications directly to their phones. Over about 18 months, a new mobile platform entered the public arena.

The Android platform

With Android's breadth of capabilities, it would be easy to confuse it with a desktop operating system. Android is a layered environment built upon a foundation of the Linux kernel, and it includes rich functions. The UI subsystem includes:
  • Windows
  • Views
  • Widgets for displaying common elements such as edit boxes, lists, and drop-down lists
Android includes an embeddable browser built upon WebKit, the same open source browser engine powering the iPhone's Mobile Safari browser.
Android boasts a healthy array of connectivity options, including WiFi, Bluetooth, and wireless data over a cellular connection (for example, GPRS, EDGE, and 3G). A popular technique in Android applications is to link to Google Maps to display an address directly within an application. Support for location-based services (such as GPS) and accelerometers is also available in the Android software stack, though not all Android devices are equipped with the required hardware. There is also camera support.
Historically, two areas where mobile applications have struggled to keep pace with their desktop counterparts are graphics/media, and data storage methods. Android addresses the graphics challenge with built-in support for 2-D and 3-D graphics, including the OpenGL library. The data-storage burden is eased because the Android platform includes the popular open source SQLite database. Figure 1 shows a simplified view of the Android software layers.

Figure 1. Android software layers






Application architecture 

As mentioned, Android runs atop a Linux kernel. Android applications are written in the Java programming language, and they run within a virtual machine (VM). It's important to note that the VM is not a JVM as you might expect, but is the Dalvik Virtual Machine, an open source technology. Each Android application runs within an instance of the Dalvik VM, which in turn resides within a Linux-kernel managed process, as shown below.
Figure 2. Dalvik VM



An Android application consists of one or more of the following classifications:
Activities
An application that has a visible UI is implemented with an activity. When a user selects an application from the home screen or application launcher, an activity is started.
Services
A service should be used for any application that needs to persist for a long time, such as a network monitor or update-checking application.
Content providers
You can think of content providers as a database server. A content provider's job is to manage access to persisted data, such as a SQLite database. If your application is very simple, you might not necessarily create a content provider. If you're building a larger application, or one that makes data available to multiple activities or applications, a content provider is the means of accessing your data.
Broadcast receivers
An Android application may be launched to process a element of data or respond to an event, such as the receipt of a text message.
An Android application, along with a file called AndroidManifest.xml, is deployed to a device. AndroidManifest.xml contains the necessary configuration information to properly install it to the device. It includes the required class names and types of events the application is able to process, and the required permissions the application needs to run. For example, if an application requires access to the network — to download a file, for example — this permission must be explicitly stated in the manifest file. Many applications may have this specific permission enabled. Such declarative security helps reduce the likelihood that a rogue application can cause damage on your device.

Required tools:

The easiest way to start developing Android applications is to download the Android SDK and the Eclipse IDE (see Resources). Android development can take place on Microsoft® Windows®, Mac OS X, or Linux.
This article assumes you are using the Eclipse IDE and the Android Developer Tools plug-in for Eclipse. Android applications are written in the Java language, but compiled and executed in the Dalvik VM (a non-Java virtual machine). Coding in the Java language within Eclipse is very intuitive; Eclipse provides a rich Java environment, including context-sensitive help and code suggestion hints. Once your Java code is compiled cleanly, the Android Developer Tools make sure the application is packaged properly, including the AndroidManifest.xml file.
It's possible to develop Android applications without Eclipse and the Android Developer Tools plug-in, but you would need to know your way around the Android SDK.
The Android SDK is distributed as a ZIP file that unpacks to a directory on your hard drive. Since there have been several SDK updates, it is recommended that you keep your development environment well organized so you can easily switch between SDK installations. The SDK includes:
android.jar
Java archive file containing all of the Android SDK classes necessary to build your application.
documention.html and docs directory
The SDK documentation is provided locally and on the Web. It's largely in the form of JavaDocs, making it easy to navigate the many packages in the SDK. The documentation also includes a high-level Development Guide and links to the broader Android community.
Samples directory
The samples subdirectory contains full source code for a variety of applications, including ApiDemo, which exercises many APIs. The sample application is a great place to explore when starting Android application development.
Tools directory
Contains all of the command-line tools to build Android applications. The most commonly employed and useful tool is the adb utility (Android Debug Bridge).
usb_driver
Directory containing the necessary drivers to connect the development environment to an Android-enabled device, such as the G1 or the Android Dev 1 unlocked development phone. These files are only required for developers using the Windows platform.
Android applications may be run on a real device or on the Android Emulator, which ships with the Android SDK. Figure 3 shows the Android Emulator's home screen.

Figure 3. Android Emulator
Android Debug Bridge 

The adb utility supports several optional command-line arguments that provide powerful features, such as copying files to and from the device. The shell command-line argument lets you connect to the phone itself and issue rudimentary shell commands. Figure 4 shows the adb shell command against a real device connected to a Windows laptop with a USB cable.

Figure 4. Using the adb shell command

Within this shell environment, you can:
  • Display the network configuration that shows multiple network connections. Note the multiple network connections:
    • lo is the local or loopback connection.
    • tiwlan0 is the WiFi connection with an address provisioned by a local DHCP server.
  • Display the contents of the PATH environment variable.
  • Execute the su command to become the super-user.
  • Change the directory to /data/app, where user applications are stored.
  • Do a directory listing where you see a single application. Android application files are actually archive files that are viewable with WinZip or equivalent. The extension is apk.
  • Issue a ping command to see if Google.com is available.
From this same command-prompt environment, you can also interact with SQLite databases, start programs, and many other system-level tasks. This is fairly remarkable function, considering you're connected to a telephone.


Coding a basic application :
This section provides a whirlwind tour of building an Android application. The example application is about as simple as you can imagine: a modified "Hello Android" application. You'll add a minor modification to make the screen background color all white so you can use the phone as a flashlight. Not very original, but it will be useful as an example. Download the full source code.
To create an application in Eclipse, select File > New > Android project, which starts the New Android Project wizard.

Figure 5. New Android project wizard
 Next, you create a simple application with a single activity, along with a UI layout stored in main.xml. The layout contains a text element you're going to modify to say Android FlashLight. The simple layout is shown below.

Listing 1. Flashlight layout
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/all_white">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" android:textColor="@color/all_black" 
   android:gravity="center_horizontal"/>
</LinearLayout>
 
Create a couple of color resources in strings.xml.

Listing 2. Color in strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Android FlashLight</string>
    <string name="app_name">FlashLight</string>
    <color name="all_white">#FFFFFF</color>
    <color name="all_black">#000000</color>
</resources>

The main screen layout has a background color defined as all_white. In the strings.xml file, you see that all_white is defined as an RGB triplet value of #FFFFFF, or all white.
The layout contains a single TextView, which is really just a piece of static text; it is not editable. The text is set to be black and is centered horizontally with the gravity attribute.
The application has a Java source file called FlashLight.java, as shown below.
 
Listing 3. Flashlight.java
package com.msi.flashlight;
import android.app.Activity;
import android.os.Bundle;
public class FlashLight extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

The code is boiler-plate directly from the New Project wizard:
  • It is part of a Java package called com.msi.flashlight.
  • It has two imports:
    • One for the activity class
    • One for the bundle class
  • When this activity is initiated, the onCreate method is invoked, passing in a savedInstanceState. Don't be concerned with this bundle for our purposes; it is used when an activity is suspended and then resumed.
  • The onCreate method is an override of the activity class method of the same name. It calls the super class's onCreate method.
  • A call to setContentView() associates the UI layout defined in the file main.xml. Anything in main.xml and strings.xml gets automatically mapped to constants defined in the R.java source file. Never edit this file directly, as it is changed upon every build.
Running the application presents a white screen with black text.




 




 




 

Friday 15 April 2011

Project Structure of Android Programming



The Android build system is organized around a specific directory tree structure for your Android project, much like any other Java project. The specifics, though, are fairly unique to Android and what it all does to prepare the actual application that will run on the device or emulator. Here's a quick primer on the project structure, to help you make sense of it all.

Root Contents

When you create a new Android project (e.g., via activityCreator.py), you
Get five key items in the project's root directory:

• AndroidManifest.xml, which is an XML file describing the application
Being built and what components – activities, services, etc. – are
Being supplied by that application

• build.xml, which is an Ant script for compiling the application and
Installing it on the device

• Bin/, which holds the application once it is compiled

• Sac/, which holds the Java source code for the application

• Res/, which holds "resources", such as icons, GUI layouts, and the
Like, that gets packaged with the compiled Java in the application

• Assets/, which hold other static files you wish packaged with the
Application for deployment onto the device

The Sweat Off Your Brow

When you created the project (e.g., via activityCreator.py), you supplied the fully-qualified class name of the "main" activity for the application (e.g., com.android.demo). You will then find that your project's src/ tree already has the namespace directory tree in place, plus a stub Activity subclass representing your main activity (e.g., src/com/ android/Demo.java). You are welcome to modify this file and add others to the src/ tree as needed to implement your application.


The first time you compile the project (e.g., via ant), out in the "main" activity's namespace directory, the Android build chain will create R.java. This contains a number of constants tied to the various resources you placed out in the res/ directory tree. You should not modify R.java yourself, letting the Android tools handle it for you. You will see throughout many of the samples where we reference things in R.java (e.g., referring to a layout's identifier via R.layout.main).

And Now, the Rest of the Part of Structure

You will also find that your project has a res/ directory tree. This holds "resources" – static files that are packaged along with your application, either in their original form or, occasionally, in a preprocessed form. Some of the subdirectories you will find or create under res/ include:

• Res/drawable/ for images (PNG, JPEG, etc.)
• Res/layout/ for XML-based UI layout specifications
• Res/raw/ for general-purpose files (e.g., a CSV file of account
Information)
• Res/values/ for strings, dimensions, and the like
• Res/xml/ for other general-purpose XML files you wish to ship

What You Get Out Of It

When you compile your project (via ant or the IDE), the results go into the
Bin/ directory under your project root. Specifically:

• bin/classes/ holds the compiled Java classes
• Bin/classes.dex holds the executable created from those compiled
Java classes
• bin/yourapp.apk is the actual Android application (where yourapp is
the name of your application)

The .apk file is a ZIP archive containing the .dex file, the compiled edition of
your resources (resources.arsc), any un-compiled resources (such as what
You put in res/raw/) and the AndroidManifest.xml file.




Inside the Manifest

The foundation for any Android application is the manifest file: Android Manifest.xml in the root of your project. Here is where you declare
What all is inside your application – the activities, the services, and so on.
You also indicate how these pieces attach themselves to the overall Android
System; for example, you indicate which activity (or activities) should appear
On the device's main menu (a.k.a., launcher).

When you create your application, you will get a starter manifest generated
For you. For a simple application, offering a single activity and nothing else,
the auto-generated manifest will probably work out fine, or perhaps require
a few minor modifications. On the other end of the spectrum, the manifest
file for the Android API demo suite is over 1,000 lines long. Your production
Android applications will probably fall somewhere in the middle.

Most of the interesting bits of the manifest will be described in greater detail in the chapters on their associated Android features. For example, the service element will be described in greater detail in the chapter on creating services. For now, we just need to understand what the role of the manifest is and its general overall construction.

In The Beginning, There Was the Root, And It
Was Good

The root of all manifest files is, not surprisingly, a manifest element:


...


Note the namespace declaration. Curiously, the generated manifests only apply it on the attributes, not the elements (e.g., it's manifest, not android: manifest). However, that pattern works, so unless Android changes, stick with their pattern.

The biggest piece of information you need to supply on the manifest element is the package attribute (also curiously not-name spaced). Here, you can provide the name of the Java package that will be considered the "base" of your application. Then, everywhere else in the manifest file that needs a class name, you can just substitute a leading dot as shorthand for the package. For example, if you needed to refer to com.joomlavogue.android.Snicklefritz in this manifest shown above, you could just use .Snicklefritz, since com.joomlavogue.android is defined as the
Application’s package.


Permissions, Instrumentations, and Applications

Underneath the manifest element, you will find:

•uses-permission elements, to indicate what permissions your application will need in order to function properly – see the chapter on permissions for more details

• permission elements, to declare permissions that activities or services might require other applications hold in order to use your application's data or logic –again, more details are forthcoming in the chapter on permissions

• instrumentation elements, to indicate code that should be invoked on key system events, such as starting up activities, for the purposes of logging or monitoring

• an application element, defining the guts of the application that the manifest describes







...

In the preceding example, the manifest has uses-permission elements to indicate some device capabilities the application will need – in this case, permissions to allow the application to determine its current location. And, there is the application element, whose contents will describe the activities, services, and whatnot that make up the bulk of the application itself.




Wednesday 13 April 2011

Android Market Changes

The Android Market is now available on the Web. Users can now find and share information about apps from their favorite browser, then purchase and download them over-the-air to their Android-powered devices. They also introduced a new feature calledBuyer’s Currency that lets you set prices for your apps separately in each of the currencies on Android Market. They are deploying this feature country-by-country over the next few months, starting with developers in the U.S. When the feature is available to you, they will notify you by email. In-app Billing is also now available on Android Market.