Android

Android - Hello World Example

Android - Hello World Example

In this section, we are going to understand actual programming with the Android framework. To do that, you need Android Studio and I assume you already installed Android SDK and are done with the set-up of environment variables. 
Let us start by creating an application in Android Studio:

  • The first step you need to do is to Start a new Android Studio Project that will ask you for some details about the project like the application name, package information, and location. 
  • After providing all these details, it will ask you to declare the supported android OS version.
  • Next, it will ask you to specify the layout of your android application.
  • At last, you are going to use the development tool to write the application. 

A development window will open in Android studio where you can write your application code. First, we are going to edit the main activity file Activity.java with the code given below:

package com.example.helloworld;
import android.support.v9.app.AppCompatActivity;
import android.os.Bundle;
public class Activity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

Now we are going to declare the components of the application in its manifest.xml file. The default manifest.xml file will look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.greatlearning.firstapp">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      
      <activity android:name=".Activity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

We also need to add strings of text that we want to show in our application. The default strings.xml file in our application will look like this:

<resources>
   <string name="app_name">HelloWorldApp</string>
   <string name="hello_world">Hello World!!</string>
   <string name="menu_settings">Settings</string>
   <string name="title_activity_main">Activity</string>
</resources>

Next, we are going to edit the layout file named activity_main.xml in our project. The following code we are going to use in this file:

<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/hello_world"
      tools:context=".Activity" />
      
</RelativeLayout>

This way, we are done with all configurations for our first android application. Now you can run the app and see how it works. 

Featured Degree & Certificate Programs

Top course recommendations for you

    Linux Tutorial
    2 hrs
    Beginner
    57.6K+ Learners
    4.47  (5612)
    Functions in Python
    1 hrs
    Beginner
    18.2K+ Learners
    4.46  (1136)
    GitHub Tutorial for Beginners
    2 hrs
    Beginner
    21.3K+ Learners
    4.44  (1481)
    Trees in Java
    2 hrs
    Beginner
    8.4K+ Learners
    4.54  (241)
    C for Beginners
    2 hrs
    Beginner
    165.7K+ Learners
    4.49  (16576)
    Binary Trees
    2 hrs
    Intermediate
    5.7K+ Learners
    4.6  (233)
    PowerPoint for Beginners
    2 hrs
    Beginner
    74.4K+ Learners
    4.53  (5238)
    UI / UX for Beginners
    1 hrs
    Beginner
    338.9K+ Learners
    4.53  (35446)
    Splunk Tutorial
    1 hrs
    Beginner
    4.3K+ Learners
    4.5  (331)
    Turbo C++
    1 hrs
    Beginner
    16.5K+ Learners
    4.41  (723)