Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Using PHP and MySQL in Android To Manage Records

This document provides steps to connect an Android app to a MySQL database hosted on 000webhost using PHP. It describes creating database tables to store schedules and user accounts, as well as PHP scripts to add, retrieve, register, update and validate user data.

Uploaded by

joiannagcaoili
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Using PHP and MySQL in Android To Manage Records

This document provides steps to connect an Android app to a MySQL database hosted on 000webhost using PHP. It describes creating database tables to store schedules and user accounts, as well as PHP scripts to add, retrieve, register, update and validate user data.

Uploaded by

joiannagcaoili
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Using PHP and MySQL in Android to Manage Records

STEPS:

1. Open Internet Browser.


2. Go to URL 000webhost.com.
3. Click Sign-in button.
4. Click Login with Google.
5. Click your personal Gmail account.
6. Click Manage button of your created website. For new website, see the previous PDF about this.
7. Click Tools at the left menu panel of your Dashboard.
8. Click Database Manager.
9. Click … button of your created database. For new database, see the previous PDF about this.
10. Click PhpMyAdmin.

11. Click your database.

12. Clcik Create new table. Name your table as tblSchedules. Type 5 for the number of columns. Click Create.

5
13. Create the following fields.
a. Make id as integer (3)
b. Click Index and select Primary Key. Click OK at the dialog.
c. Check A_I checkbox for Auto Increment.
d. Type course_code varchar(15).
e. Type course_title varchar(255).
f. Type course_schedule varchar(255).
g. Type course_instructor varchar(120).
h. Click Save.

Click tblschedules. You should have this table structure.

14. Click Insert. Add the following sample records. Click Go.
15. Add the other sample records.

16. Create a new table. Name it tblusers.


17. Add the following fields:

18. Add an admin record. Use 5f4dcc3b5aa765d61d8327deb882cf99 for password.


5f4dcc3b5aa765d61d8327deb882cf99 is actually means password.

19. Go back to your website’s Dashboard in 000webhost.com.


20. Click Tools at the left menu panel
21. Click File Manager.
22. Double-click public_html folder.
23. Click New File icon.
24. Name the webpage as connect.db. Click Create.

25. Type the following PHP code.

<?php

$host = "localhost";
//Here, use the details for your own database and user
$username = "id21888060_admin";
$password = "#HKScholars2024";
$database = "id21888060_dbhkscholars";

$pdo = new PDO('mysql:host=' . $host . ';dbname=' . $database, $username, $password);

?>

26. Click Save & Close.


27. Create a New File. Name it addschedule.php. Type the following PHP code. Click Save & Close.

<?php
$course_code = $_POST['course_code'];
$course_title = $_POST['course_title'];
$course_schedule = $_POST['course_schedule'];
$course_instructor = $_POST['course_instructor'];

$response = array();

//Check if all fieds are given


if (empty($course_code) || empty($course_title) || empty($course_schedule) || empty($course_instructor))
{
$response['success'] = "0";
$response['message'] = "Some fields are empty. Please try again!";
echo json_encode($response);
die;
}
$scheduledetails = array(
'course_code' => $course_code,
'course_title' => $course_title,
'course_schedule' => $course_schedule,
'course_instructor' => $course_instructor
);
//Insert the user into the database
if (saveSchedule($scheduledetails)) {
$response['success'] = "1";
$response['message'] = "Schedule added successfully!";
echo json_encode($response);
} else {
$response['success'] = "0";
$response['message'] = "Schedule adding failed. Please try again!";
echo json_encode($response);
}

function saveSchedule($scheduledetails) {
require './connect.php';
$query = "INSERT INTO tblschedules (course_code, course_title, course_schedule, course_instructor)
VALUES "
. "(:course_code, :course_title, :course_schedule, :course_instructor)";
$stmt = $pdo->prepare($query);
return $stmt->execute($scheduledetails);
}

28. Create a New File. Name it getschedules.php. Type the following PHP code. Click Save & Close.

<?php
$response = array();

//Insert the user into the database


$success = getSchedules();

if (!empty($success)) {
$response['success'] = "1";
$response['message'] = "Schedules fetched successfully!";
$response['details'] = $success;
echo json_encode($response);
} else {
$response['success'] = "0";
$response['message'] = "Failed to fetch schedules!";
echo json_encode($response);
}

function getSchedules() {
require './connect.php';
$array = array();
$stmt = $pdo->prepare("SELECT * FROM tblschedules");
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = null;
return $array;
}
29. Create a New File. Name it register.php. Type the following PHP code. Click Save & Close.

<?php
$useremail = $_POST['useremail'];
$username = $_POST['username'];
$password = $_POST['password'];
$password1 = $_POST['password1'];

$response = array();

//Check if all fieds are given


if (empty($useremail) || empty($username) || empty($password)) {
$response['success'] = "0";
$response['message'] = "Some fields are empty. Please try again!";
echo json_encode($response);
die;
}

//Check if password match


if ($password !== $password1) {
$response['success'] = "0";
$response['message'] = "Password mistmatch. Please try again!";
echo json_encode($response);
die();
}

//Check if email is a valid one


if (!filter_var($useremail, FILTER_VALIDATE_EMAIL)) {
$response['success'] = "0";
$response['message'] = "Invalid email. Please try again!";
echo json_encode($response);
die();
}

//Check if email exists


if (checkUserEmail($useremail)) {
$response['success'] = "0";
$response['message'] = "Email already exist. Please try again!";
echo json_encode($response);
die();
}

//Check if user name exists


if (checkUserName($username)) {
$response['success'] = "0";
$response['message'] = "Username already exist. Please try again!";
echo json_encode($response);
die();
}
$userdetails = array(
'useremail' => $useremail,
'username' => $username,
'password' => md5($password),
);

//Insert the user into the database


if (registerUser($userdetails)) {
$response['success'] = "1";
$response['message'] = "User registered successfully!";
echo json_encode($response);
} else {
$response['success'] = "0";
$response['message'] = "User registration failed. Please try again!";
echo json_encode($response);
}

function registerUser($userdetails) {
require './connect.php';
$query = "INSERT INTO tblusers (useremail, username, password) VALUES "
. "(:useremail, :username, :password)";
$stmt = $pdo->prepare($query);
return $stmt->execute($userdetails);
}

function checkUserEmail($value) {
require './connect.php';
$stmt = $pdo->prepare("SELECT * FROM tblusers WHERE useremail = ? ");
$stmt->execute([$value]);
$array = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = null;
return !empty($array);
}

function checkUserName($value) {
require './connect.php';
$stmt = $pdo->prepare("SELECT * FROM tblusers WHERE username = ? ");
$stmt->execute([$value]);
$array = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = null;
return !empty($array);
}
30. Create a New File. Name it updateuserprofile.php. Type the following PHP code. Click Save & Close.

<?php
$useremail = $_POST['useremail'];
$username = $_POST['username'];
$password = $_POST['password'];
$password1 = $_POST['password1'];

$response = array();

//Check if all fieds are given


if (empty($useremail) || empty($username) || empty($password)) {
$response['success'] = "0";
$response['message'] = "Some fields are empty. Please try again!";
echo json_encode($response);
die;
}
//Check if password match
if ($password !== $password1) {
$response['success'] = "0";
$response['message'] = "Password mistmatch. Please try again!";
echo json_encode($response);
die();
}
$userdetails = array(
'useremail' => $useremail,
'username' => $username,
'password' => md5($password),
);
//Update the user into the database
if (updateUserProfile($userdetails)) {
$response['success'] = "1";
$response['message'] = "User profile updated successfully!";
echo json_encode($response);
} else {
$response['success'] = "0";
$response['message'] = "User profile updating failed!";
echo json_encode($response);
}
function updateUserProfile($userdetails) {
require './connect.php';
$query = "UPDATE tblusers SET username=:username, password=:password WHERE useremail=:useremail";
$stmt = $pdo->prepare($query);
return $stmt->execute($userdetails);
}

?>
31. Create a New File. Name it validate.php. Type the following PHP code. Click Save & Close.

<?php
$useremail = $_POST['useremail'];
$password = $_POST['password'];
$response = array();

//Check if all fieds are given


if (empty($useremail) || empty($password)) {
$response['success'] = "0";
$response['message'] = "Some fields are empty. Please try again!";
echo json_encode($response);
die;
}

$userdetails = array(
'useremail' => $useremail,
'password' => md5($password)
);

//Insert the user into the database


$success = loginUser($userdetails);

if (!empty($success)) {
$response['success'] = "1";
$response['message'] = "Login successfully!";
$response['details'] = $success;
echo json_encode($response);
} else {
$response['success'] = "0";
$response['message'] = "Login failed. Please try again!";
echo json_encode($response);
}

function loginUser($userdetails) {
require './connect.php';
$array = array();
$stmt = $pdo->prepare("SELECT * FROM tblusers WHERE useremail = :useremail AND password =
:password");
$stmt->execute($userdetails);
$array = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = null;
return $array;
}
32. Create a New File. Name it viewprofile.php. Type the following PHP code. Click Save & Close.

<?php
$useremail = $_POST['useremail'];
$response = array();

//Check if all fieds are given


if (empty($useremail)) {
$response['success'] = "0";
$response['message'] = "Email field is empty. Please try again!";
echo json_encode($response);
die;
}

$userdetails = array(
'useremail' => $useremail
);

//Search the user from the database


$success = searchUserDetails($userdetails);

if (!empty($success)) {
$response['success'] = "1";
$response['message'] = "User details retrieved successfully!";
$response['details'] = $success;
echo json_encode($response);
} else {
$response['success'] = "0";
$response['message'] = "Retrieving user details failed. Please try again!";
echo json_encode($response);
}

function searchUserDetails($userdetails) {
require './connect.php';
$array = array();
$stmt = $pdo->prepare("SELECT * FROM tblusers WHERE useremail = :useremail");
$stmt->execute($userdetails);
$array = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = null;
return $array;
}
33. After creating the APIs, you should have these PHP files.

34. Open your Android Project.


35. Double-click build.gradle.kts (Module:app). Add the following entry.
implementation("mysql:mysql-connector-java:5.1.49")
implementation("com.android.volley:volley:1.2.1")

36. Click Sync Now.

37. Double-click the java MainActivity. Edit the existing codes. Add nav_profile, nav_show_schedules,
nav_add_schedule.
38. Double-click the java LoginActivity. Delete all existing codes. Type the following Java codes.

package <type your package here>;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity {

EditText tuseremail,tpassword;
Button blogin;
Context ctx;
RequestQueue rQueue;
SharedPreferences sh;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

ctx = this;

tuseremail = (EditText) findViewById(R.id.useremail);


tpassword = (EditText)findViewById(R.id.password);
blogin = (Button) findViewById(R.id.login_btn);
blogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String useremail = String.valueOf(tuseremail.getText());


String password = String.valueOf(tpassword.getText());

if (useremail.isEmpty()) {
tuseremail.setError("Email is required");
tuseremail.requestFocus();
return;
}
if (password.isEmpty()) {
tpassword.setError("Password is required");
tpassword.requestFocus();
return;
}
if (useremail.length() >0 && password.length() > 0) {

Toast.makeText(ctx, "Validating user credentials.",


Toast.LENGTH_LONG).show();

rQueue = Volley.newRequestQueue(getApplicationContext());

StringRequest stringRequest = new StringRequest(Request.Method.POST,


getResources().getString(R.string.url) + "validate.php", new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
try {

JSONObject jsonObject = new JSONObject(response);

if (jsonObject.optString("success").equals("1")) {

JSONObject jsonObject1 =
jsonObject.getJSONObject("details");
Toast.makeText(LoginActivity.this, "Login
Successfully! ", Toast.LENGTH_SHORT).show();

// Fetching the stored data from the SharedPreference


sh = getSharedPreferences("userdetail",
MODE_PRIVATE);

// Creating an Editor object to edit(write to the


file)
SharedPreferences.Editor myEdit = sh.edit();
myEdit.putString("useremail",
jsonObject1.getString("useremail"));
myEdit.putString("username",
jsonObject1.getString("username"));
myEdit.putString("password",
jsonObject1.getString("password"));
myEdit.commit();

startActivity(new Intent(getBaseContext(),
MainActivity.class));
finish();
} else {
Toast.makeText(LoginActivity.this,
jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("useremail", useremail);
params.put("password", password);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-
urlencoded");
return params;
}
};
rQueue.add(stringRequest);
} else {
Toast.makeText(getApplicationContext(), "Please enter a valid email
and password", Toast.LENGTH_SHORT).show();
}
}
});
}
public void register(View v){
Intent i = new Intent(ctx, RegisterActivity.class);
startActivity(i);
}
}

39. Double-click the java RegisterActivity. Delete all existing codes. Type the following Java codes.
package <type your package name here>;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {

EditText tuseremail, tusername, tpassword, tpassword1;


Button bregister;
Context ctx;
RequestQueue rQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

tuseremail = findViewById(R.id.useremail);
tusername = findViewById(R.id.username);
tpassword = findViewById(R.id.password);
tpassword1 = findViewById(R.id.password1);
bregister = findViewById(R.id.register_btn);
bregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String useremail = tuseremail.getText().toString();
final String username = tusername.getText().toString();
final String password = tpassword.getText().toString();
final String password1 = tpassword1.getText().toString();

if (useremail.isEmpty()) {
tuseremail.setError("Email is required");
tuseremail.requestFocus();
return;
}
if (username.isEmpty()) {
tusername.setError("Name is required");
tusername.requestFocus();
return;
}
if (password.isEmpty()) {
tpassword.setError("Password is required");
tpassword.requestFocus();
return;
}
if (password1.isEmpty()) {
tpassword1.setError("Very password is required");
tpassword1.requestFocus();
return;
}

if (!password1.equals(password)) {
tpassword1.setError("Very password is incorrect");
tpassword1.requestFocus();
return;
}
rQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.POST,
getResources().getString(R.string.url) + "register.php", new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.optString("success").equals("1")) {
Toast.makeText(RegisterActivity.this, "Registered
Successfully! Now Login", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getBaseContext(),
LoginActivity.class));
finish();
} else {
Toast.makeText(RegisterActivity.this,
jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RegisterActivity.this, error.toString(),
Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("useremail", useremail);
params.put("username", username);
params.put("password", password);
params.put("password1", password1);
return params;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-
urlencoded");
return params;
}
};
rQueue.add(stringRequest);
}
});
}

public void login(View v){


Intent i = new Intent(ctx, LoginActivity.class);
startActivity(i);
}
}

40. Create the java class ProfileFragment. Delete all existing codes. Type the following Java codes.
package <type your package name here>;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

import androidx.fragment.app.Fragment;
public class ProfileFragment extends Fragment {

TextView tuseremail;
EditText tusername, tpassword, tpassword1;
Button bupdate;
Context ctx;
RequestQueue rQueue;
SharedPreferences sh;

public ProfileFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle


savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);

sh = this.getActivity().getSharedPreferences("userdetail", Context.MODE_PRIVATE);

String useremail = sh.getString("useremail", "");


String username = sh.getString("username", "");
String password = sh.getString("password", "");

tuseremail = view.findViewById(R.id.useremail);
tusername = view.findViewById(R.id.username);
tpassword = view.findViewById(R.id.password);
tpassword1 = view.findViewById(R.id.password1);

tuseremail.setText(useremail);
tusername.setText(username);
tpassword.setText(password);
tpassword1.setText(password);

bupdate = view.findViewById(R.id.update_btn);
bupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateProfile();
}
});

return view;
}
public void updateProfile(){
final String useremail = tuseremail.getText().toString();
final String username = tusername.getText().toString();
final String password = tpassword.getText().toString();
final String password1 = tpassword1.getText().toString();
if (useremail.isEmpty()) {
tuseremail.setError("Email is required");
tuseremail.requestFocus();
return;
}
if (username.isEmpty()) {
tusername.setError("Name is required");
tusername.requestFocus();
return;
}
if (password.isEmpty()) {
tpassword.setError("Password is required");
tpassword.requestFocus();
return;
}
if (password1.isEmpty()) {
tpassword1.setError("Very password is required");
tpassword1.requestFocus();
return;
}

if (!password1.equals(password)) {
tpassword1.setError("Very password is incorrect");
tpassword1.requestFocus();
return;
}
rQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.POST,
getResources().getString(R.string.url) + "updateuserprofile.php", new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.optString("success").equals("1")) {
Toast.makeText(getActivity().getApplicationContext(), "User
profile updated successfully!", Toast.LENGTH_SHORT).show();

// Fetching the stored data from the SharedPreference


sh = getActivity().getSharedPreferences("userdetail",
Context.MODE_PRIVATE);

// Creating an Editor object to edit(write to the file)


SharedPreferences.Editor myEdit = sh.edit();
myEdit.putString("useremail", jsonObject.getString("useremail"));
myEdit.putString("username", jsonObject.getString("username"));
myEdit.putString("password", jsonObject.getString("password"));
myEdit.commit();
} else {
Toast.makeText(getActivity().getApplicationContext(), "User
profile updated successfully!", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ctx, error.toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("useremail", useremail);
params.put("username", username);
params.put("password", password);
params.put("password1", password1);
return params;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
rQueue.add(stringRequest);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
}

@Override
public void onDetach() {
super.onDetach();
}
}

41. Create the java class Schedule. Delete all existing codes. Type the following Java codes.
package <type your package name here>;

public class Schedule {


private String course_code;
private String course_title;
private String course_schedule;
private String course_instructor;

public Schedule(String course_code, String course_title, String course_schedule,


String course_instructor) {
this.course_code = course_code;
this.course_title = course_title;
this.course_schedule = course_schedule;
this.course_instructor = course_instructor;
}

public Schedule() {
}

public void setCourseCode(String course_code) {


this.course_code = course_code;
}

public void setCourseTitle(String course_title) {


this.course_title = course_title;
}

public void setCourseSchedule(String course_schedule) {


this.course_schedule = course_schedule;
}

public void setCourseInstructor(String course_instructor) {


this.course_instructor = course_instructor;
}

public String getCourseCode() {


return course_code;
}
public String getCourseTitle() {
return course_title;
}
public String getCourseSchedule() {
return course_schedule;
}
public String getCourseInstructor() {
return course_instructor;
}

42. Create the java class ScheduleAdapter. Delete all existing codes. Type the following Java codes.
package <type your package here>;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import static androidx.core.content.ContextCompat.startActivity;


import static com.google.android.material.internal.ContextUtils.getActivity;

public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ViewHolder> {

private ArrayList<Schedule> list;


Context ctx;
RequestQueue rQueue;

public ScheduleAdapter(ArrayList<Schedule> list) {


this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = (View)
LayoutInflater.from(parent.getContext()).inflate(R.layout.schedule, parent, false);

return new ViewHolder(v);


}

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


Schedule schedule = list.get(position);

holder.course_code.setText(schedule.getCourseCode());
holder.course_title.setText(schedule.getCourseTitle());
holder.course_schedule.setText(schedule.getCourseSchedule());
holder.course_instructor.setText(schedule.getCourseInstructor());

holder.flowmenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu=new PopupMenu(v.getContext(), holder.flowmenu);
popupMenu.inflate(R.menu.flowmenu);
popupMenu.show();
Toast.makeText(v.getContext(), "Popup Menu: ",
Toast.LENGTH_SHORT).show();
popupMenu.setOnMenuItemClickListener(new
PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.edit_menu) {//edit operation
Bundle bundle = new Bundle();
bundle.putString("course_code", schedule.getCourseCode());
bundle.putString("course_title", schedule.getCourseTitle());
bundle.putString("course_schedule",
schedule.getCourseSchedule());
bundle.putString("course_instructor",
schedule.getCourseInstructor());
Intent intent = new Intent(v.getContext(),
EditScheduleFragment.class);
intent.putExtra("userdata", bundle);
v.getContext().startActivity(intent);
} else if (itemId == R.id.delete_menu) {
rQueue = Volley.newRequestQueue(v.getContext());
StringRequest stringRequest = new
StringRequest(Request.Method.POST, v.getContext().getResources().getString(R.string.url)
+ "deleteschedule.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.optString("success").equals("1"))
{
Toast.makeText(v.getContext(), "Schedule
deleted successfully!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(),
jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(v.getContext(), error.toString(),
Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String,
String>();
params.put("course_code", schedule.getCourseCode());
return params;
}

@Override
public Map<String, String> getHeaders() throws
AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-
urlencoded");
return params;
}
};
rQueue.add(stringRequest);
return false;
} else {
return false;
}
return true;
}
});
}
});
}
@Override
public int getItemCount() {
if (list != null) {
return list.size();
} else {
return 0;
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public final View view;
public final TextView course_code;
public final TextView course_title;
public final TextView course_schedule;
public final TextView course_instructor;
public final ImageView flowmenu;
public ViewHolder(View view) {
super(view);
this.view = view;
course_code = view.findViewById(R.id.course_code);
course_title = view.findViewById(R.id.course_title);
course_schedule = view.findViewById(R.id.course_schedule);
course_instructor = view.findViewById(R.id.course_instructor);
flowmenu = view.findViewById(R.id.flowmenu);
}
}
}
43. Create the java class SchedulesFragment. Delete all existing codes. Type the following Java codes.
package <type your package name here>;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class SchedulesFragment extends Fragment {
private RecyclerView rv;
private RecyclerView.Adapter adapter;
RequestQueue rQueue;
ArrayList<Schedule> list;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle


savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_schedules,container, false);

list = new ArrayList<>();


rv = (RecyclerView) view.findViewById(R.id.rvschedules);
RecyclerView.LayoutManager mLayoutManager = new
LinearLayoutManager(this.getContext());
rv.setLayoutManager(mLayoutManager);

StringRequest stringRequest = new StringRequest(Request.Method.POST,


getResources().getString(R.string.url) + "getschedules.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
rQueue.getCache().clear();
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.optString("success").equals("1")) {
JSONArray jsonArray = jsonObject.getJSONArray("details");
//Toast.makeText(getContext(), "jsonArray: " +
jsonArray.toString(), Toast.LENGTH_LONG).show();
for (int i=0; i<jsonArray.length(); ++i) {
JSONObject itemObj = jsonArray.getJSONObject(i);
String course_code =
itemObj.getString("course_code");
String course_title =
itemObj.getString("course_title");
String course_schedule =
itemObj.getString("course_schedule");
String course_instructor =
itemObj.getString("course_instructor");

//Toast.makeText(getContext(), "schedule: " +


course_code + "," + course_title + "," + course_schedule + "," + course_instructor,
Toast.LENGTH_LONG).show();

list.add(new
Schedule(course_code,course_title,course_schedule,course_instructor));
}
adapter = new ScheduleAdapter(list);
rv.setAdapter(adapter);
} else {
Toast.makeText(getContext(),
jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.toString(),
Toast.LENGTH_LONG).show();
}
});
rQueue = Volley.newRequestQueue(getContext());
rQueue.add(stringRequest);

return view;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
}

@Override
public void onDetach() {
super.onDetach();
}
}
44. Create the java class AddScheduleFragment. Delete all existing codes. Type the following Java codes.
package <type your package name here>;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class AddScheduleFragment extends Fragment {

EditText tcourse_code, tcourse_title, tcourse_schedule, tcourse_instructor;


Button bsave;
Context ctx;
RequestQueue rQueue;

public AddScheduleFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_schedule,container, false);

tcourse_code = view.findViewById(R.id.course_code);
tcourse_title = view.findViewById(R.id.course_title);
tcourse_schedule = view.findViewById(R.id.course_schedule);
tcourse_instructor = view.findViewById(R.id.course_instructor);
bsave = view.findViewById(R.id.save_btn);
bsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String course_code = tcourse_code.getText().toString();
final String course_title = tcourse_title.getText().toString();
final String course_schedule = tcourse_schedule.getText().toString();
final String course_instructor = tcourse_instructor.getText().toString();

if (course_code.isEmpty()) {
tcourse_code.setError("Course code is required");
tcourse_code.requestFocus();
return;
}
if (course_title.isEmpty()) {
tcourse_title.setError("Course title is required");
tcourse_title.requestFocus();
return;
}
if (course_schedule.isEmpty()) {
tcourse_schedule.setError("Course schedule is required");
tcourse_schedule.requestFocus();
return;
}
if (course_instructor.isEmpty()) {
tcourse_instructor.setError("Course instructor is required");
tcourse_instructor.requestFocus();
return;
}

rQueue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest(Request.Method.POST,
getResources().getString(R.string.url) + "addschedule.php", new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.optString("success").equals("1")) {
Toast.makeText(getContext(), "New schedule added
successfully!", Toast.LENGTH_SHORT).show();
tcourse_code.setText("");
tcourse_title.setText("");
tcourse_schedule.setText("");
tcourse_instructor.setText("");
} else {
Toast.makeText(getContext(),
jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
tcourse_code.setText("");
tcourse_title.setText("");
tcourse_schedule.setText("");
tcourse_instructor.setText("");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.toString(),
Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("course_code", course_code);
params.put("course_title", course_title);
params.put("course_schedule", course_schedule);
params.put("course_instructor", course_instructor);
return params;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
rQueue.add(stringRequest);
}
});
return view;
}
}
45. Create layout for activity_login. Delete existing codes. Type the following codes.
<?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=".LoginActivity">

<ImageView
android:id="@+id/imageView5"
android:layout_width="155dp"
android:layout_height="142dp"
android:layout_marginTop="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />

<EditText
android:id="@+id/useremail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/imageView5"
app:layout_constraintStart_toStartOf="@+id/imageView5"
app:layout_constraintTop_toBottomOf="@+id/imageView5" />

<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/useremail"
app:layout_constraintStart_toStartOf="@+id/useremail"
app:layout_constraintTop_toBottomOf="@+id/useremail" />

<Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="96dp"
android:onClick="login"
android:text="Login"
app:layout_constraintEnd_toEndOf="@+id/password"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintStart_toStartOf="@+id/password"
app:layout_constraintTop_toBottomOf="@+id/password" />

<Button
android:id="@+id/register_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:onClick="register"
android:text="Register "
app:layout_constraintEnd_toEndOf="@+id/login_btn"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="@+id/login_btn"
app:layout_constraintTop_toBottomOf="@+id/login_btn" />

</androidx.constraintlayout.widget.ConstraintLayout>

46. Create layout for activity_register. Delete existing codes. Type the following codes.
<?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=".RegisterActivity">

<ImageView
android:id="@+id/imageView5"
android:layout_width="155dp"
android:layout_height="142dp"
android:layout_marginTop="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />

<EditText
android:id="@+id/useremail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/imageView5"
app:layout_constraintStart_toStartOf="@+id/imageView5"
app:layout_constraintTop_toBottomOf="@+id/imageView5" />

<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Enter User Name"
android:inputType="text"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/useremail"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/useremail"
app:layout_constraintTop_toBottomOf="@+id/useremail" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/username"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/username"
app:layout_constraintTop_toBottomOf="@+id/username" />

<EditText
android:id="@+id/password1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Verify Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/password"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/password"
app:layout_constraintTop_toBottomOf="@+id/password" />

<Button
android:id="@+id/register_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:onClick="register"
android:text="Register "
app:layout_constraintEnd_toEndOf="@+id/password1"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="@+id/password1"
app:layout_constraintTop_toBottomOf="@+id/password1" />

<Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="login"
android:text="Login"
app:layout_constraintEnd_toEndOf="@+id/register_btn"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintStart_toStartOf="@+id/register_btn"
app:layout_constraintTop_toBottomOf="@+id/register_btn" />

</androidx.constraintlayout.widget.ConstraintLayout>

47. Create layout for fragment_add_schedule. Delete existing codes. Type the following codes.
<?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=".AddScheduleFragment">

<ImageView
android:id="@+id/imageView5"
android:layout_width="155dp"
android:layout_height="142dp"
android:layout_marginTop="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />

<EditText
android:id="@+id/course_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Enter Course Code"
android:inputType="text"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/imageView5"
app:layout_constraintStart_toStartOf="@+id/imageView5"
app:layout_constraintTop_toBottomOf="@+id/imageView5" />

<EditText
android:id="@+id/course_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Enter Course Title"
android:inputType="text"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/course_code"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/course_code"
app:layout_constraintTop_toBottomOf="@+id/course_code" />

<EditText
android:id="@+id/course_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Course Schedule"
android:inputType="text"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/course_title"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/course_title"
app:layout_constraintTop_toBottomOf="@+id/course_title" />
<EditText
android:id="@+id/course_instructor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Enter Course Instructor"
android:inputType="text"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/course_schedule"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/course_schedule"
app:layout_constraintTop_toBottomOf="@+id/course_schedule" />

<Button
android:id="@+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:onClick="save"
android:text="Save"
app:layout_constraintEnd_toEndOf="@+id/course_instructor"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="@+id/course_instructor"
app:layout_constraintTop_toBottomOf="@+id/course_instructor" />

</androidx.constraintlayout.widget.ConstraintLayout>

48. Create layout for fragment_profile. Delete existing codes. Type the following codes.
<?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=".ProfileFragment">

<ImageView
android:id="@+id/imageView5"
android:layout_width="155dp"
android:layout_height="142dp"
android:layout_marginTop="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />

<EditText
android:id="@+id/useremail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:minHeight="48dp"
android:enabled="false"
app:layout_constraintEnd_toEndOf="@+id/imageView5"
app:layout_constraintStart_toStartOf="@+id/imageView5"
app:layout_constraintTop_toBottomOf="@+id/imageView5" />
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Enter User Name"
android:inputType="text"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/useremail"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/useremail"
app:layout_constraintTop_toBottomOf="@+id/useremail" />

<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/username"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/username"
app:layout_constraintTop_toBottomOf="@+id/username" />

<EditText
android:id="@+id/password1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Verify Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/password"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/password"
app:layout_constraintTop_toBottomOf="@+id/password" />

<Button
android:id="@+id/update_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Update"
app:layout_constraintEnd_toEndOf="@+id/password1"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="@+id/password1"
app:layout_constraintTop_toBottomOf="@+id/password1" />

</androidx.constraintlayout.widget.ConstraintLayout>
49. Create layout for fragment_schedules. Delete existing codes. Type the following codes.

<?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=".SchedulesFragment">

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Schedules"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/rvschedules"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvschedules"
android:layout_width="416dp"
android:layout_height="662dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
</androidx.constraintlayout.widget.ConstraintLayout>

50. Create layout for schedule. Delete existing codes. Type the following codes.
<?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="100dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@color/material_dynamic_neutral95">

<ImageView
android:id="@+id/imageView6"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginBottom="2dp"
android:layout_marginStart="16dp"
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toEndOf="@+id/imageView6"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/lbl_course_code"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Code"
android:textSize="14dp"
android:textStyle="bold" />

<TextView
android:id="@+id/course_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code"
android:textSize="14dp" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/lbl_course_title"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="14dp"
android:textStyle="bold" />

<TextView
android:id="@+id/course_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="14dp" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/lbl_course_schedule"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Schedule"
android:textSize="14dp"
android:textStyle="bold" />

<TextView
android:id="@+id/course_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schedule"
android:textSize="14dp" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/lbl_course_instructor"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Instructor"
android:textSize="14dp"
android:textStyle="bold" />

<TextView
android:id="@+id/course_instructor"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="Instructor"
android:textSize="14dp" />
</TableRow>
</LinearLayout>

<ImageButton
android:id="@+id/flowmenu"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginBottom="36dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="32dp"
android:background="@null"
android:src="@drawable/baseline_more_vert_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias=".50"
app:layout_constraintStart_toEndOf="@+id/linearLayout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
51. Double-click menu activity_main_drawer. Add the following (green colored code):

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">

...

<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_profile" />
</group>
<item android:title= "Schedules" >
<menu>
<item
android:id="@+id/nav_show_schedules"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_show_schedules" />
<item
android:id= "@+id/nav_add_schedule"
android:icon= "@drawable/ic_menu_camera"
android:title= "@string/menu_add_schedule" />
</menu>
</item>
</menu>

52. Create a menu. Name it flowmenu.xml. Delete existing codes. Type the following codes.

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Edit"
android:id="@+id/edit_menu"/>
<item
android:title="Delete"
android:id="@+id/delete_menu"/>
</menu>

53. Double-click the navigation mobile_navigation.xml. Add the following codes (green colored code).
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mobile_navigation"
app:startDestination="@id/nav_home">

...

<fragment
android:id="@+id/nav_profile"
android:name="com.hktracker.ProfileFragment"
android:label="@string/menu_profile" />

<fragment
android:id="@+id/nav_show_schedules"
android:name="com.hktracker.SchedulesFragment"
android:label="@string/menu_show_schedules" />
<fragment
android:id="@+id/nav_add_schedule"
android:name="com.hktracker.AddScheduleFragment"
android:label="@string/menu_add_schedule" />
</navigation>

54. Double-click the values strings.xml. Add the following codes (green colored codes):
<resources>
...
<string name="menu_profile">Profile</string>
<string name="menu_show_schedules">Show Schedules</string>
<string name="menu_add_schedule">Add Schedule</string>
<string name="url">https:/auhkscholarshub.000webhostapp.com/</string>

<string name="nav_open">Open</string>
<string name="nav_close">Close</string>

</resources>

55. Build the program.


56. Install and test on your Android smartphone.

Screenshots:

You might also like