Professional Documents
Culture Documents
Android Programming
PRN
2020017001761336
T.Y.B.C.A.
(SEM VI)
Student Name
Suraj Dayashankar Pal
Academic Year
(2022-23)
College
L.N. College of Management & Technology
University
Yashwantrao Chavan Maharashtra Open
University
1
Yashwantrao Chavan Maharashtra Open University
DnyaGangotri, Govardhan, Near Gangapur Dam, Nashik - 422222
B.C.A P131
Session: May 2023
Class: - T.Y.B.C.A. (Semester – VI)
Subject: - Android Programming
Name: - Suraj Dayashankar Pal
P.R.N No: - 2020017001761336
Academic Year: - 2022-23 (May)
2
LN COLLEGE OF MANAGEMENT AND TECHNOLOGY
CLASS: - T.Y.B.C.A.
ACADEMIC YEAR: - 2022-23
P.R.N No: -2020017001761336
This certificate serves as confirmation that Suraj Dayashankar Pal has
successfully completed the Android Programming Semester VI - Practical
as part of his T.Y.B.C.A. coursework for the academic year 2022-23. The
practical work has been reviewed and evaluated by the subject teacher and external
examiner, meeting all academic standards and requirements.
3
INDEX
Practical No. Practical Name Page. No
5. Create Android application using Spinner & Auto complete test view 13
7. Create Android application using Rating Bar, Web view, Seek Bar 18
PRACTICAL NO. 1
4
AIM: Create an application with following functionalities: Print and show
a simple message e.g., Hello Word
Solution)
Step 1: Open Android Studio and create a new project by selecting "Empty Activity" template.
Step 2: Choose a name and location for your project and click "Finish".
Step 3: In the MainActivity.java file, add the following code in the onCreate() method:
MainActivity.java
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<TextView
android:id="@+id/hello_world_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</androidx.constraintlayout.widget.ConstraintLayout>
5
Output:
PRACTICAL NO. 2
AIM: Android button & Toggle button from this create application
SOLUTION)
MainActivity.java
package com.example.button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
6
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
toggleButton = findViewById(R.id.toggleButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Button clicked!",
Toast.LENGTH_SHORT).show();
}
});
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Toggle on!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Toggle off!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:layout_centerInParent="true"/>
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle me!"
android:layout_below="@id/button"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
7
Output:
<Button
android:id="@+id/btn_show_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Toast" />
8
</androidx.constraintlayout.widget.ConstraintLayout>
3. Create a new layout file named "custom_toast.xml" in the "res/layout" folder. This file
will contain the layout for the custom toast.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#ff5722"
android:orientation="horizontal">
<ImageView
android:id="@+id/toast_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_info"
android:layout_marginEnd="16dp"/>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="This is a custom toast message"/>
</LinearLayout>
4. In MainActivity.java, add the following code to show the custom toast when the button
is clicked:
package com.example.toast;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PRACTICAL NO. 4
AIM: Create Android application using CheckBox & AlertDiaglogBox
SOLUTION)
1. Create a new Android project in Android Studio and give it a suitable name.
2. In the activity_main.xml file, add a CheckBox and a Button. Set the id of the CheckBox
to chkBox and the id of the Button to btnShowDialog.
10
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/chkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check me" />
<Button
android:id="@+id/btnShowDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show dialog" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. In the MainActivity.java file, add the following code to initialize the CheckBox and
Button and set an onClickListener for the Button:
package com.example.box;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCheckBox = findViewById(R.id.chkBox);
mButton = findViewById(R.id.btnShowDialog);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialog();
}
});
}
11
builder.setTitle("Dialog Title");
builder.setMessage("This is the message of the dialog.");
builder.create().show();
}
}
4. In Create a new layout file called dialog_checkbox.xml and add a CheckBox to it. Set
the id of the CheckBox to dialog_chkBox.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/dialog_chkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check this box" />
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT:
12
Show Dialog
Button CheckBox AlertDialogBox
PRACTICAL NO. 5
AIM: Create Android application using Spinner & Auto complete test
view
SOLUTION)
1. Create a new Android Studio project with an empty activity.
2. Open the activity_main.xml file and add the following code:
<?xml version="1.0" encoding="UTF-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
13
android:entries="@array/cities"
android:prompt="@string/select_city"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3. Open the MainActivity.java file and add the following code:
package com.example.spinnerauto;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.spinnerauto.R;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
14
<string name="select_city">Select a city</string>
<string name="search_hint">Type to search</string>
<string-array name="cities">
<item>Mumbai</item>
<item>New Delhi</item>
<item>Pune</item>
<item>Bangalore</item>
<item>Kolkata</item>
<item>Agra</item>
<item>Jaipur</item>
<item>Surat</item>
<item>Chandigarh</item>
<item>Indore</item>
</string-array>
</resources>
OUTPUT:
Using Spinner
Using Autocomplete
PRACTICAL NO. 6
AIM: Create an application with following functionalities: Calculator for
Basic mathematical operations.
15
SOLUTION)
MainActivity.java
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
editTextNumber1 = findViewById(R.id.editTextNumber1);
editTextNumber2 = findViewById(R.id.editTextNumber2);
buttonAdd = findViewById(R.id.buttonAdd);
buttonSubtract = findViewById(R.id.buttonSubtract);
buttonMultiply = findViewById(R.id.buttonMultiply);
buttonDivide = findViewById(R.id.buttonDivide);
textViewResult = findViewById(R.id.textViewResult);
@Override
public void onClick(View v) {
// Get values from EditText fields
float num1 = Float.parseFloat(editTextNumber1.getText().toString());
float num2 = Float.parseFloat(editTextNumber2.getText().toString());
switch (v.getId()) {
case R.id.buttonAdd:
textViewResult.setText(String.valueOf(num1 + num2));
break;
case R.id.buttonSubtract:
textViewResult.setText(String.valueOf(num1 - num2));
break;
case R.id.buttonMultiply:
textViewResult.setText(String.valueOf(num1 * num2));
break;
case R.id.buttonDivide:
if (num2 == 0) {
textViewResult.setText("Cannot divide by zero");
} else {
textViewResult.setText(String.valueOf(num1 / num2));
}
break;
}
}
}
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"
16
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 1"
android:inputType="numberDecimal"
android:padding="8dp"
android:textSize="18sp" />
<EditText
android:id="@+id/editTextNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 2"
android:inputType="numberDecimal"
android:padding="8dp"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add"
android:textSize="18sp" />
<Button
android:id="@+id/buttonSubtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Subtract"
android:textSize="18sp" />
<Button
android:id="@+id/buttonMultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Multiply"
android:textSize="18sp" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Divide"
android:textSize="18sp" />
</LinearLayout>
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Result will be displayed here"
android:textSize="18sp" />
</LinearLayout>
17
Output:
PRACTICAL NO. 7
AIM: Create Android application using Rating Bar, Web view, Seek Bar
SOLUTION)
MaiActivity.java
package com.example.bar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RatingBar;
import android.widget.SeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
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">
OUTPUT:
19
PRACTICAL NO. 8
AIM: Design an android application for “Registration Form” using
different layout such as table layout, linear layout etc. Use Drawable
Resources, option menu, List Views and Adapters.
SOLUTION)
MainActivity.java
package com.example.registration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
20
editTextEmail = findViewById(R.id.editTextEmail);
editTextPassword = findViewById(R.id.editTextPassword);
buttonRegister = findViewById(R.id.buttonRegister);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
// Code for opening settings activity goes here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.java
<?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">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Registration Form"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPersonName" />
</LinearLayout>
21
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textEmailAddress" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword" />
</LinearLayout>
<Button
android:id="@+id/buttonRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Register" />
</LinearLayout>
22
OUTPUT:
23
PRACTICAL NO. 9
AIM: Create Android application using Date picker & Time Picker
SOLUTION)
MainActivity.java
package com.example.datetime;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import com.example.datetime.R;
import java.text.DateFormat;
import java.util.Calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
24
});
buttonPickTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the current time
final Calendar calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date and Time"
android:textSize="24sp"
android:textStyle="bold"/>
<Button
android:id="@+id/button_pick_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Date"/>
<Button
android:id="@+id/button_pick_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time"/>
<TextView
android:id="@+id/text_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Selected date: "/>
<TextView
android:id="@+id/text_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Selected time: "/>
</LinearLayout>
25
Output:
26
PRACTICAL NO. 10
AIM: Create Android application using Progress Bar, Implicit Intend
SOLUTION)
MainActivity.java
package com.example.intend;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
downloadButton = findViewById(R.id.downloadButton);
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.google.co.in/url?sa=t&source=web&rct=j&url=https://www.africau.edu/
images/default/sample.pdf&ved=2ahUKEwjL5cXWhM_-AhX4RmwGHZ3OAo0QFnoECCkQAQ&usg=AOvVaw0B2jC3SIfJyqSsyBlwEi27";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE);
request.setTitle("Downloading File");
request.setDescription("Downloading file...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file-to-
download.pdf");
27
DownloadReceiver.java
package com.example.intend;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager downloadManager = (DownloadManager)
context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = downloadManager.getUriForDownloadedFile(downloadId);
String downloadedFilePath = downloadUri.getPath();
activity_main.xml
<?xml version=”1.0” encoding=”utf-8”?>
<RelativeLayout 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”
tools:context=”.MainActivity”>
<ProgressBar
android:id=”@+id/progressBar”
style=”@style/Widget.AppCompat.ProgressBar.Horizontal”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”32dp”
android:layout_marginLeft=”32dp”
android:layout_marginRight=”32dp”
android:visibility=”invisible”/>
<Button
android:id=”@+id/downloadButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Download File”
android:layout_centerInParent=”true”/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
28
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Intend"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<receiver
android:name=".DownloadReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
</application>
</manifest>
OUTPUT:
29
PRACTICAL NO. 11
AIM: Create Android application using fragments
SOLUTION)
1. Create a new Android project in Android Studio.
2. In the "Create New Project" dialog, select "Empty Activity" and click "Next".
3. Give your project a name, choose a package name and a location to save it in, and click
"Finish".
4. In the project structure on the left side of the screen, right-click on the "app" folder and
select "New > Fragment > Fragment (Blank)".
5. Give your fragment a name, for example "MyFragment", and click "Finish".
6. In the "MyFragment.java" file that was just created, extend the Fragment class and
override the onCreateView() method. This method should return a View that will be
displayed as the content of the fragment.
7. In the "fragment_my.xml" file that was created alongside "MyFragment.java", add
some UI elements that you want to display in the fragment.
8. In your MainActivity.java file, you can now use a FragmentManager to add the
fragment to the activity.
9. In the "activity_main.xml" layout file, add a container where the fragment will be
displayed.
myFragment.java
package com.example.fragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Use the {@link myFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class myFragment extends Fragment {
30
public myFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment myFragment.
*/
// TODO: Rename and change types and number of parameters
public static myFragment newInstance(String param1, String param2) {
myFragment fragment = new myFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my, container, false);
}
}
}
fragment_my.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello from MyFragment!"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
package com.example.fragments;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
31
// Create a new instance of MyFragment
myFragment.MyFragment fragment = new myFragment.MyFragment();
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
32
PRACTICAL NO. 12
AIM: Design and Implement a Registration Form using SQLite Database.
SOLUTION)
Step 1) Update build.gradle file.
Before you can use Material Design in your projects you need to add the following compile line
to your Gradle dependencies block in your build.gradle file and rebuilt the project.
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.core:core:1.7.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
33
<color name="colorTextHint">#51d8c7</color>
<color name="purple_500">#9c27b0</color>
<color name="purple_700">#7b1fa2</color>
<color name="teal_200">#80cbc4</color>
<color name="teal_700">#00796b</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="purple_200">#ce93d8</color>
</resources>
Step 4) Update styles.xml.
Add the below style values to the styles.xml located in res => values => styles.xml.
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColor">@color/colorText</item>
<item name="android:textColorHint">@color/colorText</item>
<item name="colorControlNormal">@color/colorText</item>
<item name="colorControlActivated">@color/colorText</item>
</style>
</resources>
Step 5) Add Logo Image.
Download the below logo image and add it to the drawable folder located in res => drawable.
34
Step 9) Create a LoginActivity class.
Now create a package named activities and create a class named LoginActivity and add below code. Here i have
written the code to validate the input fields Email and Password using the InputValidation class which I
described above. Also, code for navigation to registration screen on the click of registration link and to user list
screen after clicking on the login button if credentials are valid.
Step 10) Create activity_register.xml.
Now create a layout file for the RegisterActivity.java i.e activity_register.xml and add the below code in your
layout file. The code will create a simple registration form containing a logo on the top, 4 input fields name,
email, password, and confirm password, register button, and login screen navigation link.
Step 11) Create RegisterActivity class.
Now create a package named activities and create a class named RegisterActivity and add below code. Here I
have written the code to validate the input fields Name, Email, Password, and Confirm Password using the
InputValidation class which I described above. Also, code for navigation to the login screen on the click of
login link and shows snackbar with success message for registration.
Step 12) Create activity_users_list.xml.
Now create a layout file for the UsersListActivity.java i.e activity_users_list.xml and add the below code in
your layout file. The code will create a simple view containing two views one view i.e LinearLayout shows the
welcome message with email id of the logged in user and another view i.e RecyclerView shows the list of
registered users in the app.
Step 13) Writing the Adapter Class.
Now create a package named adapters and create a class named UsersRecyclerAdapter and add below code.
Here onCreateViewHolder() method inflates item_user_recycler.xml. In onBindViewHolder() method the
appropriate User data (name, email, and password) set to each row.
Step 15) Update AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="activities.LoginActivity"
android:screenOrientation="fullSensor"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="activities.RegisterActivity"
android:screenOrientation="fullSensor" />
<activity
android:name="activities.UsersListActivity"
android:screenOrientation="fullSensor" />
</application>
</manifest>
35
Full Code Files:
OUTPUT
36