Thursday, 5 September 2019

How to create Android First App Hello World using Android Studio


               In this post, we will see How to create Android First App Hello World using Android Studio. 


                To know how to install Android Studio in Linux, check following link:
https://www.comrevo.com/2019/01/how-to-install-android-studio-in-linux.html.

                To find details about Architecture of Android Operating System, check following link:
https://www.comrevo.com/2018/10/architecture-of-android-operating-system.html.

Explanation: 
  
To open Android Studio, open Terminal. Go to folder  /opt/android studio/bin. Run studio.sh file as follows:

parag@parag-Inspiron-N4010:/$ cd /opt/android-studio/bin/
parag@parag-Inspiron-N4010:/opt/android-studio/bin$ ./studio.sh
 
Go to the File->New->New Project. Choose any template as per your requirement. For beginning, choose Empty Activity. Then click Next.

 
Give the name to the project (e.g. HelloWorldApp). Default Package name and Save location will be given by Android Studio. Choose Language (Java or Kotlin). Choose Minimum API level. If you choose lower API level, your app can be run on more number of Android devices. Then click Finish.
 
                   We need to edit two files activity_main.xml and MainActivity.java
                   You can find activity_main.xml in app->res->layout. It is used to design Activity i.e. to add TextView, Label, Button. (Note: Frame is called Activity in Android)
                   You can find MainActivity.java in app->java->com.example.helloworldapp. It is used to implement logic in our Android App.

                   To edit activity_main.xml, we have two options (Design & Text). We can edit in Design mode where we have Drag & Drop feature. We can choose any component (e.g. Button) and can drag to the frame. Keep in mind Label is called as TextView, TextField is called as EditText (Plain Text) in Android. Then we have to set attributes for the component. Major attributes are id, name, font size etc. We also need to select location of component in Layout. Better choose location from Top and Left boundaries.

                   We can also edit activity_main.xml in Text mode.

                    We get default code in MainActivity.java. For your first Android App, we don't need to edit it. Default code is sufficient. But, for other applications which involves some logic, we have to write code in MainActivity.java.              

                  Go through the following programs:

Program (activity_main.xml)

Design Mode:
                   Drag and drop TextView component over the activity. Edit its attributes. There you can add text; can its id, can change font size, type etc.


Text Mode: 
                  Text for activity_main.xml will get automatically updated as soon as you update it in Design Mode. If you want, you can update it in Text Mode as well.


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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_marginStart="50dp"
        android:layout_marginTop="50dp"
        android:text="Hello World"
        android:textSize="50sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


Program (MainActivity.java)

package com.example.helloworldapp;

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);
    }
}
  
How to run or test our App:
                  For testing our App, we have two options. One is Emulator while other is Mobile phone.
                   To run, click on Run button (angular button) on top. A new window will be opened as shown in following screenshot:



How to test app on Mobile Phone:
To test app on Android Mobile device, we need to enable USB debugging.
  
For Android 8.0 or higher version, go to the Settings -> System -> About phone and tap Build number seven times.
   
For Android 4.2 to 7.1.2, go to the Settings -> About phone and tap Buid number seven times.

Then return to the Settings. Go to Developer options. Scroll down and enable USB debugging.

After that, on your mobile phone, you will get Key Ring. Accept it.

Then click on Run button (angular button) at the top. You will find your mobile phone as USB connected device. Choose it and click OK

                   I have run this app in my mobile phone. Output is shown in following screenshot:
 

How to test app on Emulator:
To run our app on Emulator, we need to create virtual device. For that, click on Create New Virtual Device shown in above screenshot.
Then, we need to choose Virtual Mobile Phone (e.g. Nexus) and then we have choose OS (e.g. Nougat or Oreo). 
  


               Then click Finish.

Keep in mind, for the better use of Emulator, our system (Laptop or Desktop machine) should have minimum 8 GB RAM.

               Thank you so much for reading this post. Hope, you like it.

No comments:

Post a comment