We saw about how to install the Android ADT in my previous post. Now we'll see how to develop a basic android app.
Open the "exclipse.exe" application from the "eclipse" folder of the installation directory. Don't get bogged down by the complexity of the layout!
We will now open a new project.
File - New - Android Application Project.
In the New Android Application window that appears, we will give a name to the application. (App names preferably start with a capital letter). The fields "Minimum Required SDK", "Target SDK", "Compile With" and "Theme" are pretty self explanatory from their names. I presume you should be aware of the fact that the version for which you are developing an app should be selected in the "Target SDK" and "Compile With" drop down list boxes. Keep clicking next and hit finish.
Now, if you are witnessing the coding behind android apps for the first time, brace yourself! Even I never expected "Java" to be here! I'm mostly from an electrical and electronics background. Though I had some coding classes at college, that was just at novice level. But, I could manage to grasp some basic concepts and believe me, these basics are more than enough for a hobbyist programmer.
On the left, there is the project explorer pane. As of now, we need to understand just a few areas we are going to meddle with. I've highlighted them for you.
"MainActivity.java" is like the main function of a c program. It is where all the function blocks are brought together and are given their functionality. The "activity_main.xml" file under the "layout" area of the "res" folder is where the various elements (like text input, number input, buttons, etc.) are defined and arranged. This will decide how the layout will look when the app runs on any android platform. Next is the "strings.xml" file under the "values" area of the folder. This is the file that holds the variables that are used in the app.
Open the "activity_main.xml" file. Voila! On the right pane, we can visualize how the app will look like when it is actually running. Don't be baffled. The default blank template comes with a "Hello world!" message already!
You may need to select the Graphical Layout tab to see this.
Give the AVD a name. I'll name it vijay. I've used Nexus 4 as the Device and "Android 4.2.2 - API Level 17" as the Target. All the remaining setting depend on the hardware configuration of your laptop or PC that is currently running the eclipse application. If you use host GPU, you'll have a faster emulation, at the cost of your laptop's graphic card! Additionally, you can select the snapshot option to optimize the speed. This will save the last state of the emulator while you close it, so that it will start quickly on your next run. (Obviously, the first run will be slower.) Close the Virtual Device Manager.
Select the "MainActivity.java" file from the Project Explorer by double clicking it. Click the run button on the tool bar or under the run menu (Ctrl + F11 is the shortcut). In the window that appears, select run as "Android Application" and click OK.
In the Android Device Chooser window, select the virtual device you just created and click OK. Now, wait patiently till the device boots up. This really tests the degree of patience in us, especially if the processor of your laptop is slow!
And when its done, its celebration time!
To maximize the emulator, use the shortcut Alt+Enter.
To change the orientation of the emulator, use Ctrl+F11.
To toggle network ON or OFF, use F8 key.
Running the app on a real device.
If you own a real android device, you could relish the fun of watching your first app perform in your own device. Firstly, you may need to enable "Developer options". It was openly available in previous versions of android. But from the version 4.2.2, they are hidden. To reveal this option, open "About device" from the settings menu and tap on the "Build number" several times. Now enable "USB debugging" under the Developer options. Connect the phone to your laptop. The USB driver will automatically be installed from the OEM site.
Now every time you click the run button in the eclipse application, you have an option to run the Android application on either the virtual device you created above or your phone.
Kudos! You've just experienced your first android app on your own phone!
Open the "exclipse.exe" application from the "eclipse" folder of the installation directory. Don't get bogged down by the complexity of the layout!
We will now open a new project.
File - New - Android Application Project.
In the New Android Application window that appears, we will give a name to the application. (App names preferably start with a capital letter). The fields "Minimum Required SDK", "Target SDK", "Compile With" and "Theme" are pretty self explanatory from their names. I presume you should be aware of the fact that the version for which you are developing an app should be selected in the "Target SDK" and "Compile With" drop down list boxes. Keep clicking next and hit finish.
Now, if you are witnessing the coding behind android apps for the first time, brace yourself! Even I never expected "Java" to be here! I'm mostly from an electrical and electronics background. Though I had some coding classes at college, that was just at novice level. But, I could manage to grasp some basic concepts and believe me, these basics are more than enough for a hobbyist programmer.
On the left, there is the project explorer pane. As of now, we need to understand just a few areas we are going to meddle with. I've highlighted them for you.
"MainActivity.java" is like the main function of a c program. It is where all the function blocks are brought together and are given their functionality. The "activity_main.xml" file under the "layout" area of the "res" folder is where the various elements (like text input, number input, buttons, etc.) are defined and arranged. This will decide how the layout will look when the app runs on any android platform. Next is the "strings.xml" file under the "values" area of the folder. This is the file that holds the variables that are used in the app.
Open the "activity_main.xml" file. Voila! On the right pane, we can visualize how the app will look like when it is actually running. Don't be baffled. The default blank template comes with a "Hello world!" message already!
You may need to select the Graphical Layout tab to see this.
Now that we are done with the development, we need to run it, to see it live!
We have two options;
- Simulate on a virtual android device.
- Use a real android device.
Running the app on a simulated device.
We have to first create a simulated device in order to run the app in it. Lets begin by clicking on the Window menu and selecting the Android Virtual Device Manager. Click New to add a new Android Virtual Device (AVD).
Give the AVD a name. I'll name it vijay. I've used Nexus 4 as the Device and "Android 4.2.2 - API Level 17" as the Target. All the remaining setting depend on the hardware configuration of your laptop or PC that is currently running the eclipse application. If you use host GPU, you'll have a faster emulation, at the cost of your laptop's graphic card! Additionally, you can select the snapshot option to optimize the speed. This will save the last state of the emulator while you close it, so that it will start quickly on your next run. (Obviously, the first run will be slower.) Close the Virtual Device Manager.
Select the "MainActivity.java" file from the Project Explorer by double clicking it. Click the run button on the tool bar or under the run menu (Ctrl + F11 is the shortcut). In the window that appears, select run as "Android Application" and click OK.
In the Android Device Chooser window, select the virtual device you just created and click OK. Now, wait patiently till the device boots up. This really tests the degree of patience in us, especially if the processor of your laptop is slow!
And when its done, its celebration time!
To maximize the emulator, use the shortcut Alt+Enter.
To change the orientation of the emulator, use Ctrl+F11.
To toggle network ON or OFF, use F8 key.
Running the app on a real device.
If you own a real android device, you could relish the fun of watching your first app perform in your own device. Firstly, you may need to enable "Developer options". It was openly available in previous versions of android. But from the version 4.2.2, they are hidden. To reveal this option, open "About device" from the settings menu and tap on the "Build number" several times. Now enable "USB debugging" under the Developer options. Connect the phone to your laptop. The USB driver will automatically be installed from the OEM site.
Now every time you click the run button in the eclipse application, you have an option to run the Android application on either the virtual device you created above or your phone.
Kudos! You've just experienced your first android app on your own phone!