Android Assignment
Android Assignment
Dalvik plays a crucial role in Android development as the original process virtual machine (VM) used by
Android to run applications.
1. Security: Permissions protect user data and device features from unauthorized access. For example,
only apps with the camera permission can access the device camera.
2. User Privacy: Permissions give users control over what data and features an app can access,
ensuring their privacy is respected.
3. App Functionality: Proper permissions allow an app to function as intended. Without necessary
permissions (like accessing storage or internet), certain features may break.
4. Compliance: Many regulations (like GDPR or CCPA) require apps to request explicit user consent
before accessing sensitive data.
5. Trust and Transparency: Clearly requesting permissions helps build trust with users. If users
understand why permissions are needed, they're more likely to grant them.
6. Platform Guidelines: App stores (like Google Play and Apple App Store) have strict policies around
permissions. Violating them can lead to app rejection or removal.
An intent filter in Android development is used to declare the capabilities of a component (like an activity,
service, or broadcast receiver). It tells the system what types of intents a component can handle.
2. Enable App Linking: Intent filters let your app respond to specific URLs, file types, or custom actions,
enabling features like deep linking.
3. Implicit Intents: When an app sends an implicit intent (without specifying a component), Android uses
intent filters to find suitable apps/components to handle the request.
4. Control App Behavior: It helps define how and when different parts of your app should be started
based on user actions or system events (like boot completion, network changes, etc.).
The AndroidManifest.xml file is a critical configuration file in every Android app. It provides essential
information about your app to the Android operating system.
Declare App Components: It lists all components like activities, services, broadcast receivers, and content
providers used in the app.
<activity android:name=".MainActivity" />
1.
Permissions: It declares what permissions the app needs (like internet access, camera, storage, etc.).
<uses-permission android:name="android.permission.INTERNET" />
2.
3. App Metadata: It defines app-level details like:
○ App name
○ Icon
○ Theme
○ App version
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
4.
5. Intent Filters: It declares how your app responds to system or user actions (like opening a link,
receiving a broadcast, etc.).
App Entry Point: It defines the main activity (the launcher screen) with an intent filter:
<intent-filter>
</intent-filter>
6.
7. Hardware and Software Features: It declares what features the app requires, like GPS, Bluetooth, or
camera.
In short:
AndroidManifest.xml is the blueprint of an Android app. Without it, the app won’t run or be recognized
correctly by the Android system.
5. What is the proper way of setting up an Android-powered device for app development?
Setting up an Android-powered device for app development involves a few important steps to ensure the
device can communicate with your development environment (usually Android Studio). Here's the proper way
to set it up:
● Go to About phone.
● Tap Build number 7 times until you see a message like “You are now a developer!”.
● Go to Settings > System > Developer options (or just Developer Options).
● On the device, you might see a prompt asking to allow USB debugging. Tap Allow.
● If you're on Windows, you may need to install the OEM USB drivers for your device (downloadable
from the manufacturer’s website).
Type:
adb devices
●
● You should see your device listed. If it says unauthorized, check your device for a prompt and accept
it.
● Click Run (Play icon) and select your connected device from the list.
● To retain background tasks or data during configuration changes (like screen rotations).
● To manage lifecycle-aware background work.
@Override
Bundle savedInstanceState) {
return null; // No UI
1.
getSupportFragmentManager()
.beginTransaction()
.add(fragment, "headless")
.commit();
2.
Note: Since there's no UI, you add it using a tag, not a layout container ID.
1. Activities
● Represent a single screen with a user interface.
2. Services
3. Broadcast Receivers
4. Content Providers
● Acts as a mediator between ViewModel and data sources (like Room or network).
Android supports several programming languages for application development. The main ones are:
1. Kotlin (Recommended)
2. Java
3. C++
● Not native to Android SDK, but very popular for multiplatform apps.
● C# (using Xamarin).
9. Discuss Control Properties in Layout Files. Compare Views and View groups in Layout Files?
In Android, layout files are written in XML and define the UI structure. Each UI element (like a Button or
TextView) is called a View. These views have properties/attributes that control their behavior and
appearance.
Property Description
Definition Basic UI building block (e.g., Container that holds other Views or
TextView, Button). ViewGroups.
An Android application is made up of several components and files that define its functionality and UI. Here's a
breakdown:
1. Core Components:
File/Folder Description
assets/ Raw files like text or fonts bundled with the app.
build.gradle Build configuration scripts.
Android manages the lifecycle of an application to optimize performance and memory. Here’s how it works,
particularly for Activities:
Method Description
onCreate( Called when the activity is first created. Initialize UI and data here.
)
onRestart Called after the activity is stopped and before it starts again.
()
● Bound Service: Binds with bindService() → onBind() → runs as long as it’s bound.
● Methods like onCreate() and onTerminate() can be overridden to manage global state or
resources.
11. Discuss about building a UI in XML with Code. Examine Text Control and Button Control in Android.
Discuss about Date and Time Controls in Android.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="18sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
TextView textView;
Button button;
@Override
super.onCreate(savedInstanceState);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
Example:
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Android!"
android:textSize="16sp"
android:textColor="#000000" />
3. Button Control
● Used for user interaction (tapping, submitting forms, etc.).
Example:
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
textView.setText(date);
datePickerDialog.show();
TimePicker
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />
textView.setText(time);
timePickerDialog.show();
● An ArrayAdapter is a bridge between a data source (like an array or list) and a UI component
such as a ListView or Spinner.
● It converts data items into View items that can be displayed in the UI.
Common Usage:
this,
android.R.layout.simple_list_item_1,
items
);
listView.setAdapter(adapter);
Parameters:
2. Styles in Android
What is a Style?
● A style is a collection of attributes that specify the look of a View (e.g., text size, color, padding).
<style name="CustomTextStyle">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">18sp</item>
</style>
<TextView
style="@style/CustomTextStyle"
android:text="Styled Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
3. Themes in Android
What is a Theme?
● It controls UI appearance globally (e.g., default text color, background, button styles, etc.).
Applying a Theme (AndroidManifest.xml):
<application
android:theme="@style/Theme.MyCustomTheme">
<item name="colorPrimary">#6200EE</item>
<item name="colorPrimaryVariant">#3700B3</item>
<item name="colorOnPrimary">#FFFFFF</item>
</style>
13. List all the SQLite Packages and Classes. Discuss about creating an SQLite Database. Discuss about
Content Providers in Android.
Main Package:
android.database.sqlite
Important Classes:
Class Description
Steps:
@Override
String query = "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)";
@Override
onCreate(db);
// Insert data
values.put("name", "John");
values.put("email", "john@example.com");
A Content Provider allows data sharing between different apps in a secure and controlled way. It abstracts
the underlying data source (SQLite, files, etc.) and exposes it through a URI.
Key Components:
Component Purpose
ContentProvi Abstract class that handles CRUD methods (insert, query, update,
der delete).
Typical Usage:
@Override
// Initialize database
return true;
@Override
@Override
// Insert data
@Override
@Override
Declare in Manifest:
<provider
android:name=".MyProvider"
android:authorities="com.example.myapp.provider"
android:exported="true" />
14. Discuss about the Android Security Model. How do you perform Runtime Security Checks in. Android?
Explain shared prefrences in Android?
● User data
● System resources
● App integrity
Each app runs in its own sandbox with a unique user ID (UID) assigned by the system. This isolation
prevents apps from accessing each other’s data unless explicitly permitted.
Key Components:
Feature Description
App Sandbox Each app runs in its own process with separate memory and file
storage.
Digital Signatures All apps must be signed. Used to verify the developer's identity.
Permissions like accessing camera, storage, or location are considered dangerous, and must be requested at
runtime.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
} else {
openCamera();
@Override
if (requestCode == 101) {
openCamera();
} else {
Toast.makeText(this, "Camera Permission Denied", Toast.LENGTH_SHORT).show();
3. SharedPreferences in Android
What is SharedPreferences?
SharedPreferences is used to store small amounts of key-value data (like user settings or session info)
persistently.
1. Save Data:
editor.putString("username", "JohnDoe");
editor.putBoolean("isLoggedIn", true);
editor.apply(); // or commit()
2. Retrieve Data:
editor.remove("username");
editor.clear(); // clears all
editor.apply();
Use Cases:
15. What is Gallery Control? Explain with an example program. Explain Menus and Action bars with example
program?
The Gallery widget was used to display images in a horizontal scrolling list. However, it has been
deprecated since API 16. A better alternative today is RecyclerView or HorizontalScrollView with
ImageView.
activity_main.xml
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spacing="10dp"
android:layout_marginTop="20dp" />
MainActivity.java
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery.setAdapter(new ImageAdapter(this));
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
Types of Menus:
res/menu/menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_settings"
android:title="Settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
MainActivity.java
@Override
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
return super.onOptionsItemSelected(item);
16. Classify the different ways if designing the User Interface in Android. Explain about various. Layouts in
Android.
Example:
<Button
android:id="@+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"/>
button.setText("Click Me");
layout.addView(button);
a. LinearLayout
Example:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Hello"/>
</LinearLayout>
Example:
<RelativeLayout ...>
<TextView android:id="@+id/tv1" android:text="Label" />
</RelativeLayout>
c. ConstraintLayout
Example:
<androidx.constraintlayout.widget.ConstraintLayout ...>
<Button
android:id="@+id/button"
android:text="Press"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
d. FrameLayout
● Designed to hold one view (or stack multiple views on top of each other).
Example:
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
e. TableLayout
Example:
<TableLayout>
<TableRow>
<TextView android:text="Name"/>
<EditText />
</TableRow>
</TableLayout>
f. GridLayout
● Divides layout into rows and columns like TableLayout but more flexible.
g. ScrollView
Example:
<ScrollView>
<LinearLayout>
</LinearLayout>
</ScrollView>