App Development
1. Main Function of Android Manifest File:
The Android Manifest file (AndroidManifest.xml) declares essential app
information, such as activities, permissions, and app metadata,
required by the Android system.
2. Location of Android Activity XML File:
The activity XML file is located in the res/layout folder hierarchy.
3. Creating missing xml file/folder:
Right click APPNewXMLLayout XML FileChoose File
nameChoose root tagClick Finish
4. What is dp in Android?
dp (Density-independent Pixels) is a unit of measurement in Android
used to ensure UI elements appear the same size on screens with
different pixel densities. It helps create consistent designs across
devices, regardless of screen resolution.
Formula: px = dp × (density / 160).
5. View Rendering Cycle:
MeasureLayoutDraw
6. ConstraintLayout:
ConstraintLayout is a flexible layout in Android that allows you to
position UI elements relative to each other or the parent container
using constraints. It is efficient for creating complex, responsive designs
without nesting multiple layouts.
7. Types of Intents
1. Explicit Intent
• Used to launch a specific component (like a specific activity or
service).
• Example: Navigate from one activity to another.
2. Implicit Intent
• Used to perform general actions (like sharing a file or opening a
URL), letting the system decide which app to use.
• Example: Open a web page or send an email.
8. Types of Layouts in Android:
1. LinearLayout
• Arranges elements in a single row (horizontal) or column
(vertical).
• Example: Buttons stacked one below the other or side by side.
2. RelativeLayout
• Positions elements relative to each other or the parent container.
• Example: A “Next” button placed below a “Previous” button.
3. ConstraintLayout
• Provides flexibility by allowing elements to be positioned using
constraints to other elements or the container.
• It’s efficient for complex UI designs.
4. FrameLayout
• Used to display one view at a time.
• Example: A single image covering the entire screen.
5. TableLayout
• Organizes UI elements in rows and columns like a table.
• Example: A grid of buttons.
6. GridLayout
• Arranges items in a grid structure with rows and columns, offering
more control than TableLayout.
What is Data Binding in Android?
Data Binding is a technique that connects the UI (XML layout) directly
to data sources (like variables in Kotlin). It allows the UI to update
automatically when data changes, reducing the need for manual
updates in the code.
Steps to Implement Data Binding:
1. Enable Data Binding in build.gradle:
Add the following line inside the android {} block in the app’s
build.gradle file:
buildFeatures {
dataBinding true
}
2. Use <layout> Tag in XML File:
Wrap the entire XML file content inside a <layout> tag. This enables
data binding in the layout.
Example:
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="com.example.User" />
</data>
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}" />
</layout>
3. Create a Model Class in Kotlin:
Define a data model class to represent the data.
data class User(val name: String, val age: Int)
4. Bind the Layout in the Kotlin Activity File:
Replace setContentView() with data binding code in your Activity:
import androidx.databinding.DataBindingUtil
import com.example.app.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding =
DataBindingUtil.setContentView(this, R.layout.activity_main)
val user = User("John Doe", 25)
binding.user = user
}
}
Simple Steps to Memorize:
1. Enable Data Binding: Add dataBinding true in build.gradle.
2. Modify XML: Use <layout> tag and define <variable> inside
<data>.
3. Create Model Class: Write a Kotlin class for your data.
4. Bind in Activity: Use DataBindingUtil in the activity and connect
data to the layout.
Here’s how you can make a button on the first activity navigate to the
second activity when pressed:
Steps to Implement
1. XML for First Activity (activity_main.xml)
Add a button to the first activity layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/buttonGoToSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity" />
</LinearLayout>
2. Kotlin Code for First Activity (MainActivity.kt)
Add code to handle the button click event:
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.buttonGoToSecond)
button.setOnClickListener {
// Start the second activity
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}
}
3. XML for Second Activity (activity_second.xml)
Design the second activity layout (e.g., show a message):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Second Activity"
android:textSize="18sp" />
</LinearLayout>
4. Kotlin Code for Second Activity (SecondActivity.kt)
Add a simple activity class:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
}
}
5. Update AndroidManifest.xml
Declare the second activity in the manifest:
<application>
<activity android:name=".SecondActivity" />
</application>
How It Works
1. The button in MainActivity is clicked.
2. Intent is used to start SecondActivity.
3. SecondActivity opens, showing its layout.
Code to Memorize
1. Button XML:
<Button
android:id="@+id/buttonGoToSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity" />
2. Kotlin for MainActivity:
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)