Android Programming Tutorial – Create Your First Android App
This post will show you how to create your first Android app. And not the dreaded “Hello World!” app but one that teaches you a little more than that. Android development may be the next great skill for programmers. According to the International Data Corporation Android sales increased by over 45% and this growth means more and more people will have access to Android devices which means an increase in demand for apps for those devices. As a programmer, it is important to keep up to date with the latest technology and Android development is on the forefront of that new technology.
Luckily, Android is based on some of the technologies that you may already know as a programmer. To develop in Android, you need some skill in XML and Java. This tutorial will not include lessons on those but assumes you have some experience in both of those technologies. If you want to learn Java for Android or are looking to bring your Java skills up to scratch, check out Java Essentials for Android.
This course contains over one hundred lectures that will teach you the basics of Java. It covers the basic Java data types used in Android programming and it teaches you to work with strings. It includes lessons on the Java control structures like the if-then-else loop and while loops. It includes instruction on methods and classes and how they function. It also covers inheritance, interfaces and polymorphism. The course also includes an introduction to Android programming and how to work with lists and arrays.
So Let’s Create Our App
This app will be created using the Android SDK developer’s kit that is available for free from the Android website. We will be using Eclipse to create our app. To create an app you need to start Eclipse and then select file and then new Android project. Name your project and the folder and then use a unique name for your package name. If you want to upload to Google then you need a unique package name.
We will leave the settings as is for the next screen, so click next and then choose an icon for your app on the next screen. For this tutorial we will use the built in icon:
On the next screen select blank activity to create a blank app file and then on the final screen, you can enter a custom name for your main XML file but for this tutorial, we will use the default file names. Leave the navigation at none for now to create a simple app. Click finish and wait for Eclipse to build the files for your app.
Congratulations. You have just created your first app. It doesn’t do anything yet, but we will start building that part soon. This is the screen once the app files are created:
The files are docked in the tab on the left. There are some form and widget elements next to that. You get an Android emulator in the middle and then the properties window on the right. We will be using these to create our calculator app.
If you want to learn to program apps, then join thousands of students taking the Android 101: Android Development for Beginners course and start coding your own Android apps today. This course includes a full review on Java including data types, coding, lists and arrays. It also includes lessons on the object-oriented nature of Java and how that impacts programming for Android. It includes step-by-step lessons on how to create your first Android app and how to create listeners. It will teach you what Android containers are as well as what widgets are and how to work with them. It will also teach you how to add audio and video to your Android projects.
Access the Main Java File
Now that we have created the app, we need to open the main Java file so that we can code our app. To find the main Java file, select your project from the file list, and then select the src folder and your project folder and you will find your main Java file. Double click the file to open it as shown below:
You should have two files open namely the main java file and the main XML file. We are going to add a few elements to our layout first in the XML file to create elements for our app. When the app is built, a “Hello World” text field is automatically added to the app, so we are going to delete that since we do not need that text field. Just select the text field and hit delete to delete that text box. Your Android emulator should now be empty:
Use Form Widgets to Add a Text Field and a Button
The form widgets box contains a number of widgets you can add to the layout of your app and then access via Java to create your app.
Now we will add a title for our app. Drag the Large Text widget onto your app and then use the properties box to change the text of the title and the id name of the title:
And we will add a number field to allow for calculations. Select the text field widget menu and drag the number decimal text field to the app:
The program creates a default name for our text field but we will want to access it in our Java code so we will rename the text id to number. To allow for decimals and negative numbers we need to change the input type to include numberSigned. Click the ellipses after the input type and select numberSigned and then click okay.
Now we will add a button so the user can click to calculate. Drag a button from the widget menu, rename the button to btncalculate and rename the field text name to calculate:
Finally we add a test field for the answer to the calculation:
We now have all of the elements we need for our application so lets go ahead and create the Java code we need to make it all run.
Adding the Java Code
Click on your main Java file to access the Java code. We need to create instances of the field we created so we can get the input from those fields. So to create instances of those we use the “EditText” Java code. We will create instances for the number field first. So add the following code to the Java:
This creates an instance of num and also assigns the text field we created for that instance to a variable called “number.” Once entered, you will notice that there is a cross next to the line. This is a sign that there is a problem with the code. This time the problem is that we have not included the library necessary for the code. So we need to add an import statement at the beginning of the file to import the file. The error files give you a clue about the error so if you click on the cross, you can select the file to import and it will automatically be added for you:
We know we will be using other widgets so change the EditText to * to import all of the widgets.
Now we add another instance for the display field and the button:
Now that we have created the instances of our fields, we will create listeners for them.
Create the Listeners
We are going to add an event listener so that when the user clicks the button, the app does a simple calculation. You need to add the following code:
All we are doing is creating a double to hold the calculation result. Reading and parsing the input text and then creating a simple calculation by using the num = num * 5. We then set the display text to the answer.
Run Your App
Now you can run the application to see how it works. Select the run button and choose Android app:
Now go for a cup of coffee because the emulator can take a really long time to start and you deserve a cup of coffee, because you are now officially an Android developer.
Take your Android programming skills to the next level with Developing a Drawing App for Android. This course will teach you how to create an Android drawing app. The course includes lessons on how to create great looking UI’s for your apps, how to adjust the XML files for your layouts, how to create menus and code buttons. It will show you how to handle touch events on Android. It will also show you how to create a canvas for drawing and how to add a menu option.
Android development will test your XML and Java skills and learning to program in Android may take some patience and practice but the rewards are well worth it.
Android Programming: The Linear Layout
5 Android Tricks for a Smarter Smartphone
Android 2.3 vs. 4.0: What Changed
Android vs iOS Development: How Do They Compare?
Top courses in Android Development
Android Development students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.