Unit 2 Android AD
Unit 2 Android AD
In Android development, the User Interface (UI) refers to the visual elements and interactions
that allow users to interact with the app. It includes buttons, text fields, images, navigation
components, and more, all designed to provide a smooth and intuitive user experience.
There are several types of layouts in Android that help structure the UI elements. These layouts
define how the views (like buttons, text fields, etc.) are positioned on the screen. Here are five
common types of layouts in Android:
1. LinearLayout
Description: A LinearLayout arranges its child views either vertically or horizontally in a
single row or column.
Usage: It's commonly used when you want to display UI elements in a simple, linear
order.
Example:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="Button 1" />
<Button android:text="Button 2" />
</LinearLayout>
2. RelativeLayout
Description: A RelativeLayout allows you to position views relative to each other. Views
can be aligned to the top, bottom, left, right, center, etc., of other views or the parent
layout.
Usage: It’s ideal for creating flexible and complex layouts where elements depend on
the position of other elements.
Example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="Button 1"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button2"
android:text="Button 2"
android:layout_below="@id/button1" />
</RelativeLayout>
3. ConstraintLayout
Description: A ConstraintLayout provides a flexible and efficient way to create complex
layouts. You can position views relative to each other with constraints (e.g., align top,
bottom, left, right, etc.).
Usage: It's often used for responsive UIs, and it is recommended for new Android UI
designs because of its efficiency and flexibility.
Example:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="Button 1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button2"
android:text="Button 2"
app:layout_constraintTop_toBottomOf="@id/button1"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4. FrameLayout
Description: A FrameLayout is used to display a single child view at a time. It is
commonly used for fragments or to display one view that occupies the entire space of
the layout.
Usage: Ideal when you only need to show one item at a time, such as in cases with full-
screen images or videos.
Example:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="Button 1" />
</FrameLayout>
5. GridLayout
Description: A GridLayout allows you to place child views in a grid-like structure, with
rows and columns. It gives you flexibility to define where each element will appear in the
grid.
Usage: It's useful when you need a UI element to be arranged in rows and columns, like
a calculator or image gallery.
Example:
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2">
<Button android:text="Button 1" />
<Button android:text="Button 2" />
<Button android:text="Button 3" />
<Button android:text="Button 4" />
</GridLayout>
<application>
</application>
</manifest>
2. uses-sdk
It is used to define a minimum and maximum SDK version by means of an API Level integer
that must be available on a device so that our application functions properly, and the target SDK
for which it has been designed using a combination of minSdkVersion, maxSdkVersion, and
targetSdkVersion attributes, respectively. It is contained within the <manifest> element.
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="27" />
3. uses-permission
It outlines a system permission that must be granted by the user for the app to function properly
and is contained within the <manifest> element. When an application is installed (on Android 5.1
and lower devices or Android 6.0 and higher), the user must grant the application permissions.
<uses-permission
android:name="android.permission.CAMERA"
android:maxSdkVersion="18" />
4. application
A manifest can contain only one application node. It uses attributes to specify the metadata for
your application (including its title, icon, and theme). During development, we should include a
debuggable attribute set to true to enable debugging, then be sure to disable it for your release
builds. The application node also acts as a container for the Activity, Service, Content Provider,
and Broadcast Receiver nodes that specify the application components. The name of our
custom application class can be specified using the android:name attribute.
<application
android:name=".GeeksForGeeks"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/gfgIcon"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.Light"
android:debuggable="true"
tools:targetApi="31">
<!-- application nodes -->
</application>
5. uses-library
It defines a shared library against which the application must be linked. This element instructs
the system to add the library’s code to the package’s class loader. It is contained within the
<application> element.
<uses-library
android:name="android.test.runner"
android:required="true" />
6. activity
The Activity sub-element of an application refers to an activity that needs to be specified in the
AndroidManifest.xml file. It has various characteristics, like label, name, theme, launchMode,
and others. In the manifest file, all elements must be represented by <activity>. Any activity that
is not declared there won’t run and won’t be visible to the system. It is contained within the
<application> element.
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
7. intent-filter
It is the sub-element of activity that specifies the type of intent to which the activity, service, or
broadcast receiver can send a response. It allows the component to receive intents of a certain
type while filtering out those that are not useful for the component. The intent filter must contain
at least one <action> element.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<uses-configuration
android:reqTouchScreen=”finger”
android:reqNavigation=”trackball”
android:reqHardKeyboard=”true”
android:reqKeyboardType=”twelvekey”/>
11. uses-features
It is used to specify which hardware features your application requires. This will prevent our
application from being installed on a device that does not include a required piece of hardware
such as NFC hardware, as follows:
<uses-feature android:name=”android.hardware.nfc” />
12. permission
It is used to create permissions to restrict access to shared application components. We can
also use the existing platform permissions for this purpose or define your own permissions in
the manifest.
<permission
android: name=”com.paad.DETONATE_DEVICE”
android:protectionLevel=“dangerous”
android:label=”Self Destruct”
android:description=”@string/detonate_description”>
</permission>
3) define style and theme and explain any two things given code snippet
Style and Theme in Android
In Android development, styles and themes are used to define the visual appearance of the
app's UI components. Both allow developers to customize the look and feel of the application in
a more organized and reusable manner.
Style:
A style in Android is a collection of properties that define the appearance of a single view (such
as a Button, TextView, etc.). It allows you to define various visual attributes such as colors,
fonts, padding, and size that can be applied to individual elements in your app.
Example:
<style name="CustomTextStyle">
<item name="android:textColor">@android:color/black</item>
<item name="android:textSize">18sp</item>
<item name="android:fontFamily">sans-serif</item>
</style>
In this example, the CustomTextStyle style sets the text color to black, text size to 18sp, and
font family to sans-serif. You can apply this style to any TextView or similar UI element.
Theme:
A theme is a broader version of a style. It is a collection of styles that can be applied globally to
an entire activity or application. Themes affect the overall appearance of the UI, including
background color, text color, buttons, and other components. They help maintain a consistent
look across the entire app.
Example:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">@color/background</item>
<item name="android:textColorPrimary">@android:color/black</item>
</style>
In this example, AppTheme defines the overall theme for the app. It is based on the
Theme.AppCompat.Light.DarkActionBar and customizes the window background and primary
text color for the entire app or activity.
5) what is view class and explain recycler view and list view along with diagram
RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and
ListView. It is an improvement on both of them and can be found in the latest v-7 support
packages. It has been created to make possible construction of any lists with XML layouts as an
item which can be customized vastly while improving on the efficiency of ListViews and
GridViews. This improvement is achieved by recycling the views which are out of the visibility of
the user.
For example, if a user scrolled down to a position where items 4 and 5 are visible; items 1, 2,
and 3 would be cleared from the memory to reduce memory consumption.
Let us check, Android RecyclerView with a simple example.
Example: An Android Application showing the Exam Schedule
To implement a basic RecyclerView three sub-parts are needed to be constructed which offer
the users the degree of control they require in making varying designs of their choice.
1. The Card Layout: The card layout is an XML layout which will be treated as an item for
the list created by the RecyclerView.
2. The ViewHolder: The ViewHolder is a java class that stores the reference to the card
layout views that have to be dynamically modified during the execution of the program
by a list of data obtained either by online databases or added in some other way.
3. The Data Class: The Data class is a custom java class that acts as a structure for
holding the information for every item of the RecyclerView.
Step by Step to Create RecyclerView
Step 1: Create new project
Click on File, then New => New Project.
Choose “Empty Activity” for the project template.
Select language as Kotlin.
Select the minimum SDK(According to the application needs).
Step 2: Creating Layout for the Application
Layouts are important part of the Android Applications. This Application will need two main
layouts as mentioned below:
A ListView in Android is a type of AdapterView that displays a vertically scrollable list of items,
with each item positioned one below the other. Using an adapter, items are inserted into the list
from an array or database efficiently. For displaying the items in the list method setAdaptor() is
used. The setAdaptor() method binds the adapter with the ListView.
ListView in Android is a ViewGroup that is used to display the list of items in multiple rows and
contains an adapter that automatically inserts the items into the list dynamically. The main
purpose of the adapter is to retrieve data from an array or database and dynamically insert each
item into the list for the desired result.
Some Important XML Attributes of ListView
Attribute Description
android:headerDividersEnabled When set to false, the ListView will not draw the
Attribute Description
AutoTextCompleteView
AutoTextCompleteView is a widget commonly used in mobile development (particularly in
Android) to provide real-time suggestions for users as they type into a text input field. It
enhances the user experience by helping users quickly find the information or options they need
without having to type everything out.
Key Features of AutoTextCompleteView:
1. Real-time Suggestions: As the user types, the view dynamically suggests possible
completions based on the entered text.
2. Searchable Dataset: The suggestions are typically pulled from a predefined set of data,
such as a list of names, cities, or terms. The data is filtered based on the user's input.
3. User-Friendly Interface: The widget typically shows a dropdown or list of suggestions
that the user can scroll through and select from. It can make data entry much faster.
4. Use Cases:
o Search Bars: Offering suggestions while typing search queries.
o Address Input: Autocomplete addresses as the user types.
o Email or Username Fields: Autocompleting email addresses or usernames
based on previous inputs.
Example of AutoTextCompleteView in Android:
AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, data);
autoCompleteTextView.setAdapter(adapter);
In this example:
AutoCompleteTextView is a widget for user input.
ArrayAdapter is used to supply the suggestions (like a list of strings).
The adapter connects the input field with the dataset (a list of strings, such as possible
locations, emails, etc.).
8) what is storage in Android explain types of storage in real module give suitable exam
Storage in Android
In Android, storage refers to the system's ability to persistently store data, whether it is user
data, application data, or files. Android provides several options for storing data on a device,
each serving different use cases.
There are two primary types of storage in Android:
1. Internal Storage: Private to the app and not accessible by other apps.
2. External Storage: Typically accessible by other apps and users (like SD cards or
external drives), though with restricted permissions on modern Android versions.
Types of Storage in Android
1. Internal Storage:
o Definition: Data stored in internal storage is private to the app and cannot be
accessed by other apps or the user without root access. This storage is typically
used for sensitive or private data that should not be shared.
o Location: Files are stored in the app's private directory
(/data/data/com.example.app/files).
o Usage: Ideal for storing application-specific files that shouldn't be modified by the
user or other apps.
o Example: Storing user preferences, small databases, or encrypted data.
Example in Kotlin:
// Writing to internal storage
val filename = "myfile.txt"
val fileContents = "Hello, World!"
openFileOutput(filename, Context.MODE_PRIVATE).use {
it.write(fileContents.toByteArray())
}