Friday 27 July 2012

Running the Android Application

After we have created an application we would want to test and run it, obviously !

So now lets see how we can do so. We first create a sample hello world program and then run it.

While using the Eclipse IDE you would see that the project wizard by default provides us with a ready made, ready to run Hello world program. That means that we already get the following:

  • layout - The graphical user interface(GUI)on which we see the output and interact with our application.It is a .xml layout file.It is located in the directory workspace\your_application\res\layout\activity_main.xml
  • src - The .java class which has the code and all the logic enables us to interact with the GUI. It is located in the directory workspace\your_application\src\com\tutorial\example\MainActivity.java 
  • Android manifest - The .xml file which enables us to debug our application.Without this the debugging can not be done.It is located in the directory workspace\your_application\AndroidManifest.xml
  • string - Is a .xml file that helps us to reference resources by using the name attribute of it.It is located in the directory workspace\your_application\res\values\strings.xml.
So the ready made code that we get is :
MainActivity.java

public class MainActivity extends Activity//Create a new activity
{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);// Set the content on a particular layout .
    }
    
}
String.xml

<resources>

    <string name="app_name">examples</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>

</resources>

Android Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorial.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="6"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

so without even writing a single line of code we get this.:)

Running application on AVD:

  1. Go to window in the Eclipse IDE
  2. Select AVD manager
  3. create new AVD
  4. Set the properties for the AVD
  5. Click create AVD
  6. Next, go to your application 
  7. Right click on it and select "run as"
  8. go to run configuration
  9. now in the new window that opens up create a new configuration 
  10. set the target device as the AVD that you just created 
  11. Click apply
  12. Click Run.

Running application on a real Android Device:

First of all make sure that you install the drivers of your device on your PC.Then follow the following steps:
  1. Connect your device to your PC with the USB cable .
  2. Mount your android device.
  3. On your device go to settings>>Applications>>Development
  4. Enable USB debugging.
  5. Next, go to your application 
  6. Right click on it and select "run as"
  7. go to run configuration
  8. now in the new window that opens up create a new configuration 
  9. set the target
  10. select the radio button"Launch on all compatible devices/AVD"
  11. Select "active devices"

No comments:

Post a Comment