Android Development(Kotlin)- First Android App

Day1: Build Your First Android App

Folder Structure of an Adroid App

AndroidMainfest.xml

  • Contains information that a device needs to run the app
  • An intermediate between android os and our application
1
2
3
4
5
6
7
8
9
10
// intent-filter: launch activity -> Launch MainActivity activity first
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
  • contains the package details of the application: package="com.example.kotlinexe1"
  • can add permissions
    • deal with the rest api: we need to provide permission to use the internet connection

Java folder

  • contains all the source code files we created
    • source code
    • test files

Jave (generated)

android studio will generate lots of classes for us and they will be stored in this folder

Kotlin language has created on top of Java language and Java library. Kotline is the more mordern version of Java, so that they haven’t changed the folder name to Kotlin

res folder

contains all non-code resources, does not contain Kotlin files but all other resources

  • drawable: contains all the images (all the format)
  • layout: contains all the xml layout files
    • activity_main.xml is the view
    • MainActivity.kt is the controller file of that view
  • mipmap: contains icon images with all different size
  • values: contains xml files to hold different default values of the project
    • defined colors
    • defined strings
    • themes sub folder: contain details about the theme of the project
      • new themes xml files inside this themes folder

Gradle

is a automated build system

  • no need to worry about the different configurations, different libraries.

  • build.gradle:

    • app level gradle file, whenever we add a new plugin to the project, we need to define it here
    1
    2
    3
    4
    plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    }
    • app level gradle filem, we need to add dependencies here
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    }

Build Your First Android App

activity_main.xml

activity_main.xml

  • code mode
  • split mode
  • design mode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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: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_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Layout type

  • constraint layout
  • linear layout
  • box layout

UI Layout Validation

provided many different screen size

MainActivity.kt

MainActivity.kt is the layout controller class of the activity_main.xml layout

The activity_main.xml file has connected to the class through this setContentView function

In Android, onCreate function is called when the activity is first created

super.onCreate(savedInstanceState) provides us the bundle containing the activities previously frozen state if there was one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.example.kotlinexe1

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val greetingTextView = findViewById<TextView>(R.id.txt_hello)
val inputField = findViewById<EditText>(R.id.txt_name)
val submitButton = findViewById<Button>(R.id.btn_submit)

submitButton.setOnClickListener(){
val enteredName = inputField.text.toString()
val message = "Welcome $enteredName!"
greetingTextView.text = message
}
}
}

Toast.makeText(context: Context!, text: CharSequence!)

for the context, we can provide either application context or activity context.

  • applicaton context: Toast.makeText(*applicationContext*)
  • activity context: Toast.makeText(this@MainActivity, "Please enter your name!", Toast.*LENGTH_SHORT*).show()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.example.kotlinexe1

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View.VISIBLE
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val greetingTextView = findViewById<TextView>(R.id.txt_hello)
val inputField = findViewById<EditText>(R.id.txt_name)
val submitButton = findViewById<Button>(R.id.btn_submit)
val offersButton = findViewById<Button>(R.id.btn_viewoffers)

submitButton.setOnClickListener(){
val enteredName = inputField.text.toString()
if (enteredName == ""){
greetingTextView.text = "Welcome!"
offersButton
Toast.makeText(this@MainActivity, "Please enter your name!", Toast.LENGTH_SHORT).show()
}else{
val message = "Welcome $enteredName!"
greetingTextView.text = message
inputField.text.clear()
offersButton.visibility = VISIBLE
}
}
}
}

navigate to next activity

1
2
3
4
5
offersButton.setOnClickListener(){
// current activity, next activity
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}

Intent

In general, intents are used to move between some android component like (activity, service, broadcast receivers …etc) some time you need to pass some value between these components so you need to use putextra in the sender component and getextra in the receiver for example :

in the sender :

1
2
3
val intent = Intent(this, MainActivity2::class.java)
intent.putExtra("USER", enteredName) // name, value
startActivity(intent)

in the reciver:

1
val userName = intent.getStringExtra("USER")

note that you need to pass the same key to retrieve your value