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. 

Top course recommendations for you

    Linux Tutorial
    2 hrs
    Beginner
    53.9K+ Learners
    4.48  (4986)
    Functions in Python
    1 hrs
    Beginner
    17.5K+ Learners
    4.47  (1037)
    GitHub Tutorial for Beginners
    2 hrs
    Beginner
    20K+ Learners
    4.43  (1300)
    Trees in Java
    2 hrs
    Beginner
    8.1K+ Learners
    4.52  (187)
    C for Beginners
    2 hrs
    Beginner
    148.6K+ Learners
    4.48  (13167)
    Binary Trees
    2 hrs
    Intermediate
    5.5K+ Learners
    4.57  (211)
    PowerPoint for Beginners
    2 hrs
    Beginner
    70.7K+ Learners
    4.52  (4631)
    UI / UX for Beginners
    1 hrs
    Beginner
    315.6K+ Learners
    4.53  (32814)
    Splunk Tutorial
    1 hrs
    Beginner
    4.1K+ Learners
    4.54  (302)
    Turbo C++
    1 hrs
    Beginner
    15.8K+ Learners
    4.41  (663)