Introduction to Java for Android Application Development
Android-based smartphones are in vogue due to the flexibility they offer for customization. Unlike Apple’s iOS, Google Android offers better user experience in terms of applications. The Android application development kit is an open-source Linux-based operation system, which has its own middleware and key applications. The platform for app development in Android is Java. This means that you use the Java library and code the applications in Java, C, and C++ programming language. But, the most widely used programming language for android application development is Java.
Learn more about Java essentials for Android at Udemy.com
Why Java for Android application development
If you want to get started with application development, Google provides a Java API to get started and compiles your files into classes. Why did Android prefer Java for its development platform? There are multiple reasons such as; Java is a commonly used language and many programmers know it, it can run on a virtual machine (VM) so no need to recompile for different phones, better security, many development tools available for Java, and Java is a known industry language with most phones compatible with it.
Though Google provides the Java API, Android does not use JVM to execute class files. Rather, it uses Dalvik Virtual Machine (DVM). The class files are compiled into Dalvik Executable (DEX) format, and bundled as Android Package (APK) along with other resources.
With Java, if you are aware of object-oriented programming principles, creating applications for android will be much simpler than iOS app development.
Pre-Requisites
Before you can begin Java programming for Android, you will need certain tools installed. Ensure that you download the Android SDK Bundle, which includes the Android SDK and Integrated Development Environment (IDE). After you download the bundle, unzip the contents and double-click the SDK Manager.exe file. Once the installation is complete, from the Start menu, start Eclipse IDE that was bundled with the SDK.
Developing an app using Java for Android
For expert level users of Java, programming for Android will not be difficult. Let us understand how one can go about developing an application using a step-by-step approach.
- Creating an Android project in the Eclipse IDE. Here you specify certain project options such as name of the project, what Android version the project will run on, app name, class name, package name and so on.
- Configure the project and select a launcher icon. This means you are providing workspace details and selecting the appropriate size of the launcher icon.
- Creating activities is a very important aspect of app development. This activity is nothing but different activities a user gets to do on-screen. After you are done with selecting the type of activities, the project is now open with relevant resource files for you to begin the actual Java for android coding.
The user experience elements and the look of your app is defined using the activity_main.xml file in the /res/layout folder from the package. In this file, you will modify the string attributes and add views for the app. You can also add radio buttons or text fields based on the app requirement and design. By right-clicking on the element you added, you can select properties and modify them based on your layout.
New to Java programming for Android? Take a course at Udemy.com
Implementing logic using Java
After the front-end elements are finalized, the most important step remains implementing logic for all the activities to work well. The logic needs to be implemented in the MainActivity.java file from the src/com.example.tutorialapplication/ folder. The MainActivity.java file is the file that actually get converted into Dalvik compatible format and runs the application.
Let us consider that you created the front end to have a text field that displays “Udemy Online Courses” and here is the code snippet for this text display in the application.
This code snippet is for the MainActivity.java file.
package com.example.udemy;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
In the above snippet, the R.layout.activity_main element calls the activity file. The onCreate and onCreateOptionsMenu are one of the many methods that are executed when MainActivity.java file executes.
Declaring your component in Android Manifest file
The AndroidManifest.xml file found in the root of the application or project folder is where you need to declare your package for your application to execute successfully. This file bridges the gap between the OS and your application. Here is the code snippet to declare your package in the manifest file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.udemy" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
In the above snippet, all the application related resources are provided under the <application>…</application> tags. This includes the application icon, theme, app name, the name of the activity, and so on. The basic declaration made in the manifest file are <activity> for activities in the app, <service> for all the services used by your class, <receiver> for broadcast receiver, and <provider> for content providers.
Providing text element in the Strings file
All the textual elements used in the UI of the app are provided in the Strings file. This includes button names, labels, default text on-screen, and so on. The following code snippet shows the default string declarations:
<resources> <string name="app_name">Udemy Online Courses</string> <string name="udemy_courses"> Udemy Online Courses!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> </resources>
Finalizing the layout of your application
The activity_main.xml mentioned earlier is used to control the layout of the app. This means you will be altering it quite often to make your app screen optimized. The following code snippet shows default layout declarations:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:padding="@dimen/padding_medium" android:text="@string/udemy_courses" tools:context=".MainActivity" /> </RelativeLayout>
Get better at writing native mobile apps in Java at Udemy.com
Running the created application
For executing your application, you must install the Android Virtual Machine (AVM). This lets you execute your application on your computer, virtually. Click the Run icon in your Eclipse IDE to execute your application.
Recommended Articles
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.