Build project using Java

Android programming is based on Java programming language so if you have basic understanding on Java programming then it will be easier for you to learn Android application development. See the steps below to develop your Android app using Java. 

There are two ways to create User Interface (UI) on Android: (1) Write Java codes; (2) Layout via XML descriptions and let the system generates the Java code for you.

Let’s begin with writing Java codesWe shall continue from the “Hello Android” project created earlier.

MainActivity.java

Expand the “app” node. Expand the “java” node. Expand the “com.example.helloandroid” package node. Open the “MainActivity.java” (which actually has already been opened). REPLACE the onCreate() method as follows and add the import statement. Do not touch the rest of the codes.

package ......;
 
import ......;
import android.widget.TextView;   // Add this line
 
public class MainActivity extends ...... {

    // REPLACE the ENTIRE onCreate() method as follows:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);   // Construct a TextView UI component
        textView.setText("Hello, from my Java code!"); // Set the text message for TextView
        setContentView(textView);  // this Activity sets its content to the TextView
    }
    
    // Do not touch the rest of the codes, if any
    ......
    ......
}

Run the application (“Run” ⇒ “Run app”). You shall see the message “Hello, from my Java code!” displayed.

Dissecting the “MainActivity.java” – Application, Activity & View

An Android application could have one or more Activity.

An Activity, which usually has a screen, is a single, focused thing that the user can interact with the application (hence called activity). The MainActivity extends the android.app.Activity class (or android.support.v7.app.AppCompatActivity in the later version), and overrides the onCreate() method. The onCreate() is a call-back method, which is called back by the Android system when the activity is launched.

A View is a UI component (or widget, or control). We construct a TextView (which is a subclass View for showing a text message), and set its text. We then set the content-view of the MainActivity screen to this TextView.

Android Application Descriptor File – “AndroidManifest.xml

Each Android application has a manifest file named AndroidManifest.xml under “app” ⇒ “manifests”. It describes the Android app.

For example, our “Hello Android” application, with an activity called MainActivity, has the following manifest (generated automatically by the Android Studio when the project was built):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloandroid" >
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Hello Android"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
  • The <manifest> element specifies the package name.
  • The <manifest> contains one <application> element.
  • The <application> element specifies the icon, label (the app’s title) and theme of this application. It contains one ore more <activity> elements.
  • This application has one activity. The <activity> element declares its program name (“MainActivity” in current package “.“). It may contain <intent-filter>.
  • The <intent-filter> declares that this activity is the entry point (android.intent.action.MAIN) of the application. This activity is to be added to the application launcher (android.intent.category.LAUNCHER).