Mad Ex 1to10
Mad Ex 1to10
Mad Ex 1to10
TITLE: Write an Android application to perform text to speech and generate apk file.
EXCERCISE:
1) Write an Android application to perform text to speech and generate apk file.
Code :
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" android:paddingLeft="16dp"
android:paddingTop="16dp" android:paddingRight="16dp" android:paddingBottom="16dp"
tools:context=".MainActivity">
<EditText android:id="@+id/inputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text to speak"
android:layout_marginBottom="16dp"
android:inputType="textMultiLine"
android:maxLines="3"
android:layout_above="@id/speakButton" />
<Button android:id="@+id/speakButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
→. MainActivity.java :
package com.example.practical10;
speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textToRead = inputText.getText().toString(); if
(!textToRead.isEmpty()) {
textToSpeech.speak(textToRead, TextToSpeech.QUEUE_FLUSH,
null, null);
}
}
});
}
@Override
public void onInit(int status) { if (status ==
TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
@Override
protected void onDestroy() { if
(textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}
→. OUTPUT :
QUIZ:
Answer the Followings:
1. What is an apk file.
→. An APK file (Android Package Kit file format) is the file format for applications used
on the Android operating system (OS). An APK file contains all the data an app needs,
including all of the software program's code, assets and resources.
2. Write steps to publishing android applications.
→. Steps to publishing applications :
1. Prepare
2. Keystore
3. Release Build
4. Sign
5. Optimize
6. Release APK
7. Developer Account
8. Store Listing
9. Upload APK
10. Pricing
11. In-App Purchases
12. Publish
Experiment No: 9
EXCERCISE:
1) Write an android application to perform API calling with Retrofit.
→. Answer:
import android.os.Bundle; import
android.util.Log; import
android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call; import retrofit2.Callback; import
retrofit2.Response; import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; public
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
textViewResult = findViewById(R.id.text_view_result);
// Retrofit setup
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/") // Base URL of the API
.addConverterFactory(GsonConverterFactory.create()) // Gson converter
factory for JSON parsing
.build();
Post post =
response.body(); String
content = ""; if (post != null) { content += "ID: "
+ post.getId() + "\n"; content += "User ID: " +
post.getUserId() + "\n"; content += "Title: " +
post.getTitle() + "\n";
content += "Text: " + post.getText() + "\n\n";
}
textViewResult.setText(content);
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
}
→. Output :
QUIZ:
Answer the Followings:
EXCERCISE:
1) Write an android application to perform CRUD operation on Firebase.
→. Answer:
import android.os.Bundle; import
android.text.TextUtils; import
android.view.View; import
android.widget.Button; import
android.widget.EditText; import
android.widget.Toast;
editTextName = findViewById(R.id.editTextName);
editTextAge = findViewById(R.id.editTextAge);
buttonAdd = findViewById(R.id.buttonAdd);
buttonUpdate = findViewById(R.id.buttonUpdate);
buttonDelete = findViewById(R.id.buttonDelete);
buttonRetrieve = findViewById(R.id.buttonRetrieve);
databaseReference =
FirebaseDatabase.getInstance().getReference().child("Users");
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addUser();
}
});
buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUser();
}
});
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteUser();
}
});
buttonRetrieve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { retrieveUsers();
}
});
}
if (id != null) {
User user = new User(id, name, age);
databaseReference.child(id).setValue(user);
Toast.makeText(this, "User added successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Error generating user ID",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Please enter name and age",
Toast.LENGTH_SHORT).show();
}
}
if (!TextUtils.isEmpty(id)) { databaseReference.child(id).removeValue();
Toast.makeText(this, "User deleted successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Please enter user ID",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(MainActivity.this, "Error retrieving users",
Toast.LENGTH_SHORT).show();
}
});
}
}
→. Output :
QUIZ:
EXCERCISE:
1) Write an android application to insert Customer Details (cID, cName,cOrderID) in SQLite Database in
Android.
→. CODE :
→. activity_main.xml :
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview"
android:layout_width="409dp" android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginEnd="45dp"
android:layout_marginBottom="43dp"
android:background="@color/black"
android:clickable="true" android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.988"
app:srcCompat="@drawable/baseline_add"/>
</androidx.constraintlayout.widget.ConstraintLayout> →. MainActivity.java :
package com.example.customerdetails;
<EditText android:id="@+id/c_ID_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="148dp" android:ems="10"
android:hint="Customer id" android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.315"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText android:id="@+id/c_ORDERID_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:ems="10"
android:hint="Customer Orderid"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.315"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c_NAME_input"/>
<Button android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="40dp" android:text="ADD"
android:textAllCaps="false" android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c_ORDERID_input"/>
</androidx.constraintlayout.widget.ConstraintLayout>
→. AddActivity.java package
com.example.customerdetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_add);
→. MyDatabaseHelper.java :
package com.example.customerdetails;
private static final String TABLE_NAME ="Customer"; private static final String
COLUMN_ID ="c_ID"; private static final String COLUMN_NAME ="c_NAME";
private static final String COLUMN_ORDERID ="c_ORDERID";
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,
"+
COLUMN_NAME + " TEXT, " +
COLUMN_ORDERID + " INTEDER);"; db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
void addCustomer(String c_NAME,Integer c_ORDERID){
SQLiteDatabase db = this.getWritableDatabase(); ContentValues cv
= new ContentValues(); cv.put(COLUMN_NAME, c_NAME);
cv.put(COLUMN_ORDERID, c_ORDERID); long result =
db.insert(TABLE_NAME,null,cv); if(result == -1){
Toast.makeText(context,"Failed",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context,"Added
Successfully",Toast.LENGTH_SHORT).show();
}
}
}
→ OUTPUT :
QUIZ:
→. Differences: NoSQL allows flexible data models, dynamic schemas, and horizontal
scalability, while traditional RDBMS relies on fixed schemas, vertical scaling, and strong
ACID properties.
Experiment No: 6
TITLE: Write an application to record video and audio on topic ‚Intent and play the audio and
video.
EXCERCISE:
1) Write an application to record video and audio on topic ‚Intent and play the audio and video.
→. CODE :
→. avtivity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="163dp"
android:layout_marginTop="200dp"
android:onClick="btnRecordPressed"
android:text="Record"
android:layout_centerVertical="true" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="62dp" />
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:onClick="btnStopPressed"
android:text="Stop"
android:layout_centerVertical="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.566"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:onClick="btnplayPressed" android:text="play"
android:layout_centerVertical="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout> →.
MainActivity.java :
package com.example.voicerecorder;
if(isMicrophonePresent()){ getMicrophonePermission();
}
}
Toast.makeText(this,"RECORDING is
STRATING",Toast.LENGTH_LONG).show();
}
catch (Exception e){
e.printStackTrace();
}
}
public void btnStopPressed(View v) { mediaRecorder.stop();
mediaRecorder.release(); mediaRecorder = null;
Toast.makeText(this,"RECORDING is STOPPED.",Toast.LENGTH_LONG).show();
}
public void btnPlayPressed(View v) throws IOException { try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getRecordingfilepath());
mediaPlayer.prepare(); mediaPlayer.start();
Toast.makeText(this,"RECORDING is
PLAYING.",Toast.LENGTH_LONG).show();
}
catch (Exception e){
e.printStackTrace();
}
if(this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROP
HONE)){
return true;
}
else { return false;
}
}
}
}
→. OUTPUT :
QUIZ:
EXCERCISE:
1) Write an application to mark the daily route of travel in map.
→ CODE : 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">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Show Map"
android:id="@+id/showMap"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> →.
androidx.appcompat.app.AppCompatActivity;
showMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MapActivity.class); startActivity(intent);
}
});
}
}
→. Activity_map.xml :
tools:context=".MapActivity" tools:ignore="MissingClass">
</fragment> →.
mapActivity.java :
package com.example.routeoftravel;
GoogleMap gMap;
FrameLayout map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map); map =
findViewById(R.id.map);
@Override
public void onMapReady(@NonNull GoogleMap googleMap) { this.gMap = googleMap;
}
}
→. Output :
QUIZ:
→. This is for a few reasons: API keys can't authenticate the individual user making the
request, only the project or application sending the request. API keys are like passwords —
only effective if the owner stores them securely.
Experiment No: 4
TITLE: Write an Android application that creates a simple Dialog box with only 1 button.
EXCERCISE:
1) Write an Android application that creates a simple Dialog box with only 1 button.
Code :
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">
<Button android:id="@+id/showDialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog"
android:layout_centerInParent="true"/>
</RelativeLayout> →.
MainActivity.java
package com.example.practical4;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { // Call the
function to show the dialog
showDialog();
}
});
}
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Simple Dialog")
.setMessage("This is a simple dialog with one button.");
builder.create().show();
}
}
→. OUTPUT :
QUIZ:
→ . A dialog is a small window that prompts the user to make a decision or enter additional
information. A dialog doesn't fill the screen and is normally used for modal events that
require users to take an action before they can proceed.
<EditText android:id="@+id/editTextN1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number 1"
android:inputType="numberDecimal" />
<EditText android:id="@+id/editTextN2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextN1"
android:layout_marginTop="16dp" android:hint="Enter
Number 2" android:inputType="numberDecimal" />
<Button android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextN2"
android:layout_marginTop="16dp"
android:text="Add" />
<Button android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnAdd"
android:layout_marginTop="16dp"
android:text="Subtract" />
<Button android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnSub"
android:layout_marginTop="16dp"
android:text="Multiply" />
<Button android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnMul"
android:layout_marginTop="16dp"
android:text="Divide" />
<Button android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnDiv"
android:layout_marginTop="16dp"
android:text="Clear" />
<Button android:id="@+id/btnEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnClear"
android:layout_marginTop="16dp"
android:text="Equal" />
<TextView android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnEqual"
android:layout_marginTop="16dp" android:text="Result: "
/>
</RelativeLayout>
→. MainActivity.java :
package com.example.calculator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
buttonAdd = findViewById(R.id.btnAdd); buttonSub =
findViewById(R.id.btnSub); buttonMul =
findViewById(R.id.btnMul); buttonDiv =
findViewById(R.id.btnDiv); buttonClear =
findViewById(R.id.btnClear); buttonEqual =
findViewById(R.id.btnEqual);
buttonSub.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View
v) {
performOperation("-");
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View
v) {
performOperation("*");
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View
v) {
performOperation("/");
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View
v) { clearFields();
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View
v) {
calculateResult();
}
}); }
private
void
perform
Operati
on(Strin
g
operator
) { try {
num1 = Float.parseFloat(editTextN1.getText().toString()); num2 =
Float.parseFloat(editTextN2.getText().toString());
} catch (NumberFormatException e) {
Toast.makeText(this, "Invalid number format",
Toast.LENGTH_SHORT).show();
return;
}
switch (operator) {
case "+": textView.setText("Result: " + (num1 + num2)); break;
→. Output :
QUIZ:
1) Explain EditText.
→. EditText is a Widget of user interface (UI) used to retrieve and modify text data from a user
in an Android app. EditText is a subclass of TextView that inherit all the property of TextView.
Nowadays, EditText is represented with the PlainText element in UI, which displays an empty
text field while designing the app.
→ Android Toggle Button is used to display on and off state on a button. Switch is another
type of toggle button that's predominantly used since Android 4.0. Android Switch provides a
slider control. Both ToggleButton and Switch are subclasses of CompoundButton class.
Experiment No: 2
TITLE: Write an Android application to make a Button to open a new activity from another
activity.
EXCERCISE:
1) Write an Android application to make a Button to open a new activity from another
activity.
→. Code :
→.activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="Activity_1" android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="307dp"
android:text="open activity_2" />
</RelativeLayout>
→. MainActivity.java :
package com.example.myapplication; import
androidx.appcompat.app.AppCompatActivity;
}
}
→. Activity_main2.xml :
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Activity_2" android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
→. MainActivity2.java :
package com.example.myapplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
→. Output :
QUIZ:
Answer the Followings:
1) What are OnResume() and OnPause() call back methods in Activity life cycle.
→. After a fragment has been created successfully it goes through the following stages: The
onStart() method is called when the fragment is now visible to the user followed by the
onResume() method, which is called when the fragment is interactive. The onPause() method
is called once the fragment is no longer interactive.
2) Explain types of intent.
→. Types of Intents in Android:
1. Explicit Intent:
2. Implicit Intent:
• Invokes a system component or an external application's component without
specifying the exact class name.
• Target component is determined based on the intent's action, data, and category.
Experiment No: 1
Aim: Introduction to Android Programming and installation of Android Studio.
• EXERCISE:
2. Android Components:
- Activities: Represent the UI and user interaction.
- Services: Run in the background to perform tasks.
- Broadcast Receivers: Respond to system-wide broadcast announcements.
- Content Providers: Manage and share application data.
Android Studio is the official IDE for Android development. Follow these steps to install it:
3. Initial Setup:
- Open Android Studio.
- Complete the initial setup, which may include installing necessary components and
configuring the Android Virtual Device (AVD) Manager.
• QUIZ
1. What is SDK?
Ans: SDK stands for Software Development Kit. It is a set of tools, libraries, documentation,
and sample code that developers use to create and build software applications for specific
platforms, frameworks, or programming languages. SDKs provide a standardized way for
developers to interact with and develop applications for a particular software or hardware
platform.
2. Enlist languages in which we can make android app.
Ans: Android app development primarily supports two main programming languages:
Java and Kotlin
3. Differentiate between JVM and DVM.
Ans:
JVM DVM
1) It is Stack based. 1) It is Register based which is designed to
run on low memory.
2) JVM uses java byte code and runs 2) DVM uses its own byte code and runs
“.class” file having JIT (Just In Time). the “.Dex” file. From Android 2.2 SDK
Dalvik has got a Just in Time compiler
3) A single instance of JVM is shared with 3) DVM has been designed so that a
multiple applications device can run multiple instances of the VM
efficiently. Applications are given their own
instance.
4) JVM supports multiple operating 4) DVM supports the Android operating
systems. system only.
5) For JVM many Re-tools are available. 5) For DVM very few Re-tools are
available
6) It has a constant pool for every class. 6) There is a constant pool for every
application.
7) Here the executable is JAR. 7) Here the executable is APK.
Prof. Neil Saxena
Prof. Neil Saxena