Unit 1
1 Difference between explicit and implicit
Point Explicit Intent Implicit Intent
1. Definition Starts a specific app component Requests an action to be handled
by any suitable app
2. Component Yes (e.g., SecondActivity.class) No, system finds suitable
Specified component
3. Used For Starting activities or services Performing actions like view,
within the same app send, capture, etc.
4. Target Known? Yes No
5. System Directly launches the specified System chooses the best app to
Involvement component handle the intent
6. Code You write the class name of the You write the action in code
activity in code
7. App-to-App Not used for cross-app Commonly used for
Interaction communication communication between apps
8. Intent Filters Not necessary Required in receiving
Needed? components (defined in
manifest)
2 Various Layouts in Android – Explained Simply
In Android, layouts are used to arrange the user interface (UI) elements on the screen. Each
layout type has its own way of organizing views like buttons, text, and images. Here are some
commonly used layouts in Android:
1. LinearLayout
LinearLayout arranges its child views in a single direction, either vertically (top to
bottom) or horizontally (left to right). It is simple and useful when you want all elements
in a straight line. For example, if you want to place a TextView above a Button, you can
use a vertical LinearLayout.
2. RelativeLayout
RelativeLayout allows views to be positioned relative to each other or to the parent
layout. For example, you can place one button to the right of another, or below it. This
layout is more flexible than LinearLayout, especially when you want to avoid nesting
multiple layouts.
3. ConstraintLayout
ConstraintLayout is a powerful and flexible layout that lets you position views using
constraints. Constraints are like rules that tell a view where to appear (e.g., center,
topleft, below another view). It helps reduce the number of nested layouts and is ideal for
complex screens.
4. FrameLayout
FrameLayout is designed to block out an area on the screen to display a single view.
Though you can add multiple views, they will be drawn on top of each other, like layers.
It's commonly used for things like displaying a full-screen image or fragment container.
5. TableLayout
TableLayout organizes views into rows and columns, similar to a table. Each row is a
TableRow object, and views are added in cells. It's good for displaying form-like UIs
where fields are arranged in a tabular format.
6. GridLayout
GridLayout also arranges views in rows and columns but provides more control over
how views span across multiple rows or columns. It's more modern and flexible than
TableLayout and can be used for designing grid-based UIs like image galleries.
7. ScrollView
ScrollView is a layout that allows users to scroll the screen vertically. It is useful when
the content is longer than the screen size, like when displaying long forms, articles, or
lists. It can contain only one child view (which is usually a LinearLayout).
8. HorizontalScrollView
This is similar to ScrollView, but it allows content to be scrolled horizontally. It is useful
when you want to show items side by side that go off the screen, like a horizontal image
slider or category tabs.
Linear layout program: XML file
<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"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>
Main_activity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
button.setOnClickListener(view -> textView.setText("Button Clicked!"));
}
}
3 Android Activity Lifecycle (Explained Simply)
1. onCreate()
• This is called when the activity is created for the first time.
• You use this to set up the screen layout and initialize things.
• Example: setContentView() is called here to display the UI.
2. onStart()
• This is called when the activity becomes visible to the user.
• The activity is not yet in the interact , but it's about to be.
3. onResume()
• This is called when the activity is ready for the user to interact with.
• This is the state where the app is active and running.
• Example: You can start animations or resume paused tasks here.
4. onPause()
• This is called when the activity is still visible but partially covered, like when a dialog
pops up or another activity is opening.
• Use this to pause animations, save data, or stop camera or audio.
5. onStop()
• This is called when the activity is no longer visible.
You should release heavy resources here (like sensors, GPS, etc.) to save battery and memory.
6. onRestart()
• This is called after onStop() if the activity is coming back again.
• Useful to reload any data or UI updates before showing the activity again.
7. onDestroy()
• This is called before the activity is completely destroyed (like when the user closes the
app or system kills it).
• Use this to clean up everything like removing listeners or saving the final data.
Unit 2
1 explain main activity , manifest, string and layout
1. MainActivity
• MainActivity is the entry point of an Android app. It is the first screen the user sees when
they open the app.
• In every Android app, MainActivity is typically the starting activity defined in the
AndroidManifest.xml file.
• This is where you write the code to set up the user interface (UI) and handle user
interactions.
Example:
o When a user opens your app, MainActivity runs first, and it might display a
welcome screen, buttons, or navigation options.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
button.setOnClickListener(view -> textView.setText("Button Clicked!"));
}
}
2. AndroidManifest.xml
• The AndroidManifest.xml file is a configuration file in your Android project that tells the
Android system how to launch your app and how to manage its components.
• It includes important information like:
o App name and icon
o Permissions (like internet access)
o Activities (like MainActivity) and their relationships
o Services and Broadcast Receivers
Example:
o If you want your app to use the internet, you add the necessary permission in this
file: <uses-permission android:name="android.permission.INTERNET"/>.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:label="MyApp"
android:theme="@style/Theme.AppCompat.Light">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
3. Strings (strings.xml)
• The strings.xml file is where you store all the text that your app will display (like labels,
buttons, messages).
• By placing these in a separate file, it helps you manage multi-language support
(localization), as well as easily change text without modifying the code.
• This file is located in the res/values/ folder of your project.
Example:
o If you have a button that says "Submit", instead of hardcoding "Submit" in your
layout file, you can use a string reference:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyApp</string>
<string name="welcome_text">Welcome to MyApp!</string>
<string name="button_text">Click Me</string>
</resources>
4. Layout (layout.xml)
• A layout file in Android defines how the UI (user interface) elements are arranged on the
screen. It is written in XML format.
• The layout file contains views (such as buttons, text fields, and images) and ViewGroups
(containers like LinearLayout, RelativeLayout, etc.) to organize these views.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:text="@string/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:text="@string/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2 date picker and time picker
1. DatePicker (Pick a Date)
• DatePicker is a widget that allows the user to choose a date, which includes day, month,
and year.
• It can appear in two ways:
o As a dialog box (using DatePickerDialog).
o Directly in your app layout using the DatePicker widget.
Common Methods for DatePicker:
• setDate(int year, int month, int day): Sets the date in the DatePicker.
• getYear(): Gets the year selected by the user.
• getMonth(): Gets the month selected by the user (0-11).
• getDayOfMonth(): Gets the day of the month selected by the user. 2. TimePicker (Pick
a Time)
What is TimePicker?
• TimePicker allows the user to pick a time. It includes hour and minute (and sometimes
AM/PM for 12-hour format).
• It can appear in two ways:
o As a dialog box (using TimePickerDialog).
o Directly in your app layout using the TimePicker widget.
Common Methods for TimePicker:
• setCurrentHour(int hour): Sets the hour in the TimePicker.
• setCurrentMinute(int minute): Sets the minute in the TimePicker.
• getCurrentHour(): Gets the hour selected by the user.
• getCurrentMinute(): Gets the minute selected by the user.
Example Code for date and TimePicker Dialog:
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button dateButton =
findViewById(R.id.dateButton);
Button timeButton =
findViewById(R.id.timeButton);
// DatePickerDialog
dateButton.setOnClickListener(v -> {
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(this, (view, year, month,
dayOfMonth) ->
Toast.makeText(this, "Date: " + dayOfMonth + "/" + (month + 1) + "/" + year,
Toast.LENGTH_SHORT).show(), calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
).show();
});
// TimePickerDialog
timeButton.setOnClickListener(v -> {
Calendar calendar = Calendar.getInstance();
new TimePickerDialog(this, (view, hourOfDay,
minute) ->
Toast.makeText(this, "Time: " + hourOfDay + ":" + minute,
Toast.LENGTH_SHORT).show(), calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), true
).show();
});
}
3 directory structure in android project
• MyApp/ – Root folder of your Android project.
• app/ – Contains all the app-related code and resources.
• src/ – Source folder where your code and resources are stored.
• main/ – Main source set for your app’s code and assets.
• java/ – Contains all your Java/Kotlin files (logic of your app).
• res/ – Stores all app resources like layouts, images, and strings.
• layout/ – XML files that define the UI of activities/fragments.
• drawable/ – Contains images and drawable XML files.
• mipmap/ – Holds app launcher icons in different resolutions.
• values/ – Stores XML files for strings, colors, dimensions, styles, etc.
• AndroidManifest.xml – Declares app info, activities, and permissions.
• build.gradle (Project) – Configuration file for the whole project.
• build.gradle (Module: app) – Configures dependencies and build settings for the app.
• settings.gradle – Lists modules included in the project.
3 What is a Spinner?
A Spinner in Android is just like a drop-down menu or combo box.
It shows a list of items (like cities, countries, options, etc.), and the user can select only one item
from that list.
When the user taps on it, the full list opens. After choosing an option, it shows the selected item
on the screen.
Why use a Spinner?
• To let the user pick one value from a list.
• Saves screen space (compared to radio buttons).
• Commonly used for:
Choosing gender
Selecting language
Picking a category or location
Method
setAdapter() - Sets list of items to display in the Spinner setOnItemSelectedListener() Detects
when the user selects something
getSelectedItem() - Gets the selected item from the Spinner
Ex [ India ▼ ] → when clicked shows:
- India
- USA
- UK
- Canada Program
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner mySpinner = findViewById(R.id.mySpinner);
String[] countries = {"India", "USA", "UK", "Canada"};
mySpinner.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, countries));
mySpinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() { public void
onItemSelected(AdapterView<?> p, View v, int pos, long id) {
Toast.makeText(MainActivity.this, "Selected: " + p.getItemAtPosition(pos),
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> p) {}
});
}
Unit 3
1. Define Menu. types of menus
In an android. Menu is set of commands or options where user can choose from typically
displayed list when they interact with button or action.
Point Options Menu Context Menu Popup
Menu
1. Trigger Tapped on the menu Long press on a view/item Tapped on a
button or 3 dots view/button
2. Location Appears in the Action Bar Appears near the item Appears near
or App bar the
button/view
3. Menu Type Shows a list of general Shows options related to Shows a
options the item small
floating
menu
4. Example Settings, Logout, About Us Copy, Paste, Delete Edit, Delete,
Usage Share
5. Menu Source Defined in XML Defined in XML Defined in
(res/menu) (res/menu) XML
(res/menu)
6. Appearance Displays in a vertical list Displays as a vertical list Displays as a
small
floating
list
7. Visibility Available everywhere in Appears when long Appears on
app pressed on item button click
8. Number of Can have multiple Typically has few options Usually has
Items options 2-
3 options
9. Code Uses Uses Uses
Implementation onCreateOptionsMenu() onCreateContextMenu() PopupMenu
and and class and
onOptionsItemSelected() onContextItemSelected() show()
2. Theme and style
Point Style Theme
1. Definition Set of design rules for one Set of design rules for the whole app or
UI element screen
2. Scope Affects single views like Affects entire app or activity
Button, TextView
3. Where used Used in layout XML files Set in AndroidManifest.xml or in
Activity
4. Purpose Reuse design for multiple Apply a consistent look across the app
widgets
5. Syntax style="@style/MyStyle" in a android:theme="@style/MyTheme" in
widget Manifest or code
6. Contains UI appearance only (color, UI appearance + behavior (status bar,
size, padding) window, ActionBar)
7. Affects ❌ No ✅ Yes
ActionBar?
8. Inheritance Can inherit from other styles Can inherit from other themes
9. Control over ❌ No ✅ Yes (e.g. status bar color, navigation
system UI? bar)
10. Example Style a single button with Make all buttons red across the app
Use red text
3. Screen Navigation in Android
refers to the process of moving between different screens (or activities) within an app. It helps
users navigate the app and go from one screen to another, depending on their actions.
Types of Screen Navigation:
1. Intent-based Navigation
This is the most common way to navigate between screens. You use intents to open
different screens (activities) in your app.
2. Explicit Intents
This is when you tell the app exactly which screen (activity) you want to open.
3. Implicit Intents
Here, you don’t tell the app which screen to open. Instead, you tell it what action you
want (e.g., open a webpage), and the app decides which screen can handle that action.
4. Back Stack
The back stack is like a list of the screens you've visited. When you press the back
button, it takes you to the previous screen, based on the list.
5. Fragments
Fragments are parts of a screen. Instead of switching between whole screens, you can
swap or add fragments inside a single screen (activity) to change what the user sees.
6. Navigation Component
This is a new, easier way to manage screen navigation. It uses a navigation graph to
define where you can go and how to get there. It helps in managing screens without much
code.
7. Toast & Snackbars
These are messages that appear at the bottom of the screen. Toast shows a simple
message, while Snackbars can show actions like "Undo" with the message.
8. Bottom Navigation
A menu at the bottom of the screen allows users to quickly switch between major sections
of the app.
9. Drawer Navigation
A sliding menu from the left side of the screen that lets users navigate between different
sections of the app.
Unit 5
1. Advantages and disadvantages of SQLite database in android
Advantages of SQLite in Android:
1. Built-in:
SQLite is already included in Android, so no need to install anything extra
2. Lightweight:
It uses very little memory and storage—great for mobile apps.
3. Simple to Use:
Easy to create, read, update, and delete data with SQL commands.
4. No Server Needed:
Works directly in your app—no need for a separate database server.
5. Fast:
Good speed for small to medium-sized apps.
6. Reliable:
SQLite is tested and widely used, so it's stable and safe.
Disadvantages of SQLite in Android:
1. Not Good for Big Data:
Slower and harder to manage with large or complex databases.
2. No Built-in Encryption:
Data is not encrypted by default, so security needs extra work.
3. Manual Work:
You have to write SQL code and handle upgrades (e.g., table changes) manually.
4. Limited Multi-User Support:
Not ideal for apps that need multiple users accessing the database at the same time.
5. No Cloud Sync:
It doesn't sync with cloud services by default; you need to build that separately.
6. Error Handling is Basic:
Debugging and handling database errors in SQLite is harder compared to more advanced
databases.
2. Compare SQLite and Client server database in android
Point SQLite Client-Server Database
1. What it is A small database inside your A database stored on a remote
app. server.
2. Setup Very easy to set up. Needs a server and internet setup.
3. Where data is Data is stored on the phone. Data is stored on a server online.
stored
4. Network No internet needed. Works Needs internet to access data.
offline.
5. Speed Fast because it’s local. Can be slow because of internet
delays.
6. Size & Growth Best for small apps. Doesn’t Can handle large apps with lots of
grow much. data.
7. Syncing No syncing needed. Data is Needs syncing between phone and
local. server.
8. Security Basic security (local to the More security options (encrypted,
phone). protected).
9. Best for Simple, offline apps (like Big apps with shared data (like
notes). social media).
3. Steps to establish connectivity to an android application
1. Create a SQLite Helper Class
First, you need to create a class that extends SQLiteOpenHelper. This class helps you
manage the creation and versioning of the database.
2. Override the onCreate() and onUpgrade() Methods
In the onCreate() method, you define the SQL statements to create the necessary tables.
The onUpgrade() method is used to handle schema changes when updating the app.
3. Initialize the Helper Class in Your Activity
Create an instance of your helper class in an activity or a ViewModel to access the
database.
4. Open the Database
Use getWritableDatabase() to get a database object that allows writing data, or
getReadableDatabase() if you only need to read data.
5. Perform CRUD Operations
Use methods such as insert(), query(), update(), and delete() to add, retrieve, modify, or
remove data from the database.
6. Close the Database Connection
Always close the database using db.close() when you are done to free up resources and
avoid memory leaks.
Example : SQLiteDatabase db = myDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "John");
db.insert(“users, null, values);