MCAD
MCAD
Unit – I
Basics of Android Development
What is Android?
Android is an open source and Linux-based Operating System for mobile devices
such as smartphones and tablet computers. Android was developed by the Open
Handset Alliance, led by Google, and other companies.
Android offers a unified approach to application development for mobile devices
which means developers need only develop for Android, and their applications
should be able to run on different devices powered by Android.
Android development environment:
Step 1: Install Android Studio
Download Android Studio from the website: https://developer.android.com/studio
Run the downloaded installer and follow the installation wizard instructions.
During the installation, select the components that we want to install, such as the
Android SDK, emulator, and development tools.
Step 2: Launch Android Studio
After the installation is complete, launch Android Studio.
Step 3: Configure SDK and AVD Manager
On the Android Studio Welcome screen, click on "Configure" in the bottom-right
corner and select "SDK Manager."
The SDK Manager allows us to install SDK components, platform versions, and
system images for different device configurations.
Step 4: Install SDK Platforms and System Images
In the SDK Platforms tab, install the Android versions (API levels) that we want to
target in our app. For example, Android 11 (API 30).
In the SDK Tools tab, install the "Android Emulator" and "Intel x86 Emulator
Accelerator (HAXM Installer)" if system supports hardware acceleration (Intel-
based CPUs).
Step 5: Create an AVD (Android Virtual Device)
After installing the necessary SDK components, click on "AVD Manager" on the
Android Studio toolbar (the green Android icon with a phone).
In the AVD Manager, click on "Create Virtual Device."
Choose the device type we want to emulate (e.g., Pixel 4) and click "Next."
Select a system image for the virtual device (e.g., Android 11, x86 or x86_64) and
click "Next."
Configure the AVD properties (e.g., device orientation, scale, RAM, and storage)
and click "Finish" to create the AVD.
Step 6: Run the AVD
Back in the AVD Manager, click the green "Play" button next to the AVD we
created to start the virtual device.
The AVD will start up, and the Android system running on the virtual device.
Step 7: Use AVD for Testing
In Android Studio, open Android project.
On the toolbar, select AVD from the device drop-down menu (next to the "Play"
button).
Click the "Run" button (green triangle) to run app on the selected AVD.
• Configure an AVD:
We can easily manage AVDs by creating new ones or editing existing ones through
the AVD Manager in Android Studio. This way, we can test our app on various
virtual devices with different screen sizes, resolutions, and Android versions.
Create and manage virtual devices
An Android Virtual Device (AVD) is a configuration that defines the characteristics
of an Android phone, tablet, Wear OS, Android TV, or Automotive OS device that
we want to simulate in the Android Emulator. The Device Manager is tools you can
launch from Android Studio that helps you create and manage AVDs.
To open the new Device Manager, do one of the following:
• From the Android Studio Welcome screen, select More Actions > Virtual
Device Manager.
• After opening a project, select View > Tool Windows > Device
Manager from the main menu bar, and then click Create device.
About AVDs
An AVD contains a hardware profile, system image, storage area, skin, and other
properties.
Hardware profile
The hardware profile defines the characteristics of a device as shipped from the
factory. The Device Manager comes pre-loaded with certain hardware profiles, such
as Pixel devices, and we can define or customize the hardware profiles as needed.
A device definition labeled with the Google Play logo in the Play Store column
includes both the Google Play Store app and access to Google Play services,
including a Google Play tab in the Extended controls dialog that provides a
convenient button for updating Google Play services on the device.
Devices with this logo and a device type of "Phone" are also CTS compliant and
might use system images that include the Play Store app.
System images
A system image labeled with Google APIs includes access to Google Play services.
The Recommended tab lists recommended system images. The other tabs include a
more complete list. The right pane describes the selected system image.
Storage area
The AVD has a dedicated storage area on development machine. It stores the device
user data, such as installed apps and settings, as well as an emulated SD card. If
needed, we can use the Device Manager to wipe user data so the device has the same
data as if it were new.
Skin
An emulator skin specifies the appearance of a device. The Device Manager
provides some predefined skins.
Create an AVD
To create a new AVD:
1. Open the Device Manager.
2. Click Create Device.
The Select Hardware window appears.
Only some hardware profiles include Play Store. These profiles are
fully CTS compliant and might use system images that include the Play Store app.
3. Select a hardware profile, then click Next.
The System Image window appears.
4. Select the system image for a particular API level, and then click Next.
The Verify Configuration window appears.
3. Click Finish.
New hardware profile appears in the Select Hardware window. You can create an
AVD that uses the hardware profile by clicking Next or click Cancel to return to
the Virtual tab or target device menu.
To create a hardware profile using a copy as a starting point:
1. In the Select Hardware window, select a hardware profile and click Clone
Device or right-click a hardware profile and select Clone.
2. In the Configure Hardware Profile window, change the hardware profile
properties as needed.
3. Click Finish.
New hardware profile appears in the Select Hardware window. Create an AVD that
uses the hardware profile by clicking Next or click Cancel to return to
the Virtual tab or target device menu.
Edit existing AVDs
You can perform the following operations on an AVD from the Device
Manager's Virtual tab:
• To edit an AVD, click Edit this AVD and make your changes.
• To delete an AVD, click Menu and select Delete.
• To show the associated AVD INI and IMG files on disk, click Menu and
select Show on Disk.
• To view AVD configuration details that you can include in bug reports to the
Android Studio team, click Menu and select View Details.
Edit existing hardware profiles
We can't edit or delete the preloaded hardware profiles, but can perform the
following operations on other hardware profiles from the Select Hardware window:
• To edit a hardware profile, select it and click Edit Device. We can also right-
click a hardware profile and select Edit. Next, make your changes.
• To delete a hardware profile, right-click it and select Delete.
Run and stop an emulator and clear data
From the Virtual tab, we can perform the following operations on an emulator:
Activities: An activity represents a single screen with a user interface. It is the most
fundamental component of an Android app and is responsible for interacting with
the user. An application may consist of one or multiple activities. Each activity
extends the Activity class and typically corresponds to a specific user interaction.
Fragments: Fragments are modular sections of an activity that can be combined and
reused across multiple activities. They are useful for creating responsive user
interfaces, especially on devices with varying screen sizes. Fragments extend the
Fragment class and are managed by activities.
Content Providers: A content provider allows the app to share data with other apps
securely. It acts as an interface to access and manage the app's data, which can be
stored in a database, file, or any other data source. Content providers enable inter-
app data sharing, allowing other apps to read or modify the data based on defined
permissions.
Layouts: Android uses XML-based layout files to define the user interface of an
activity or fragment. These layout files specify how different UI elements are
arranged and displayed on the screen.
Resources: Android apps use resources such as strings, colors, styles, and images.
These resources are kept separately from the code, allowing for easy localization and
management.
Gradle Build System: Gradle is used to manage the build process of an Android
app. It defines dependencies, compiles code, and packages the app for distribution.
Activity is a window that contains the user interface of your application. As there
are various states of activity like Running, Paused, Stopped and Killed.
Activity base class contains events that govern the life cycle of an activity.
onCreate(): Called when the activity is first created
onStart(): Called when the activity becomes visible to the user
onResume(): Called when the activity starts interacting with the user
onPause(): Called when the current activity is being paused and the previous
activity is being resumed
onStop(): Called when the activity is no longer visible to the user
onDestroy(): Called before the activity is destroyed by the system
onRestart(): Called when the activity has been stopped and is restarting again
Unit – II
Modeling GUI Using Android
Android Layout Types
There are number of Layouts provided by Android which you will use in almost all
the Android applications to provide different view, look and feel.
WebView is a browser that is used to display the web pages in our activity
layout.
• Android ListView:
Layout Attributes
Each layout has a set of attributes which define the visual properties of that layout.
There are few common attributes among all the layouts and there are other attributes
which are specific to that layout. Following are common attributes and will be
applied to all the layouts:
Attribute Description
android:id This is the ID which uniquely identifies the view.
android:layout_width This is the width of the layout.
android:layout_height This is the height of the layout
This is the extra space on the top side of the
android:layout_marginTop
layout.
This is the extra space on the bottom side of the
android:layout_marginBottom
layout.
This is the extra space on the left side of the
android:layout_marginLeft
layout.
This is the extra space on the right side of the
android:layout_marginRight
layout.
android:layout_gravity This specifies how child Views are positioned.
For example, a simple vertical linear layout with a text view and a button looks like
this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a Button" />
</LinearLayout>
Android UI Controls
We have a different type of UI controls available in android to implement the user
interface for our android applications.
Android ProgressBar:
In android, ProgressBar is a user interface control which is used to indicate the
progress of an operation. For example, downloading a file, uploading a file.
<ProgressBar
android:id="@+id/pBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:minWidth="200dp"
android:max="100"
android:indeterminate="true"
android:progress="1" />
Attribute Description
ImageView:
ImageView class is used to display any kind of image resource in the android
application.
Attribute Description
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Unit– III
Event Handling in Android Application
Android - Event Handling:
Events are a useful way to collect data about a user's interaction with interactive
components of Applications.
Like button presses or screen touch etc. The Android framework maintains an
event queue as first-in, first-out (FIFO) basis. We can capture these events in our
program and take appropriate action as per requirements.
There are following three concepts related to Android Event Management −
• Event Listeners − An event listener is an interface in the View class that
contains a single callback method. These methods will be called by the
Android framework when the View to which the listener has been registered
is triggered by user interaction with the item in the UI.
• Event Listeners Registration − Event Registration is the process by which
an Event Handler gets registered with an Event Listener so that the handler
is called when the Event Listener fires the event.
• Event Handlers − When an event happens and we have registered an event
listener for the event, the event listener calls the Event Handlers, which is
the method that actually handles the event.
Event Listeners & Event Handlers:
OnClickListener()
This is called when the user either clicks or touches or
onClick()
focuses upon any widget like button, text, image etc. You
will use onClick() event handler to handle such event.
OnLongClickListener()
This is called when the user either clicks or touches or
onLongClick()
focuses upon any widget like button, text, image etc. for
one or more seconds. You will use onLongClick() event
OnFocusChangeListener()
This is called when the widget looses its focus ie. user
onFocusChange()
goes away from the view item. You will use
onFocusChange() event handler to handle such event.
OnFocusChangeListener()
This is called when the user is focused on the item and
onKey()
presses or releases a hardware key on the device. You will
use onKey() event handler to handle such event.
OnTouchListener()
This is called when the user presses the key, releases the
onTouch()
key, or any movement gesture on the screen. You will use
onTouch() event handler to handle such event.
OnMenuItemClickListener()
This is called when the user selects a menu item. You will
onMenuItemClick()
use onMenuItemClick() event handler to handle such
event.
onCreateContextMenuItemListener()
onCreateContextMenu() This is called when the context menu is being built(as the
result of a sustained "long click)
Following are the simple steps to show how we will make use of separate Listener
class to register and capture click event. Similar way you can implement your
listener for any other required event type.
Step Description
5 Run the application to launch Android emulator and verify the result
of the changes done in the aplication.
Intents could be Implicit, for instance, calling intended actions, and explicit as
well, such as opening another activity after some operations like onClick or
anything else. Below are some applications of Intents:
1. Sending the User to Another App
2. Getting a Result from an Activity
3. Allowing Other Apps to Start Your Activity
Some Important Method of Intent and their Description
Methods Description
Methods Description
Implicit Intent
Implicit Intent doesn’t specify the component. In such a case, intent provides
information on available components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
Syntax:
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.geeksforgeeks.org/"));
startActivity(intent);
For Example: a webpage is going to be opened. As you type the name of your
desired webpage and click on the ‘CLICK’ button. Your webpage is opened.
Explicit Intent
Explicit Intent specifies the component. In such a case, intent provides the external
class to be invoked.
Syntax:
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
For Example: In the given example, there are two activities (FirstActivity, and
SecondActivity). When you click on the ‘GO TO OTHER ACTIVITY’ Button in
the FirstActivity, then you move to the SecondActivity. When you click on the
‘GO TO HOME ACTIVITY’ button in the SecondActivity, then you move to the
first activity. This is getting done through Explicit Intent.
Attribute Description
Example: activity_main.xml
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
➢ Go to MainActivity.java.
➢ Now in the java file create a string array and store the values you want to
display in the list.
Example: MainActivity.java
public class MainActivity extends Activity
{
ListView l;
String tutorials[]={ "Algorithms", "Data Structures",
"Languages", "Interview Corner"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
l = findViewById(R.id.list);
ArrayAdapter<String> arr;
arr = new ArrayAdapter<String>(this,
R.layout.support_simple_spinner_dropdown_item,
tutorials);
l.setAdapter(arr);
}
}
Spinner in Android
➢ It provides an easy way to select one item from the list of items and it shows
a dropdown list of all values when we click on it.
➢ The default value of the android spinner will be the currently selected value
and by using Adapter we can easily bind the items to the spinner objects.
Attribute Description
Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following
code. Here we will create a Spinner inside Relative Layout.
activity_main.xml:
<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
Step 3: Now open app-> java -> package -> MainActivity.java and add the
following code. Here we will use ArrayAdapter to fill the data in Spinner. Also we
are using Toast to display when the item in Spinner is selected.
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener
{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_it
em);
Types of Permissions
➢ Install-Time Permissions: If the Android 5.1.1 (API 22) or lower, the
permission is requested at the installation time at the Google Play Store.
➢ If the user Accepts the permissions, the app is installed. Else the
app installation is canceled.
➢ If the user accepts the permissions, then that feature of the app can be used.
Else to use the feature, the app requests permission again.
<uses-permission android:name=”android.permission.PERMISSION_NAME”/>
activity_main.xml:
<!--Declaring the required permissions-->
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
android:layout_marginTop="16dp"
android:padding="8dp"
android:layout_below="@id/storage"
android:layout_centerHorizontal="true"/>
Check for permissions: Beginning with Android 6.0 (API level 23), the user
has the right to revoke permissions from any app at any time, even if the app
targets a lower API level. So to use the service, the app needs to check for
permissions every time.
Syntax:
Syntax:
ActivityCompat.requestPermissions (MainActivity.this,
permissionArray,
requestCode);
Java File:
// Function to check and request permission
public void checkPermission(String permission, int requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity.this,
permission) == PackageManager.PERMISSION_DENIED)
{
ActivityCompat.requestPermissions(MainActivity.this, new String[] {
permission }, requestCode);
}
else
{
Toast.makeText(MainActivity.this, "Permission already
granted", Toast.LENGTH_SHORT).show();
}
}
➢ ToggleButton allows users to change settings between two states from your
phone’s Settings menu such as turning your WiFi, Bluetooth, etc. on / off.
activity_main.xml:
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onToggleClick" />
Java File:
public class MainActivity extends Activity
{
ToggleButton togglebutton;
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState)
{
togglebutton = (ToggleButton)findViewById(R.id.toggleButton);
textview = (TextView)findViewById(R.id.textView);
}
Unit– IV
Dialog, Menu and Database with RecyclerView
And CardView
❖ Menu in Android
➢ There are 3 types of menus in Android:
• Option Menu
• Context Menu
• Pop-up Menu
❖ Option Menu
➢ The options menu is the primary collection of menu items for an activity.
➢ It's where you should place actions that have a overall impact on the app,
such as Search, Compose Email and Settings.
❖ Context Menu
➢ A context menu is a floating menu that appears when the user performs a
long-click on an element.
❖ PopUp Menu
➢ It's good for providing an overflow of actions that relate to specific content
or to provide options for a second part of a command.
➢ Instead of building a menu in your activity's code, you should define a menu
and all its items in an XML menu resource.
➢ You can then inflate the menu resource i.e load the XML files as a Menu
object in your activity.
Menu_example.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/mail"
android:icon="@drawable/ic_mail"
android:title="@string/mail" />
<item android:id="@+id/upload"
android:icon="@drawable/ic_upload"
android:title="@string/upload"
android:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
Attributes Description
➢ Now you have to set the positive (yes) or negative (no) button using the
object of the AlertDialogBuilder class.
➢ Dialog display a small window i.e a popup which draws the user attention
over the activity before they continue moving forward.
➢ The dialog appears over the current window and display the content defined
in it.
➢ The Alert Dialog and Custom Alert Dialog both prompt a small window to
make decision.
Example:
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
Button dialogButton = (Button)
dialog.findViewById(R.id.dialogButtonOK);
Toast.makeText(getApplicationContext(),"Dismissed..!!",Toas
t.LENGTH_SHORT).show();
}
});
dialog.show();
}
});
➢ In order to access this database, you don't need to establish any kind of
connections for it like JDBC,ODBC etc.
❖ Database - Package
➢ The main package is android.database.sqlite that contains the classes to
manage your own databases
❖ Database - Creation
➢ In order to create a database you just need to call this method
openOrCreateDatabase with your database name and mode as a parameter.
❖ Database - Insertion
➢ We can create table or insert data into table using execSQL method defined
in SQLiteDatabase class.
❖ Database - Fetching
➢ We can retrieve anything from database using an object of the Cursor class.
➢ We will call a method of this class called rawQuery and it will return a
resultset with the cursor pointing to the table.
➢ With the help of CardView, we can add radius and elevation to our items of
RecyclerView. CardView gives a rich look and feels to our list of data.
• Card Layout.
• Modal Class.
• ViewHolder class.
Unit– V
Introduction to Android Development using Kotlin
❖ What is Kotlin?
➢ Kotlin is a statically typed, object-oriented programming language that is
interoperable with the Java virtual machine (JVM), Java Class Libraries and
Android.
• Data science: Kotlin is often used for data science tasks, such as building
data pipelines and putting machine learning models into production.
activity_main.xml
Create an activity_main.xml file in layout folder and add the following code.
<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" />
</android.support.constraint.ConstraintLayout>
MainActivity.kt
Create MainActivity.kt file in example.app.javatpoint.helloworld package add the
following code.
package example.app.javatpoint.helloworld
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
Output:
myToast.show() ;