Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:21Length:2.6 hours

Next lesson playing in 5 seconds

Cancel

Free Preview: Learn Java for Android

Introduction

01:20
  • Overview
  • Transcript

Android is the world's most-installed mobile OS, running devices as different as phones, tablets, watches and TVs. By developing for Android, you can create apps for all these devices. But before you start coding Android apps, you'll have to learn the Java programming language!

In this course, Envato Tuts+ instructor Sue Smith will help you master the basics of Java programming, with a focus on the Android platform. In this practical course, you will use object-oriented programming and the Android SDK to build a basic note-keeping app. Along the way, you'll discover the fundamentals of the Java language and the essential features of Java coding for Android.

1. Introduction

1.1 Introduction

Hello, welcome to the Tuts+ Learn Java for Android course. My name is Sue Smith. In this course, we will learn basic foundation and Java programming for the Android platform. We'll explore the fundamentals of object-oriented development in Java, and we'll build the outline of a basic app for note keeping. The app will present a list of notes to the user and allow them to add new notes. What you'll learn in the course will put you in the position to continue developing the app afterwards. We will create class declarations, use objects, and headings, and Java interfaces to code our app using existing resources. If you've programmed in other languages, perhaps web languages like PHP and JavaScript, no matter what level of expertise you've developed, you will be able to learn the basics of Java programming during this course. At the end of the course, you will have learned enough about programming in Java to develop functional, robust Android applications.

2. Language Basics

2.1 Statements

Hello. Welcome to the Tuts Plus Learn Java for Android course. My name's Susan. Let's get started by looking at some Java code. In this line, we declared a variable. If you've worked mainly with languages for web development, such as PHP in JavaScript. One of the main differences you'll see with Java is that it is a strongly typed language. This means that when you declare a variable, you specify the type. In this case we are declaring an integer variable using the keyword int. An int is a primitive type. Primitive types in java are predefined as part of the language, as opposed to object types, which we'll see later. In the variable declaration, the type is followed by the name of the variable. In this numNotes. Notice that the variable name uses camel case, starting with a lowercase character and each section of the name after that starting with an uppercase character. This is standard in Java. Let's initialize the variable. Assigning an initial value to it. On the right side of the equal sign is the value, so this is now an assignment statement. There are eight primitive types in Java. Int, double, float, short, long, byte, char, and boolean. In short double float and long are all number types. The byte type is an 8 bit sue's compliment integer. The boolean type indicates a value of true or false. And the carrot type stores a single character value. And it's one of two main types for storing text in Java, the other being the string. The string is a sequence of character values. Strings are actually objects in Java but they have some of the same behavior as primitives so we'll be looking at strings in more detail later. Primitive values can be included in your Java code without being stored in a variable. For example, this line could be used to write a message to the Android log while your application's running. The code writes a portion of text to the log together with a tag. And we'll be using this structure throughout the course once we start working in Android studio. The message output includes our text string and the number. When the values of included data lay in accord like this they are described as literals. So, we have a string literal and an int literal. There are a few types of comment Java. These two are the most common. Single line comments are useful, for example, before variable declarations. Multi-line comments are useful where you need a bit more detail. For example, before a method, or to explain functionality. Let's finish this lesson by switching to Android Studio where we'll create an android application project and add a small amount of code to it. To create a new project, open Android Studio and choose file, then new. Menu project. Android Studio will prompt you to choose a name for your new application. We are calling ours NoteKeep. Make sure the name has no spaces. And use camel case if you want to observe the standards. You can leave the other settings as they are for the moment and click next. The next thing allows you to target particular types of device with your app. Again, you can leave the default option set for the moment. For future apps you might want to alter these, particularly the PI level. Next, we choose an activity type. Activity is the name we use for a single user interface screen on Android. There are lots of options here. But for the national app, we'll choose a blind captivity. The final screen sets up some of the components in the app, including the initial Java and XML layout finals. Again, you can leave these at the default settings for now, but when you code your own apps, you might want to change them. When you click finish, Android studio will create a new app and open it in a new window. This process might take a few minutes. But when it's complete, you'll be able to see and interact with the components in your new project. Initially you'll see the xml leo file. Which we'll look at later in the course. Open the MainActivity file, which is the Java class file for the activity that we'll run when your app is launched. Don't worry too much about classes at this stage, we'll get to them later. You can see the directory structure above the editor area. You can also interact with the project structure in the sidebar, where you can navigate to the files. The initial code we enter for this course will be in the OnCreate method of the main activity file. Since this executes when the app runs. But don't worry too much about Java methods at this stage because we'll look into them soon. All you need to know for now is that the code between the opening and closing brackets for on create will run when that runs. Let's add some code to that. Notice that the code also includes a comment line above the variable declaration describing its purpose. Add this code inside the onCreate method after the set content view line. Throughout the course we will be trying out various aspects of Java programming for Android to build a basic note keeping app. So this code will eventually form part of that. At this point you can save your code and take a moment to look over the other code in the activity file. Android Studio entered this code when you created the new project, and it contains a lot of structures that you'll become familiar with while you learn Android development. In the next lesson we'll have a look at some of the other operators in Java.