Overview of Android App

Overview of Android App

Basic overview of Android App

Written by: saeed1907057

Sun, 03 Nov 2024

Console app in Java

  • Let's understand a java console application first:
    import java.util.Scanner;
    public class Bit2Byte {
        static class Calculator{
            private Adder adder;
            // assume other code here
            public void setAdder(Adder adder) {
                this.adder = adder;
            }
            public void printSum(int a, int b){
                if(adder == null) return;
                System.out.println("Sum is: "+adder.add(a,b));
            }
    
            static class Adder{
                public int add(int numOne, int numTwo){
                    return numOne + numTwo;
                }
            }
        }
    
        public static void main(String[] args) {
            final Scanner scanner = new Scanner(System.in);
    
            final int a = scanner.nextInt();
            final int b = scanner.nextInt();
    
            // assume a = 2, b = 4
            final Calculator calculator = new Calculator(); // <-------------------- (1)
            calculator.printSum(a, b);
    
            calculator.setAdder(new Calculator.Adder());
            calculator.printSum(a, b);
        }
    }
    
    • The program will get executed sequentially from the main() method as expected.
    • There is no error in the program. Now guess:
      • Does the line (1) gets executed immediately?
      • Assume inputs are given 2 and 4. Now guess the output of the above program?

Relation with GUI application

  • GUI application means application which has interface or design for input and output.
  • What do we need for a GUI application? A .java file. Anything else?
  • A java file is enough, but for simplicity, android has divided it into two parts(for Java-XML).
    • UI: This is the design part, done by XML. This is static.
    • JAVA: This is our main dynamic part.
  • So, if we want a simple page, we need a XML file and a Java file, and a way to connect them.
  • Let's create a simple page with Java part named MainActivity.java and XML part named activity_main.xml.
  • Now, the problem is:
    • A typical java file runs in terminal,
    • Previewing the xml isn't so easy for us,
    • Also we need to maintain security, lifecycle, manage activity, permission, designing component and so on...
  • Fortunately, these all are done by Android framework. I means they provide codes for all of these.
  • We just need to import them in our project and we can use them.
  • For maintaing all of these, android uses gradle build tools, which compiles our project and make it run.
  • In short,
    • Same as console application, it has a main() method, but we can't directly access it. It is handled by Android Framework.
    • Android system then initialize the application and launches a specific activity(page), and it calls the onCreate() method to notify us.
    • So, onCreate can be considered as our visible entry point.

Two components of a simple activity:

  • XML part:(activity_main.xml)
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • Java part:(MainActivity.java)
    package com.lazymind.myfirstapp;
    
    import android.os.Bundle;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
    }
    
    • As we can see, we have used a function named setContentView(), but haven't declared anywhere.
    • The function comes from the AppCompatActivity or any of its parent.
    • The setContentView takes the name of our xml(R.layout.activity_main) file, that we want to connect with. It then renders the files into UI.
      • R.layout.activity_main
        • R: resources(res folder).
        • layout: folder.
        • activity_main: name of the file.
    • AppCompatActivity contains many many functions that we can use according to our need for maintaing lifecycle or connecting.

So far

  • We have created a simple page and connected the whole xml with our java file.
  • If we run our application, we can see the app, which is basically the xml file.

Remember

  • Once an activity is started and the design is loaded, the xml is no longer needed.
  • After that, we have to do everything using our dynamic part java.
User12024-11-02

This is a comment 1.

User12024-11-02

This is a comment 2.