Creating a simple ‘Hello World’ application is the traditional way to start learning a new programming language or platform. In this blog, we’ll walk you through the process of creating your first Android application that displays the text “Hello, World!”.
Step 1: Create a New Project
Open Android Studio and select New Project
. In the New Project
dialog box, choose Empty Activity
, and then click Next
.
Step 2: Configure Your Project
On the Configure your project
page, fill in the following fields:
- Name: Hello World
- Package name: in.infovistar.helloworld
- Save location: Choose a location on your computer to save the project.
- Language: Java
- Minimum SDK: API 21: Android 5.0 (Lollipop)
Click Finish
to create your project.
Step 3: Update the Layout File
Navigate to the activity_main.xml
file located in app > res > layout
. This XML file defines the layout for the activity’s user interface (UI). Replace the existing code with the following:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello, World!"
android:textSize="30sp" />
</RelativeLayout>
Step 4: Main Activity File
The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application. Following is the default code generated by the application wizard for Hello World! application −
package in.infovistar.helloworld; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Step 5: Manifest File
When developing an application, it is important to declare all components in a manifest.xml file located at the root of the project directory. This file serves as an interface between the Android operating system and your application. If a component is not declared in this file, it will not be recognized by the OS. A default manifest file will typically look like the following example.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.infovistar.helloworld"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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>
Congratulations on developing your first Android application! Follow the tutorial step-by-step to become a great Android developer. Best of luck!