Android - Hello, world!

This article explains the way to create a Hello World Android application using Android Studio. Step by step guidance is provided with screenshots and the sample project is available at Git hub.

Start the Android Studio and click on the button “Start a new Android Studio Project”.


In the next window enter your application name. For the company domain provide your website URL if you have one. If you do not have a website, just give your name as com.<your-name>. According to Java standards the reverse domain name is used as package name to maintain the uniqueness, but it is not a strict rule to follow. Once you provide the domain name, package name will be created automatically. If you want to customize the package name, click on the edit link against the package name and edit it as you want. By default, the project will be saved in the Android Studio's work space directory. You can change the location if you need and proceed to the next.


Select the “Phone and Tablet” as your target, since we are going to develop our application only for Phones and Tablets. For the minimum SDK select “API 10: Android 2.3.3 (Gingerbread)”. Selecting a lower version ensures your application's compatibility from Android 2.3.3 to the latest version. However, remember that if you are selecting a lowest version as your lower limit, you can target more number of Android users but cannot use some advanced features of Android which are introduced in latest versions of Android. Click next to continue.


This is the time to select a default template for your application. Until you have any specific requirement, selecting “Blank Activity” is fine. If you select “Blank Activity”, Android Studio will create a project with a single Activity. In Android we call a window as an activity and it is match similar to JFrame in Java swing.


In the next window, Android provides you a chance to customize the default names. Since we have selected the “Blank Activity”, it asks for the name of that activity, name of the layout,  title of the activity and the name of menu resource. At this point, no need to change anything here just click the Finish button.


Android Studio will take some time to create your application and once the project has been created, Android Studio will open an activity_main.xml and a MainActivity.java in two tabs as shown below. Here activity_main.xml is responsible for user interface and the MainActivity.java is for handling the events and business logic.


On the left hand side project structure is displayed. The AndroidManifest.xml file presents essential information about your application to the Android system, information the system must have before it can run any of the app's code. The ic_launcher.png is the icon of your application and the menu_main.xml is used to define a menu for the application. Values folder is used to save some more XML resources like strings.xml, styles.xml and dimens.xml. These values XML files are used to store some values in a maintainable format, and will be discussed later in this tutorial series. Click on the Text tab which is on the bottom of the activity_main.xml tab to have a look on the XML code which generates the graphical user interface you have.


By default, there is a "TextView" with a text "Hello world!". Compare the TextView with JLabel if you are already familiar with Java swing components. For any GUI component, the andoid:layout_width and android:layout_height are mandatory properties. An optional android:id would be used, if you want to refer that component either in the XML itself or in a Java code.


Now this is the time to run your application. Click the run button (Shortcut Shift + 10). Android Studio will ask you to select a virtual device; select a device from the list as shown below and click OK.


Once the device is ready, your application will be launched automatically and displayed in the device. Now we have successfully created a Hello world application where almost every thing was created by the Android Studio. Now, replace the value of android:text in the XML code by your name and run your application. It should display your name in place of Hello world.


Now replace the <TextView>...</TextView> as shown below in the screenshot and check the output. Here we have changed the location of TextView to center of the device.


Practice Problem

Explore the GUI components available in the Palette of Design view, by drag and drop them into the parent activity. Get your hands dirty and try to create an application as shown below in the image. The requirement is creating a Graphical User Interface (GUI) for a login screen. (No need for any event handling codes).


Find the sample project at Git Hub.
Previous
Next Post »

Contact Form

Name

Email *

Message *