Android Project – Organ Donation App

FREE Online Courses: Click, Learn, Succeed, Start Now!

Welcome! In this project, we will try to create an Android application for organ donation tracking using Android Studio. Join us as we dive into the world of Android app development and work towards building an application that promotes the noble cause of organ donation. Let’s get started right away!

About Android Organ Donation App

Organ donation is a remarkable act of kindness that has the potential to save lives and bring hope to those in need. With the aim of spreading awareness and facilitating the organ donation process, our app serves as a valuable resource for individuals interested in becoming organ donors.

Through this app, users can access vital information about organ donation, find local organ donation centres, and stay informed about the other recipients in need. By leveraging technology, we hope to inspire more people to become organ donors, ultimately contributing to a brighter and healthier future for all.

Flow of Application:

The organ donation app follows a user-friendly flow that simplifies the process of organ donation. Here’s a breakdown of its main features and the flow of the app:

  •  Donor Registration: Users who wish to become organ donors can easily register through the app. They will have the option to specify their organ donation preferences, blood type, age, addresses and phone number.
  •  Organ Donation Hospitals: The app offers a directory of nearby organ donation centres and hospitals. Users can search for centres based on location, availability of specific organs, and other relevant criteria.
  •  Blood Specification: The app offers a directory of specific donors based on the blood types of the donors so that they can easily find the most compatible organs.
  •  Organ Specification: The app offers a directory of specific organs so that they can easily find the exact organs that they might be looking for.

Requirements for The Project:

To develop an Android Organ Donation project, you’ll need the following requirements:

  • Knowledge of Java or Kotlin.
  •  Android Studio.
  •  Understanding of Android Input Methods.
  •  Understanding of Firebase Hosting and Database.
  •  Knowledge of XML Layout Design.
  •  Android Keyboard API
  •  User Interface Design Concepts
  •  Object Oriented Programming Concepts

By fulfilling these requirements, you’ll be well-equipped to develop a custom Android organ donation project.

Download Android Organ Donation App Project.

Please download the source code of the Android Organ Donation App Project:  Android Organ Donation App Project Code.

Project Description:

On downloading the code, you are going to see many folders. This section gives you a brief about which folders and files are essential for your project:

Manifest File (AndroidManifest.xml): The manifest file gives the Android system crucial details about your app, including the package name, activities, services, permissions, and hardware requirements.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Java Class Code (com.example.orgdon) : There are multiple classes in our project. Dividing our whole project into these multiple classes offers advantages such as modularity, code organisation, reusability, scalability, maintainability, and improved readability and debugging. The whole code can be divided into five types:
o Organ List and Classes
o Blood List and Classes
o Donor and Hospital Lists Classes
o Main Activity and Firebase Classes
o Adapter Class

Resources:
o drawables: Contains the icons/vectors required for the application.
o layout: Contains the basic designs of the different pages of the application.
o values: Contains the references to different values used in the application
o menu: Contains the layout of the search menu option

Gradle Scripts: Contains all the dependencies and libraries that we will import in our project.

Steps To Implement the Project:

1. Download the source code.
2. Locate the zip file and unzip.
3. Open Android Studio and click on “Open an Existing Project”.
4. Locate and select the source code folder.
5. The IDE should look like this:

ide

6. On opening the folder in Android Studio, make sure the gradle scripts and the settings are suitable for your IDE and SDK.
7. Work around the files and try to figure out what they all do. Start with AndroidManifest.xml and follow the different codes to different ends.

Main Activity:

This is the front page file of our application. ImageView and TextView are holder for our images and texts, and there are certain buttons that, on click, go to their specific pages.

activity_main.xml:

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

<!--This is our front page-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:layout_marginTop="40dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:src="@drawable/log" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingBottom="15dp"
            android:text="Organ Donation"
            android:textColor="@color/black"
            android:textSize="30sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:gravity="center"
            android:paddingHorizontal="10dp"
            android:text="Giving a second change of life is in your hands."
            android:textColor="@color/black"
            android:textSize="16sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/recyclerviewbtn"
            android:layout_width="360dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:layout_marginVertical="10dp"
            android:backgroundTint="#F44336"
            android:text="ORGAN DONORS" />

        <Button
            android:id="@+id/groupList"
            android:layout_width="360dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:layout_marginVertical="10dp"
            android:backgroundTint="#F44336"
            android:text="By Blood Group" />

        <Button
            android:id="@+id/frequency"
            android:layout_width="360dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:layout_marginVertical="10dp"
            android:backgroundTint="#F44336"
            android:text="Organs" />

        <Button
            android:id="@+id/place"
            android:layout_width="360dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:layout_marginVertical="10dp"
            android:backgroundTint="#F44336"
            android:text="HOSPITALS" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginVertical="30dp"
        android:gravity="center|bottom">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:src="@drawable/download"
            android:gravity="bottom" />

    </LinearLayout>

</LinearLayout>

MainActivity.java:

/*A DataFlair Guide*/


package com.example.orgdon;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Objects;

/*
This part of the code is called first.
Here, we initialize and call our Front Page.
*/

public class MainActivity extends AppCompatActivity {

    Button donors, bloodgroup, organs, hospitals;

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

        //This is the layout of our Front Page.
        setContentView(R.layout.activity_main);

        Objects.requireNonNull(getSupportActionBar()).hide();

        //These are the four buttons that we see in our front page.
        //Each button has it's own ID in the xml part of the program, so that
        //each button can be referred to and called easily.
        donors = findViewById(R.id.recyclerviewbtn);
        bloodgroup = findViewById(R.id.frequency);
        organs = findViewById(R.id.groupList);
        hospitals = findViewById(R.id.place);

        //On clicking the respective button we go to the respective pages.
        donors.setOnClickListener(v -> {
            Intent i = new Intent(MainActivity.this, DonorList.class);
            startActivity(i);
            finish();
        });

        organs.setOnClickListener(view -> {
            Intent i = new Intent(MainActivity.this, Organs.class);
            startActivity(i);
            finish();
        });

        bloodgroup.setOnClickListener(view -> {
            Intent i = new Intent(MainActivity.this, GroupList.class);
            startActivity(i);
            finish();
        });

        hospitals.setOnClickListener(view -> {
            Intent i = new Intent(MainActivity.this, HospitalList.class);
            startActivity(i);
            finish();
        });
    }
}

front page of application

Donor List:

item.xml: This file contains the basic layout of the display list that we are going to use.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="6dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginVertical="5dp"
            android:weightSum="10"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_weight="8"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Name :"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/nameText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:text="Yashraj Kaflay"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Address :"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/address"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:text="Rangamati"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Organ :"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/lastDonated"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:text="Liver"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Age :"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/tvage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:text="18"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Hospital :"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/hospital"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:text="St. Mary's"
                        android:textColor="@color/black"
                        android:textSize="20sp" />

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:weightSum="10">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="5">

                        <TextView
                            android:id="@+id/bloodgroup"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center"
                            android:text="O+"
                            android:textColor="#FF0000"
                            android:textSize="25sp"
                            android:textStyle="bold" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="5">

                        <ImageButton
                            android:id="@+id/phone"
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:layout_gravity="center"
                            android:background="#fff"
                            android:src="@drawable/phone" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/editButton"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="Edit" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/deleteButton"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="Delete" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>

donor list

activity_donor_list.xml: This is the part of the code that takes all the different items of the list and arranges them in proper list-like order.

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.884"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.945"
        app:srcCompat="@drawable/add"
        android:focusable="true" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity donor list xml

DonorList.java:

package com.example.orgdon;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.database.FirebaseDatabase;

import java.util.Objects;

/*
This is the 'Organ Donors' page of our program.
*/

public class DonorList extends AppCompatActivity {

    RecyclerView recView;
    MyAdapter adapter;
    FloatingActionButton fb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //On creation of the page, the program calls the activity_donor_list.xml file
        //in the layout folder which is the layout of this page.
        setContentView(R.layout.activity_donor_list);
        setTitle("Search Donors");

        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);


        //This code allows the return arrow in the page, but it is not complete without
        //some action.
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        //We use this part of the code to generate the Card view of the donor list.
        recView = findViewById(R.id.recview);
        recView.setLayoutManager(new LinearLayoutManager(this));

        // We fetch all Donors from Firebase
        FirebaseRecyclerOptions<model> options =
                new FirebaseRecyclerOptions.Builder<model>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("donors"), model.class)
                        .build();

        adapter = new MyAdapter(options);
        recView.setAdapter(adapter);

        //This part allows the '+' button at the bottom-right of the screen.
        //This allows us to add an entry to our database.
        fb = findViewById(R.id.fadd);
        fb.setOnClickListener(view -> startActivity(new Intent(getApplicationContext(), AddDonor.class)));
    }

    // This is where we specify where will the program go if we press back.
    // As you can see it goes back to the MainActivity page.
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    // This is the second part of the return arrow on the top left corner of the
    // screen. This part of the code allows us to go back to the last page, which we
    // have to specify(MainActivity)
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    // If we have added some entry to our database, this notifies us that, either
    // entering the data was successfull or not.
    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onStart() {
        super.onStart();
        recView.getRecycledViewPool().clear();
        adapter.startListening();
        recView.getRecycledViewPool().clear();
        adapter.notifyDataSetChanged();

    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }

    // This part of the code helps us use the search button
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.searchmenu, menu);
        MenuItem item = menu.findItem(R.id.search);
        SearchView searchView = (SearchView) item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            // We take the string from the search bar and compare it our database entries
            // based on some specific criteria.
            @Override
            public boolean onQueryTextSubmit(String s) {
                processSearch(s.length() == 0 ? s : s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                processSearch(s.length() == 0 ? s : s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

    private void processSearch(String s) {
        // This is where we specify the criteria to filter our database and display
        // accordingly. In this case, we filter our search for donor according to
        // their names.
        FirebaseRecyclerOptions<model> options =
                new FirebaseRecyclerOptions.Builder<model>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("donors").orderByChild("name").startAt(s).endAt(s + "\uf8ff"), model.class)
                        .build();

        adapter = new MyAdapter(options);
        adapter.startListening();
        recView.setAdapter(adapter);
    }


}

activity_add_donor.xml: The page to add donors to the list.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".AddDonor">

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/addName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Name" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/addAddress"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Address" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/addDonated"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Organ Donated" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/addAge"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Age" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/addBlood"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Blood Group" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="30dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/addPhone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Phone" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginBottom="30dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/addHospital"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Hospital" />

    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/add_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:text="Add to Database"
        android:textSize="20sp" />

    <Button
        android:id="@+id/add_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back"
        android:textSize="20sp" />

</androidx.appcompat.widget.LinearLayoutCompat>

AddDonor.java:

package com.example.orgdon;

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

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.FirebaseDatabase;

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

// This is the Add Donor page which we get on pressing the '+' button on the
// Organ Donor List Page.
public class AddDonor extends AppCompatActivity {

    // Here, we specify the  different Text fields and buttons we are using in the page.
    EditText name, address, donated, age, blood, phone, hospital;
    Button submit, back;

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

        name = findViewById(R.id.addName);
        address = findViewById(R.id.addAddress);
        donated = findViewById(R.id.addDonated);
        age = findViewById(R.id.addAge);
        blood = findViewById(R.id.addBlood);
        phone = findViewById(R.id.addPhone);
        hospital = findViewById(R.id.addHospital);

        back = findViewById(R.id.add_back);
        back.setOnClickListener(view -> {
            Intent intent = new Intent(getApplicationContext(), DonorList.class);
            startActivity(intent);
        });

        submit = findViewById(R.id.add_submit);
        submit.setOnClickListener(view -> processInsert());
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, DonorList.class);
        startActivity(intent);
    }

    // This part of the code gets the text from the Text fields and then
    // inserts those values into a Firebase database.
    private void processInsert() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name.getText().toString());
        map.put("address", address.getText().toString());
        map.put("donated", donated.getText().toString());
        map.put("age", age.getText().toString());
        map.put("blood", blood.getText().toString());
        map.put("phone", phone.getText().toString());
        map.put("hospital", hospital.getText().toString());

        // Add Donors to Firebase Database
        FirebaseDatabase.getInstance().getReference().child("donors").push()
                .setValue(map)
                .addOnSuccessListener(aVoid -> {
                    name.setText("");
                    address.setText("");
                    donated.setText("");
                    age.setText("");
                    blood.setText("");
                    phone.setText("");
                    hospital.setText("");
                    Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(getApplicationContext(), DonorList.class));
                })
                .addOnFailureListener(e -> Toast.makeText(getApplicationContext(), "Could not Added", Toast.LENGTH_LONG).show());

    }
}

donor list java

dialogcontent.xml: This file is used to update any of the records of the list.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#F44336"
        android:gravity="center"
        android:text="Update Donors Data"
        android:textColor="#fff"
        android:textSize="25sp" />

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/nameEdit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Name" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/addressEdit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Address" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/donatedEdit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Organ" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/ageEdit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Age" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/bloodEdit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Blood Group" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/phoneEdit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Phone" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="20dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/hospitalEdit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Hospital" />

    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/usubmit"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="10dp"
        android:background="#009688"
        android:text="Update"
        android:textColor="#fff"
        android:textSize="20sp" />

</LinearLayout>

diagonal content

MyAdapter.java:

package com.example.orgdon;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.material.textfield.TextInputEditText;
import com.google.firebase.database.FirebaseDatabase;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.ViewHolder;

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

// This is an important class which helps us do a lot of things in our project.

public class MyAdapter extends FirebaseRecyclerAdapter<model, MyAdapter.myViewHolder> {
    public MyAdapter(@NonNull FirebaseRecyclerOptions<model> options) {
        super(options);
    }


    @Override
    protected void onBindViewHolder(@NonNull final myViewHolder holder, @SuppressLint("RecyclerView") final int position, @NonNull final model model) {
        holder.name.setText(model.getName());
        holder.address.setText(model.getAddress());
        holder.donated.setText(model.getDonated());
        holder.age.setText(model.getAge());
        holder.blood.setText(model.getBlood());
        holder.hospital.setText(model.getHospital());


        // This part of the code is for when the call button is pressed when viewing the
        // list. This code takes the user to the phone application.
        holder.call.setOnClickListener(v -> {
            String call = model.getPhone();
            String s = "tel:" + call;
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(s));
            v.getContext().startActivity(intent);

        });

        // This is the layout that is shown on pressing the edit button.
        holder.edit.setOnClickListener(view -> {
            final DialogPlus dialogPlus = DialogPlus.newDialog(holder.name.getContext())
                    .setContentHolder(new ViewHolder(R.layout.dialogcontent))
                    .setExpanded(true, 2400)
                    .create();

            // If we want to update our database we click on the entry and press edit.
            // On pressing Edit, this part of our program get invoked. This next code rewrites
            // the values into the Text fields.

            View myView = dialogPlus.getHolderView();
            final TextInputEditText name = myView.findViewById(R.id.nameEdit);
            final TextInputEditText address = myView.findViewById(R.id.addressEdit);
            final TextInputEditText donated = myView.findViewById(R.id.donatedEdit);
            final TextInputEditText age = myView.findViewById(R.id.ageEdit);
            final TextInputEditText blood = myView.findViewById(R.id.bloodEdit);
            final TextInputEditText phone = myView.findViewById(R.id.phoneEdit);
            final TextInputEditText hospital = myView.findViewById(R.id.hospitalEdit);
            Button submit = myView.findViewById(R.id.usubmit);

            name.setText(model.getName());
            address.setText(model.getAddress());
            donated.setText(model.getDonated());
            age.setText(model.getAge());
            blood.setText(model.getBlood());
            phone.setText(model.getPhone());
            hospital.setText(model.getHospital());

            dialogPlus.show();

            // Pressing the submit button then updates the data to the Firebase database.

            submit.setOnClickListener(view1 -> {
                Map<String, Object> map = new HashMap<>();
                map.put("name", Objects.requireNonNull(name.getText()).toString());
                map.put("address", Objects.requireNonNull(address.getText()).toString());
                map.put("donated", Objects.requireNonNull(donated.getText()).toString());
                map.put("age", Objects.requireNonNull(age.getText()).toString());
                map.put("blood", Objects.requireNonNull(blood.getText()).toString());
                map.put("phone", Objects.requireNonNull(phone.getText()).toString());
                map.put("hospital", Objects.requireNonNull(hospital.getText()).toString());

                FirebaseDatabase.getInstance().getReference().child("donors")
                        .child(Objects.requireNonNull(getRef(position).getKey())).updateChildren(map)
                        .addOnSuccessListener(aVoid -> dialogPlus.dismiss())
                        .addOnFailureListener(e -> dialogPlus.dismiss());
            });

        });

        // This is the Delete button that is present beside the Edit button.

        holder.delete.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(holder.name.getContext());
            builder.setTitle("Delete Donors?");
            builder.setMessage("Are you sure?");

            builder.setPositiveButton("Yes", (dialogInterface, i) -> FirebaseDatabase.getInstance().getReference().child("donors")
                    .child(Objects.requireNonNull(getRef(position).getKey())).removeValue());

            builder.setNegativeButton("No", (dialogInterface, i) -> {

            });

            builder.show();
        });

    }


    // This part of the code is used to create the specific card view layout of the
    // donor's list that we view.
    @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new myViewHolder(view);
    }

    static class myViewHolder extends RecyclerView.ViewHolder {

        final Button edit;
        final Button delete;
        final ImageButton call;
        final TextView name;
        final TextView address;
        final TextView donated;
        final TextView age;
        final TextView blood;

        final TextView hospital;

        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.nameText);
            address = itemView.findViewById(R.id.address);
            donated = itemView.findViewById(R.id.lastDonated);
            age = itemView.findViewById(R.id.tvage);
            blood = itemView.findViewById(R.id.bloodgroup);
            hospital = itemView.findViewById(R.id.hospital);
            call = itemView.findViewById(R.id.phone);
            edit = itemView.findViewById(R.id.editButton);
            delete = itemView.findViewById(R.id.deleteButton);
        }
    }
}

OfflineFirebase.java:

package com.example.orgdon;

import android.app.Application;

import com.google.firebase.database.FirebaseDatabase;

//This class is to initialize our firebase database
public class OfflineFirebase extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        //Extra COde
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    }
}

Blood List:

activity_group_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".GroupList">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_marginTop="30dp"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:text="Blood Group"
            android:textColor="@color/black"
            android:textSize="25sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginHorizontal="30dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/aPositive"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="A+" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginHorizontal="30dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/aNegative"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="A-" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginHorizontal="30dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/bPositive"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="B+" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginHorizontal="30dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/bNegative"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="B-" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginHorizontal="30dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/abPositive"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="AB+" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginHorizontal="30dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/abNegative"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="AB-" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginHorizontal="30dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/oPositive"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="O+" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginHorizontal="30dp"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/oNegative"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:backgroundTint="#F44336"
                    android:gravity="center"
                    android:text="O-" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

GroupList.java:

package com.example.orgdon;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

//This is the Blood Group List page.

public class GroupList extends AppCompatActivity {

    //The button of the page are specified here and the layout of the page is called below.
    Button aPositive, aNegative, bPositive, bNegative, abPositive, abNegative, oPositive, oNegative;

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

        aPositive = findViewById(R.id.aPositive);
        aNegative = findViewById(R.id.aNegative);
        bPositive = findViewById(R.id.bPositive);
        bNegative = findViewById(R.id.bNegative);
        abPositive = findViewById(R.id.abPositive);
        abNegative = findViewById(R.id.abNegative);
        oPositive = findViewById(R.id.oPositive);
        oNegative = findViewById(R.id.oNegative);


        //Each button on being clicked goes to it's respective page.

        aPositive.setOnClickListener(view -> {
            Intent i = new Intent(GroupList.this, APositive.class);
            startActivity(i);
        });

        aNegative.setOnClickListener(view -> {
            Intent i = new Intent(GroupList.this, ANegative.class);
            startActivity(i);
        });

        bPositive.setOnClickListener(view -> {
            Intent i = new Intent(GroupList.this, BPositive.class);
            startActivity(i);
        });

        bNegative.setOnClickListener(view -> {
            Intent i = new Intent(GroupList.this, BNegative.class);
            startActivity(i);
        });

        abPositive.setOnClickListener(view -> {
            Intent i = new Intent(GroupList.this, ABPositive.class);
            startActivity(i);
        });

        abNegative.setOnClickListener(view -> {
            Intent i = new Intent(GroupList.this, ABNegative.class);
            startActivity(i);
        });

        oPositive.setOnClickListener(view -> {
            Intent i = new Intent(GroupList.this, OPositive.class);
            startActivity(i);
        });

        oNegative.setOnClickListener(view -> {
            Intent i = new Intent(GroupList.this, com.example.orgdon.ONegative.class);
            startActivity(i);
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

organ donation app

8. Press the run button at the top bar; it will install the application on a virtual device.
9. Test if the application runs correctly.
10. Customise to your taste.

Android Organ Donation App Output:

The application should look like this:

This is the home page.

organ donation home page

We get this on opening the “Organ Donors” button.

organ donors button

This is how we “Add” and “Edit” our list.

organ donation add and edit button

This is what we get on pressing “By Blood Group” and “Organs” respectively.

organ donation blood group and organ

This is what we get on pressing hospitals and searching for anything specific in hospitals search bar.

organ donation app output

Summary:

This guide explores the development of an Android organ donation app, highlighting the importance of organ donation and providing step-by-step instructions for implementing the app using Android Studio. By leveraging multiple classes, users can register as donors, access information, find donation centres, and connect with recipients.

Through the power of technology, this app aims to increase awareness and participation in organ donation, ultimately saving lives and fostering a healthier future for all. Hope you found this guide insightful. Thank you for reading!

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *