Thursday 26 June 2014

Create First App

Once you have your development environment completely setup and ready to go, it’s time to create your first app. Sticking with tradition, your first app is going to be the venerable “Hello World” app. Although this app is very simple to create, it’s important because it familiarizes you with many Eclipse features and tests your development environment to make sure everything has been installed and configured correctly.
Although many of the components discussed in the previous section are not present in the Hello World application, you will have a much better understanding of basic Android principles by completing this exercise.
After you have successfully created and compiled Hello World, you will learn about some other techniques including a way to start creating 2D games without any programming experience. Although it may be tempting to skip to that section, every good Android developer knows how to code at least a basic application from scratch and you are no exception.
Don’t forget to check out Learn by Doing – Android for Beginners for a solid overview of Android basics in an easy-to-follow format.
To create Hello World, start by opening Eclipse and clicking New from the toolbar. In the dialog box that opens, you need to fill out the information for your application. The fields are as follows:
  • Application Name – This is the app name that appears to your end-users. For this project, make the Application Name “Hello World.”
  • Project Name – This is the working name of your project within Eclipse. Type HelloWorld into this field.
  • Package Name – The Package Name is used to differentiate your app from millions of others in the Google Play Store. For this app, you can use com.example.helloworld but for applications you choose to release to the public, Google won’t allow the com.example package name. You can use the inverse of your web domain (com.yourdomain.helloworld) or your last name or business name (com.yourname.helloworld). The idea is that the package name has to be completely unique among all published applications.
  • Minimum Required SDK – You can specify the lowest version of Android supported by your application. To ensure your app is accessible to the highest number of people, keep this set to the default setting (currently Froyo 2.2).
  • Target SDK – This is the highest version of Android your application has been tested with. As new versions of Android are released, you should test your application and update the API whenever possible to keep your app current. You can set this to whatever setting you want, but remember that Android 4.4 (the latest release) is currently only available on a few devices so maybe set it to 4.1 or 4.2 for now.
  • Compile With – This setting tells Eclipse how to compile your application. By default, it should be set to the latest version of Android (if this is not available, download the API using the Android SDK Manager).
  • Theme – This setting specifies the Android UI that will be used by default for your application. You can leave this setting alone for now but feel free to experiment with it as your skill level progresses.
Click Next to confirm these settings. The following screen shows additional settings for your app. You can leave all these settings at their default values and click Next again to continue. Another screen helps you create an Icon for your app. For now, leave this alone but remember that a real app needs an appropriate Icon. You can learn more about creating Icons for your apps in the official Android Iconography guide available athttp://developer.android.com/design/style/iconography.html.
The final screen in the setup of your application is the Activity dialog box. For Hello World, you can choose BlankActivity and click Next. Leave all the default settings for this activity alone and click Finish.
OK…so now all that is out of the way and you can actually code your app! Although many of the steps you just went through seemed rather tedious, the dialog boxes are very important when creating Android applications. In fact, many of the files your app needs to run properly were automatically created during the project creation phase of Eclipse.
Now you should find yourself in the Workroom of your Hello World project. Along the left side of the window you will see the Package Explorer. Navigate to the BlankActivity you created which should have been named MainActivity.java by default. Double-click on this file to open it and you are ready to start coding!
Below you will find the code needed to create your first Hello World application. Some of the code should already be in your MainActivity.java file so just add the missing pieces to complete the application.
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
                       @Override
                       protected void onCreate(Bundle savedInstanceState) {
                                        super.onCreate(savedInstanceState);
       TextView text = new TextView(this);
       text.setText(“Hello Android – Thanks for Using Udemy!”);
       setContentView(text);
              }
}
That’s it! Your app is now ready to be compiled and run on a real device or the AVD you created earlier.

Running Your App on a Real Android Device or Using AVD

Installing your app on an actual Android device isn’t hard, but you do need to make sure you have the appropriate drivers installed for the device. On a Windows machine, these drivers are often installed automatically but make sure they are installed correctly so Eclipse can recognize the device.
If you need additional help installing the proper drivers for your test device, please consult the official Android OEM USB Driver document athttp://developer.android.com/tools/extras/oem-usb.html.
Also make sure the device has USB Debugging enabled. On most Android devices, this setting can be found in Settings > Applications > Development. On some of the newer devices equipped with Android 4.2 or above, the Developer options are hidden. To unlock these settings, go to Settings > About Phone and tap on Build Number seven times to unlock the Developer options and make sure USB Debugging is enabled.
With your device plugged into the computer, click Run in Eclipse and selectAndroid Application from the Run As menu. Eclipse will automatically install the app and it should be located in the App Drawer of the test device under “Hello World.”
Click on the app to run the Activity and your screen should display the message you typed early on a white background and the name of your app should appear on the top (right below the status bar).
The process for running your app on an AVD is just as simple. When Eclipse detects a physical device attached, it will automatically install the Android app to the device. If no device is present, it will automatically install to your default AVD. Of course, you need to create at least one AVD (which is why this process was covered earlier) before Eclipse can use this device for testing and debugging.
This is a very simple app but it shares its structure with more complex apps. The Android Manifest, Java source files and other package components are the same in your app and the more complex ones you will go on to create in the future.
Feel free to experiment with the code in this tutorial. Its very easy to create a button that displays the message when pressed. You can learn more about using ActionListeners to create this type of effect in Android Programming Tutorial Videos for Beginners.

No comments:

Post a Comment