Experiment: 01
Aim
:Create activity and debug the app.
Objective:
The primary goal of this experiment is to gain hands-on experience in creating a new Android activity and mastering the art of debugging.
Materials:
1. Android studio installed on your computer.
2. Basic understanding of Java programming.
Procedure:
1. Launch the Android studio application.
2. Click on start a new Android studio project And select appropriate templates such as empty activity.
3. Android studio will generate some code for your main activity for the file MainActivity. Java. Then right click on the ‘app’ folder in the project
explorer and Choose "New" -> "Activity" -> "Empty Activity” and give new activity a name.
File: activity_main.xml Code:
<?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_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="131dp"
android:layout_marginBottom="303dp"
android:text="open activity 2" />
</RelativeLayout>
File: MainActivity.java Code:
package com.example.experiment1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity { private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { openActivity2();
} });
}
public void openActivity2() {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
} }
File: activity_main2.xml Code:
<?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 2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
File: MainActivity2.java Code:
package com.example.experiment1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
} }
Output:
After Click on open activity 2, activity 2 opens.
Experiment: 02
Aim: Create user input sections and screen navigation.
Objective:
The primary objective is to provide hand on experience in creating user input sections and seamlessly navigating between screens in an Android application.
Materials:
1. Android studio installed on your computer.
2. Basic understanding of Java programming.
Procedure:
1. Launch Android studio.
2. Create a new Android project.
3. Modify activity layout on both xml and Java file.
File: activity_main.xml Code:
<?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"/>
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_marginTop="20dp"
android:hint="Enter your mission objective"
android:inputType="text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="131dp"
android:layout_marginBottom="303dp"
android:text="Open Activity 2" />
</RelativeLayout>
File: MainActivity.java Code:
package com.example.experiment1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity { private Button button;
private EditText userInput;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
userInput = findViewById(R.id.userInput);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { openActivity2();
} });
}
public void openActivity2() {
String missionObjective = userInput.getText().toString();
Intent intent = new Intent(this, MainActivity2.class);
intent.putExtra("MISSION_OBJECTIVE: Experiment 02", missionObjective);
startActivity(intent);
} }
File: activity_main2.xml Code:
<?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 2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/missionObjectiveText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_marginTop="20dp"/>
</RelativeLayout>
File: MainActivity2.java Code:
package com.example.experiment1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity { private TextView missionObjectiveText;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
missionObjectiveText = findViewById(R.id.missionObjectiveText);
// Retrieve mission objective from the intent String missionObjective =
getIntent().getStringExtra("MISSION_OBJECTIVE");
// Display mission objective
missionObjectiveText.setText("Mission Objective: " + missionObjective);
} }
Output:
Experiment: 03
Aim: To evaluate the user interface design in Android studio and ensure its functionality.
Objective:
The primary objective is to evaluate the effectiveness of the user interface design in terms of usability, accessibility, and visual appeal. Additionally, the experiment aims to verify the functionality of the app, ensuring that all components behave as intended.
Materials:
1. Android studio installed on your computer.
2. Android device or emulator for testing.
Procedure:
1. Launch the Android studio.
2. Open project.
3. Review user interface design.
4. Check functionality.
5. Test on emulator or device.
6. Evaluate performance.
File: MainActivity.java Code:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity { private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { displayMessage();
} });
}
private void displayMessage() {
textView.setText("Button clicked! Hello from Android!");
} }
File: activity_main.xml Code:
<?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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_below="@id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />
</RelativeLayout>
Output:
Experiment: 04
Aim: To understand and implement background tasks in Android studio.
Objective:
The primary objective is to learn how to perform background task in Android applications to prevent blocking the main UI thread and improve overall app responsiveness.
Materials:
1. Android studio installed on your computer.
2. Basic understanding of Java programming and Android development.
Procedure:
1. Launch Android studio.
2. Create a new project and select template as empty activity.
3. Implement background task.
4. Execute background task.
5. Test and debug.
File: MainActivity.java Code:
package com.example.backgroundtaskexperiment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity { private static final String DOWNLOAD_URL =
"https://drive.google.com/uc?
export=download&id=1zYTb9lyKDUCw7Ti1LbIg5EixaXcyBll5";
private Button downloadButton;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadButton = findViewById(R.id.download_button);
progressBar = findViewById(R.id.progress_bar);
downloadButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// Execute background task to download file new DownloadFileTask().execute(DOWNLOAD_URL);
} });
}
// AsyncTask to download file in the background
private class DownloadFileTask extends AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() { super.onPreExecute();
// Show progress bar
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(String... params) { String fileUrl = params[0];
int fileLength = 0;
int totalBytesRead = 0;
// Publish progress
publishProgress((int) ((totalBytesRead * 100) / fileLength));
}
// Close streams output.flush();
output.close();
input.close();
return true;
} catch (Exception e) { e.printStackTrace();
return false;
} }
@Override
protected void onPostExecute(Boolean result) { super.onPostExecute(result);
// Hide progress bar
progressBar.setVisibility(View.GONE);
if (result) {
// File download successful
Toast.makeText(MainActivity.this, "File downloaded successfully", Toast.LENGTH_SHORT).show();
} else {
// File download failed
Toast.makeText(MainActivity.this, "Failed to download file", Toast.LENGTH_SHORT).show();
} } } }
File: activity_main.xml Code:
<?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/download_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download File"
android:layout_centerInParent="true" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/download_button"
android:layout_marginTop="16dp"
android:visibility="gone" />
</RelativeLayout>
Output:
Experiment: 05
Aim: To understand and implement background tasks in Android studio.
Objective:
The primary objective is to learn how to establish network connections send HTTP Requests and handle responses in an Android application, enabling features such as fetching data from API or accessing web services.
Materials:
1. Android studio installed on your computer.
2. Basic understanding of Java programming and Android development.
Procedure:
1. Launch Android studio.
2. Create a new project and select template as empty activity.
3. Add Internet permission to Android manifest.
4. Implement network connection.
5. Handle responses.
File: MainActivity.java Code:
package com.example.googlesearchexperiment;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity { private EditText searchQueryEditText;
private Button searchButton;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchQueryEditText = findViewById(R.id.search_query_edit_text);
searchButton = findViewById(R.id.search_button);
searchButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// Get the search query from the EditText String searchQuery =
searchQueryEditText.getText().toString().trim();
// Create a search Intent to open Google search in a web browser
Uri searchUri = Uri.parse("https://www.google.com/search?
q=" + searchQuery);
Intent searchIntent = new Intent(Intent.ACTION_VIEW, searchUri);
// Verify that there's an app available to handle the intent before starting it
if (searchIntent.resolveActivity(getPackageManager()) !=
null) {
startActivity(searchIntent);
} else {
// Handle case where no app is available to handle the intent
// For example, display an error message to the user // Toast.makeText(MainActivity.this, "No app available to handle the search", Toast.LENGTH_SHORT).show();
} } });
} }
File: activity_main.xml Code:
<?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">
<EditText
android:id="@+id/search_query_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search query"
android:layout_margin="16dp"
android:imeOptions="actionDone"
android:inputType="text"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_below="@id/search_query_edit_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />
</RelativeLayout>
Output:
Experiment: 06
Aim: To understand and implement background tasks in Android studio.
Objective:
The primary objective is to learn how to establish network connections send HTTP Requests and handle responses in an Android application, enabling features such as fetching data from API or accessing web services.
Materials:
1. Android studio installed on your computer.
2. Basic understanding of Java programming and Android development.
Procedure:
1. Launch Android studio.
2. Create a new project.
3. Implement multiple tasks handling.
4. Display notification popups.
5. Test and debug.
File: MainActivity.java Code:
package com.example.multipletasksdemo;
import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class MainActivity extends AppCompatActivity { private BroadcastReceiver broadcastReceiver;
private static final String CHANNEL_ID = "my_channel";
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
broadcastReceiver = new BroadcastReceiver() { @Override
public void onReceive(Context context, Intent intent) {
showNotification("Broadcast Received", "This is a broadcast notification.");
} };
registerReceiver(broadcastReceiver, new IntentFilter("my_action"));
}
@Override
protected void onDestroy() { super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "My Channel";
String description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
} }
private void showNotification(String title, String message) { NotificationCompat.Builder builder = new
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background) .setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.NOTIFICATION_POLICY_CONTROL) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.NOTIFICATION_POLICY_CONTROL}, 1);
return;
}
notificationManager.notify(1, builder.build());
} }
File: activity_main.xml Code:
<?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:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Notification"
android:layout_centerInParent="true"/>
</RelativeLayout>
Output: