Android and Application Practical
Android and Application Practical
Practical No– 01
Set-up of Android development environment, managing AVD and understanding its
various components.
Practical No– 02
Understanding of Various Components available in Android Application.
Android applications are built using various components that interact with each other to provide
the desired functionality and user experience.
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.
Services: A service is a component that runs in the background, independent of any user
interface. It performs long-running operations or handles tasks that should continue to execute
even when app is not in the foreground. Services can be used to play music, download data, or
handle network transactions.
Broadcast Receivers: A broadcast receiver is a component that listens for and responds to
system-wide broadcasts. These broadcasts can be sent by the Android system or other apps.
Broadcast receivers allow your app to respond to events, such as incoming SMS messages,
network connectivity changes, or battery status updates.
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.
Intents: Intents are messaging objects used to communicate between components within an app
or between different apps. They facilitate starting activities, services, or broadcasting events.
Intents can carry data along with them to pass information between components.
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.
Manifest File: The AndroidManifest.xml file contains important information about the app,
such as the app's package name, components, permissions required, and hardware features used.
It acts as a configuration file for the app.
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.
Practical No– 03
Develop a “Hello World” Application in Android and understand the structure of an
Android Application. activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="24sp" />
</LinearLayout>
Practical No– 04
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this,"onCreate method
called",Toast.LENGTH_LONG).show();
}
@Override
protected void onStart()
{
super.onStart();
Toast.makeText(MainActivity.this,"onStart method
called",Toast.LENGTH_LONG).show();
}
@Override
protected void onResume()
{
super.onResume();
Toast.makeText(MainActivity.this,"onResume method
called",Toast.LENGTH_LONG).show();
}
@Override
protected void onRestart()
{
super.onRestart();
Toast.makeText(MainActivity.this,"onRestart method
called",Toast.LENGTH_LONG).show();
}
@Override
protected void onStop()
{
super.onStop();
Toast.makeText(MainActivity.this,"onStop method
called",Toast.LENGTH_LONG).show();
}
@Override
protected void onPause()
{
super.onPause();
Toast.makeText(MainActivity.this,"onPause method
called",Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy()
{
super.onDestroy();
Toast.makeText(MainActivity.this,"onDestroy method
called",Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); return true;
}
}
Practical No– 05
LinearLayout:
activity_main.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a LinearLayout"
android:textSize="20sp" /> <!-- Add
more views as needed -->
</LinearLayout>
RelativeLayout:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a RelativeLayout"
android:textSize="20sp" />
</RelativeLayout>
GridView:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" android:verticalSpacing="8dp"
android:horizontalSpacing="8dp" android:padding="8dp"
android:gravity="center" />
FrameLayout:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"
android:contentDescription="Image in FrameLayout" />
</FrameLayout>
ConstraintLayout:
activity_main.xml
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a ConstraintLayout"
android:textSize="20sp"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Practical No– 06
AIM:- Design various Activities using different Layouts and available Widgets
(TextView, EditText, Button, RadioButton, CheckBox, ImageButton, ToggleButton,
TimePicker, DatePicker, ProgressBar, ImageView) to make the user-friendly GUI.
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Enter
your name" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
<CheckBox
android:id="@+id/chkSound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable Sound" />
<RadioButton
android:id="@+id/radioOption1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/chkSound"
android:text="Option 1" />
<RadioButton
android:id="@+id/radioOption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioOption1"
android:text="Option 2" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioOption2"
android:textOff="Off"
android:textOn="On" />
</RelativeLayout>
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp" />
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/timePicker"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
<ImageView android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"
android:contentDescription="Sample Image1"
android:visibility="gone" />
</FrameLayout>
Practical No– 07
et1=(EditText)findViewById(R.id.editText1);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) { // TODO Auto-generated
method stub
String s=et1.getText().toString();
Toast t=Toast.makeText(getApplicationContext(), s,
Toast.LENGTH_LONG);
t.show();
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); return true;
}
Practical No– 08
Activity_main.xml
<?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:padding="16dp">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Me" />
<RadioButton
android:id="@+id/radioOption1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/radioOption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
</LinearLayout>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox = findViewById(R.id.checkBox);
radioOption1 = findViewById(R.id.radioOption1);
radioOption2 = findViewById(R.id.radioOption2);
checkBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
showToast("CheckBox is checked!");
} else {
showToast("CheckBox is unchecked!");
}
}
});
Practical No– 09
AIM:- Develop code to navigate between different activities and pass the data from one
activity to other activity using Intent.
Mainactivity.java import
android.os.Bundle; import
android.app.Activity; import
android.content.Intent;
//import android.view.Menu;
import android.view.View;
import android.widget.Button;
@Override
public void onClick(View arg0) { // TODO Auto-generated
method stub
Intent i= new Intent(getApplicationContext(),Second.class);
startActivity(i);
}
});
}
}
Second.java mport
android.os.Bundle; import
android.app.Activity;
import android.view.Menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu); return true;
}
Practical No– 10
AIM:- Develop an android application to store data locally using Shared Preferences and
access-modify in different activities.
<Button
android:id="@+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Settings" />
</LinearLayout>
activity_settings.xml:
<?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:padding="16dp" android:orientation="vertical">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Enter your
name" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Enter
your email" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
Implement the logic for storing and accessing data using Shared Preferences in the
MainActivity and SettingsActivity:
MainActivity.java:
@Override
SettingsActivity.java: import
android.content.SharedPreferences;
import android.os.Bundle; import
android.view.View; import
android.widget.Button; import
android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
etName = findViewById(R.id.etName);
etEmail = findViewById(R.id.etEmail);
btnSave = findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Save the data to Shared Preferences
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME,
MODE_PRIVATE).edit();
editor.putString("name", etName.getText().toString());
editor.putString("email", etEmail.getText().toString()); editor.apply();
finish(); // Close the activity after saving
}
});
}
}
xml Copy
code
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-
filter>
</activity>
<activity android:name=".SettingsActivity" />
Practical No– 11
Develop the code to implement the ListView and the Spinner views, perform add, update,
remove items operations and implement the item selection event handling over ListView
and Spinner for appropriate example.
Program -1
To implement a ListView with add, update, and remove item operations along with item
selection event handling in Android.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:onClick="addItem" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Item"
android:onClick="updateItem" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove Item"
android:onClick="removeItem" />
</RelativeLayout>
MainActivity.java
import android.os.Bundle; import
android.view.View; import
android.widget.AdapterView; import
android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
items = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
items.remove(selectedPosition);
adapter.notifyDataSetChanged();
} else {
Toast.makeText(this, "Select an item to remove", Toast.LENGTH_SHORT).show();
}
}
}
Program -2
To implement a SpinnerView with add, update, and remove item operations along with item
selection event handling in Android
In the layout XML file (activity_spinner.xml), add a Spinner, EditText, and three Buttons
(Add, Update, and Remove) to allow user interaction.
Java Code
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
// Initialize views
spinner = findViewById(R.id.spinner);
editText = findViewById(R.id.edit_text);
addButton = findViewById(R.id.add_button);
updateButton = findViewById(R.id.update_button);
removeButton =
findViewById(R.id.remove_button);
// Initialize data
items = new ArrayList<>();
items.add("Item 1");
items.add("Item 2");
items.add("Item 3");
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
;
spinner.setAdapter(spinnerAdapter);
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Practical No– 12
Develop the code to manage Permission using Manifest file and run time from Activity,
and toggle state of WiFi and Bluetooth.
Program – 1
Manifest.xml code
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<application
</manifest>
Java code
import android.Manifest;
import android.content.pm.PackageManager; import
android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import
androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_READ_EXTERNAL_STORAGE);
}
}
Program – 2
To implement the toggle state of WiFi and Bluetooth in an Android application, you can
use the WifiManager and BluetoothAdapter classes provided by the Android
framework.
Add necessary permissions to the AndroidManifest.xml file: Ensure that you have the
following permissions in your AndroidManifest.xml to control WiFi and Bluetooth:
AndroidManifest.xml
Java code
import android.bluetooth.BluetoothAdapter;
import android.net.wifi.WifiManager; import
android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
wifiManager = (WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
wifiSwitch = findViewById(R.id.wifiSwitch);
bluetoothSwitch = findViewById(R.id.bluetoothSwitch);
wifiSwitch.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
toggleWiFi(isChecked);
}
});
bluetoothSwitch.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
toggleBluetooth(isChecked);
}
});
}
activity_main.xml
<Switch
android:id="@+id/wifiSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiFi"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Switch
android:id="@+id/bluetoothSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:layout_below="@id/wifiSwitch"
android:layout_alignParentStart="true" />
</RelativeLayout>
Practical No – 13
Develop android applications to demonstrate user interaction with the application using
Options Menu, Context Menu and Popup Menu.
Program – 1
To demonstrate user interaction with an Android application using the Options Menu
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
options_menu.xml
<!-- options_menu.xml -->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_option_1"
android:title="Option 1"
android:orderInCategory="100"
android:showAsAction="never" />
<item
android:id="@+id/menu_option_2"
android:title="Option 2"
android:orderInCategory="101"
android:showAsAction="never" />
<item
android:id="@+id/menu_option_3"
android:title="Option 3"
android:orderInCategory="102"
android:showAsAction="never" />
</menu>
Program – 2
To demonstrate user interaction with an Android application using the context Menu.
activity_main.xml
<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
android:padding="16dp" tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
context_menu.xml
<!-- context_menu.xml -->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_edit"
android:title="Edit" />
<item
android:id="@+id/action_delete"
android:title="Delete" />
</menu>
MainActivity.java
import android.os.Bundle; import
android.view.ContextMenu; import
android.view.MenuItem; import
android.view.View; import
android.widget.AdapterView; import
android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo)
{ super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
// Handle context menu item selection
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)
item.getMenuInfo();
switch (item.getItemId()) {
case R.id.action_edit: //
Perform the edit action
Toast.makeText(this, "Edit: " + selectedItem, Toast.LENGTH_SHORT).show();
return true; case R.id.action_delete: // Perform the delete action
adapter.remove(selectedItem); adapter.notifyDataSetChanged();
Toast.makeText(this, "Deleted: " + selectedItem, Toast.LENGTH_SHORT).show();
return true; default:
return super.onContextItemSelected(item);
}
}
}
Program – 3
To demonstrate user interaction with an Android application using the Popup Menu.
activity_main.xml
<Button
android:id="@+id/btnShowPopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup Menu"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/tvSelectedOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnShowPopup"
android:layout_centerHorizontal="true"
android:paddingTop="16dp" />
</RelativeLayout>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowPopup = findViewById(R.id.btnShowPopup);
tvSelectedOption = findViewById(R.id.tvSelectedOption);
btnShowPopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create and show the Popup Menu
showPopupMenu();
}
});
}
popup_menu.xml
Practical – 14
Program – 1
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnShowDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog" />
</LinearLayout>
MainActivity.java
import android.content.DialogInterface;
import android.os.Bundle; import
android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Confirmation AlertDialog
new AlertDialog.Builder(this)
.setTitle("Confirmation AlertDialog")
.setMessage("Do you want to proceed?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Code to handle positive button click
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Code to handle negative button click
}
})
.show();
// List AlertDialog
Program – 2
custom_dialog_layout.xml
<TextView
android:id="@+id/dialogTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Custom Dialog"
android:textSize="18sp"
android:textColor="@android:color/black"
android:gravity="center"/>
<Button
android:id="@+id/dialogButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close"
android:textColor="@android:color/white"
android:background="@drawable/button_background"/>
</LinearLayout>
Create a new Java or Kotlin class that extends the Dialog class to create a custom
dialog.
Java Code:
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
@Override
dialogTitle.setText(title);
dialogButton.setOnClickListener(v -> {
if (onDialogButtonClickListener != null) {
onDialogButtonClickListener.onDialogButtonClick();
}
dismiss();
});
}
Use the Custom Dialog in your activity: In your activity, use the custom dialog class
to show the dialog when needed. You can trigger the custom dialog to appear when a
button is clicked
Java Code:
@Override
findViewById(R.id.showCustomDialogButton).setOnClickListener(v ->
showCustomDialog());
}
In this example, when the button with the ID showCustomDialogButton is clicked, the
custom dialog will be displayed with the provided title.
The OnDialogButtonClickListener interface allows you to handle button click actions
inside the custom dialog.
Remember to replace R.layout.activity_main with your activity's layout file, and make
sure to have the custom dialog layout XML available in the res/layout directory.
Practical No – 15
Develop Android Application for local database connectivity and performing basic
database operations (select, insert, update, delete) using SQLiteDatabase and
SQLiteOpenHelper Classes.
1. In the XML layout file (e.g., activity_main.xml), create the necessary UI elements like
EditText and Buttons to perform database operations.
2. Create a new Java class called DatabaseHelper. This class will extend
SQLiteOpenHelper and handle creating and managing the database.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_QUERY);
}
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.editTextName);
insertButton = findViewById(R.id.buttonInsert); selectButton
= findViewById(R.id.buttonSelect); updateButton =
findViewById(R.id.buttonUpdate); deleteButton =
findViewById(R.id.buttonDelete);
insertButton.setOnClickListener(this);
selectButton.setOnClickListener(this);
updateButton.setOnClickListener(this);
deleteButton.setOnClickListener(this); }
@Override
public void onClick(View v) {
switch (v.getId()) { case
R.id.buttonInsert:
insertData();
break; case
R.id.buttonSelect:
selectData();
break; case
R.id.buttonUpdate:
updateData();
break; case
R.id.buttonDelete:
deleteData();
break;
}
}
if (!name.isEmpty()) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
if (rowId != -1) {
Toast.makeText(this, "Data inserted successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Failed to insert data", Toast.LENGTH_SHORT).show();
}
db.close();
}}
priva
te
void
selec
tData
() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
cursor.close();
db.close();
Practical No – 17
Note: target a minimum SDK version that supports RecyclerView and CardView (at least API
level 21).
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
2. Create a model class to represent the items that will be displayed in the list.
3. Create an adapter class that extends RecyclerView.Adapter to handle the item list and
bind the data to the CardView layout.
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout,
parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
Item item = itemList.get(position); holder.titleTextView.setText(item.getTitle());
holder.descriptionTextView.setText(item.getDescription());
// Bind more data to the CardView as needed
}
4. Create the layout file for your RecyclerView activity (e.g., activity_main.xml) with a
RecyclerView.
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
5. Create the layout file for the CardView item (e.g., item_layout.xml) that will display the
information for each item in the list. Customize this layout as per your requirements.
android:layout_margin="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"/>
<!-- Add more views for other information -->
</LinearLayout>
</androidx.cardview.widget.CardView>
6. In the MainActivity.java, initialize the RecyclerView, create a list of items, and set up the
RecyclerView with the custom adapter:
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Item> itemList = createItemList(); // Replace this with your own logic to create
the list of items
ItemAdapter itemAdapter = new ItemAdapter(itemList);
recyclerView.setAdapter(itemAdapter);
}
Practical No – 18
Main.kt
fun main()
{
val applicationName = "My Simple Kotlin App"
println("Hello $applicationName") }
Practical No – 19
Develop an android application using Kotlin having a Button “Click” and upon clicking
on that Button a Toast message “Button Clicked” should be displayed on screen through
Toast Message
activity_main.xml
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click" />
</LinearLayout>
MainActivity.kt
Practical No – 20
Publishing an Android application on the Google Play Store involves several steps. Here's a
step-by-step guide to help you through the process:
• Once you've completed all the necessary steps and uploaded your APK or App
Bundle, submit your app for review.
• Google Play will review your app to ensure it meets their policies and guidelines.
• This review process may take a few hours to several days, depending on various
factors.