🧱 UI Layouts
-
activity_main.xml: This is the main input screen where users can add new doctor details. It uses a
ScrollView
to ensure smooth vertical scrolling on smaller screens, and aLinearLayout
to arrange input fields and buttons neatly. -
activity_listview.xml: A simple layout containing a
ListView
to display all stored doctor entries in a readable list format.
Both layouts are styled with custom drawable backgrounds to enhance the visual appeal.
⚙️ MainActivity
This is the heart of the app where:
-
All input fields are initialized and linked to the layout.
-
A click on the "Submit" button collects user input, creates a
Doctor
object, and saves it into the database. -
After successful insertion, fields are cleared, and a toast message confirms the action.
-
The "View Doctors List" button opens another activity that shows all stored records.
🗃️ DBHelper
This class handles all database operations:
-
It creates a SQLite database named
DoctorsDB
. -
When the app is run for the first time, a table named
doctor
is created with fields like ID, name, mobile number, etc. -
It provides two key methods: one for inserting a new doctor and one for fetching all doctor entries from the database.
👨⚕️ Doctor Model Class
A simple Java class that defines the structure of a doctor object. It contains fields like ID, first name, last name, and specialization. This makes it easier to store and retrieve data in a structured way and display it as a single list item.
📋 DoctorListActivity
This activity displays all saved doctors using a ListView
and an ArrayAdapter
. When a user clicks "View Doctors List" from the main screen, this activity shows the complete list of entries pulled directly from the SQLite database.
🧩 How It All Comes Together
-
The user enters doctor details in the form.
-
When "Submit" is clicked:
-
A
Doctor
object is created. -
The object is saved into the database using
DBHelper
.
-
-
Clicking "View Doctors List" opens a new screen with all saved records listed using
ListView
.
🎨 UI & UX Enhancements
-
Custom background gradients and drawable styles make the UI modern and vibrant.
-
Elevation and padding give a material design feel.
-
Button states can include hover/touch animations using XML selectors.
-
The app is mobile-friendly and responsive across screen sizes.
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_bg"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/linear_border"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Doctors Directory"
android:textColor="@color/white"
android:textSize="45sp"
android:textStyle="bold" />
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="5dp"
android:background="@drawable/edittext_background"
android:elevation="8dp"
android:hint="Doctor ID"
android:inputType="number"
android:padding="15dp"
android:textColor="#111"
android:textSize="18sp" />
<EditText
android:id="@+id/fname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/edittext_background"
android:elevation="8dp"
android:hint="First Name"
android:padding="15dp"
android:textColor="#111"
android:textSize="18sp" />
<EditText
android:id="@+id/lname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/edittext_background"
android:elevation="8dp"
android:hint="Last Name"
android:padding="15dp"
android:textColor="#111"
android:textSize="18sp" />
<EditText
android:id="@+id/mobile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/edittext_background"
android:elevation="8dp"
android:hint="Mobile Number"
android:inputType="phone"
android:padding="15dp"
android:textColor="#111"
android:textSize="18sp" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/edittext_background"
android:elevation="8dp"
android:hint="Address"
android:padding="15dp"
android:textColor="#111"
android:textSize="18sp" />
<EditText
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/edittext_background"
android:elevation="8dp"
android:hint="City"
android:padding="15dp"
android:textColor="#111"
android:textSize="18sp" />
<EditText
android:id="@+id/specialization"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/edittext_background"
android:elevation="8dp"
android:hint="Specialization"
android:padding="15dp"
android:textColor="#111"
android:textSize="18sp" />
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@drawable/button_unique_background"
android:text="Submit"
android:textSize="20sp" />
<Button
android:id="@+id/viewList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/button_unique_background"
android:text="View Doctors List"
android:textSize="20sp"
android:visibility="gone" />
</LinearLayout>
</ScrollView>
activity_listview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
package com.tecvipul.doctorinfo;
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;
public class MainActivity extends AppCompatActivity {
EditText id, fname, lname, mobile, address, city, specialization;
Button submit, viewList;
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBHelper(this);
id = findViewById(R.id.id);
fname = findViewById(R.id.fname);
lname = findViewById(R.id.lname);
mobile = findViewById(R.id.mobile);
address = findViewById(R.id.address);
city = findViewById(R.id.city);
specialization = findViewById(R.id.specialization);
submit = findViewById(R.id.submit);
viewList = findViewById(R.id.viewList);
submit.setOnClickListener(v -> {
Doctor doctor = new Doctor(
Integer.parseInt(id.getText().toString()),
fname.getText().toString(),
lname.getText().toString(),
mobile.getText().toString(),
address.getText().toString(),
city.getText().toString(),
specialization.getText().toString()
);
db.insertDoctor(doctor);
Toast.makeText(this, "Doctor Saved!", Toast.LENGTH_SHORT).show();
id.setText("");
fname.setText("");
lname.setText("");
mobile.setText("");
address.setText("");
city.setText("");
specialization.setText("");
});
viewList.setOnClickListener(v -> {
startActivity(new Intent(this, DoctorListActivity.class));
});
}
}
DBHelper.java
package com.tecvipul.doctorinfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "DoctorsDB";
private static final int DB_VERSION = 1;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE doctor(id INTEGER PRIMARY KEY, fname TEXT, lname TEXT, mobile TEXT, address TEXT, city TEXT, specialization TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS doctor");
onCreate(db);
}
public void insertDoctor(Doctor d) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("id", d.id);
cv.put("fname", d.firstName);
cv.put("lname", d.lastName);
cv.put("mobile", d.mobile);
cv.put("address", d.address);
cv.put("city", d.city);
cv.put("specialization", d.specialization);
db.insert("doctor", null, cv);
db.close();
}
public ArrayList getAllDoctors() {
ArrayList list = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM doctor", null);
if (c.moveToFirst()) {
do {
Doctor d = new Doctor(
c.getInt(0),
c.getString(1),
c.getString(2),
c.getString(3),
c.getString(4),
c.getString(5),
c.getString(6)
);
list.add(d);
} while (c.moveToNext());
}
c.close();
db.close();
return list;
}
}
Doctor.java
package com.tecvipul.doctorinfo;
public class Doctor {
int id;
String firstName, lastName, mobile, address, city, specialization;
public Doctor(int id, String firstName, String lastName, String mobile, String address, String city, String specialization) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.mobile = mobile;
this.address = address;
this.city = city;
this.specialization = specialization;
}
@Override
public String toString() {
return id + ": " + firstName + " " + lastName + " - " + specialization;
}
}
DoctorListActivity
package com.tecvipul.doctorinfo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class DoctorListActivity extends AppCompatActivity {
ListView listView;
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_list);
listView = findViewById(R.id.listView);
db = new DBHelper(this);
ArrayList doctors = db.getAllDoctors();
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, doctors);
listView.setAdapter(adapter);
}
}
0 Comments
If you have any doubts, Please let me know