0% found this document useful (0 votes)
9 views10 pages

Attendance System (Full Sourcecode)

Uploaded by

Ark Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Attendance System (Full Sourcecode)

Uploaded by

Ark Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ATTENDANCE SYSTEM [FULL SOURCECODE]

activity_main.xml 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

K
android:padding="16dp"
tools:context=".MainActivity">

R
<EditText

A
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:inputType="text"/>
Y
B
<EditText
android:id="@+id/rollNumberEditText"
TE

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameEditText"
android:layout_marginTop="16dp"
EA

android:hint="Enter Roll Number"


android:inputType="number"/>

<Button
R

android:id="@+id/markPresentButton"
android:layout_width="wrap_content"
C

android:layout_height="wrap_content"
android:layout_below="@id/rollNumberEditText"
android:layout_marginTop="16dp"
android:text="Mark Present"/>

<Button
android:id="@+id/markAbsentButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/markPresentButton"
android:layout_marginTop="16dp"
android:text="Mark Absent"/>

<TextView
android:id="@+id/attendanceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

K
android:layout_below="@id/markAbsentButton"
android:layout_centerHorizontal="true"

R
android:layout_marginTop="16dp"
android:text="Attendance Status"
android:textSize="18sp"/>

A
<RecyclerView
android:id="@+id/attendanceRecyclerView"
Y
android:layout_width="match_parent"
B
android:layout_height="wrap_content"
android:layout_below="@id/attendanceTextView"
android:layout_marginTop="16dp"/>
TE

</RelativeLayout>
EA

MainActivity.java 
package com.project.attendancesystem;

import android.content.ContentValues;
R

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
C

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private EditText nameEditText;

K
private EditText rollNumberEditText;
private Button markPresentButton;

R
private Button markAbsentButton;
private TextView attendanceTextView;
private RecyclerView recyclerView;

A
private AttendanceAdapter attendanceAdapter;
private List<Attendance> attendanceList;
private SQLiteDatabase database; Y
B
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TE

setContentView(R.layout.activity_main);

nameEditText = findViewById(R.id.nameEditText);
rollNumberEditText = findViewById(R.id.rollNumberEditText);
EA

markPresentButton = findViewById(R.id.markPresentButton);
markAbsentButton = findViewById(R.id.markAbsentButton);
attendanceTextView = findViewById(R.id.attendanceTextView);
recyclerView = findViewById(R.id.attendanceRecyclerView);
R

// Initialize RecyclerView
C

recyclerView.setLayoutManager(new LinearLayoutManager(this));
attendanceList = new ArrayList<>();
attendanceAdapter = new AttendanceAdapter(this, attendanceList);
recyclerView.setAdapter(attendanceAdapter);

DatabaseHelper dbHelper = new DatabaseHelper(this);


database = dbHelper.getWritableDatabase();

markPresentButton.setOnClickListener(v -> markAttendance("Present"));


markAbsentButton.setOnClickListener(v -> markAttendance("Absent"));
loadAttendanceData();
}

private void markAttendance(String status) {

String currentDate = getCurrentDate();

K
String name = nameEditText.getText().toString().trim();
String rollNumber = rollNumberEditText.getText().toString().trim();

R
ContentValues values = new ContentValues();

A
values.put(DatabaseHelper.COLUMN_DATE, currentDate);
values.put(DatabaseHelper.COLUMN_NAME, name);
values.put(DatabaseHelper.COLUMN_ROLL_NUMBER, rollNumber);
Y
values.put(DatabaseHelper.COLUMN_STATUS, status);
B
long newRowId =
database.insert(DatabaseHelper.TABLE_ATTENDANCE, null, values);
TE

if (newRowId != -1) {
attendanceTextView.setText("Attendance Marked!");
Toast.makeText(this, "Attendance Marked!",
Toast.LENGTH_SHORT).show();
EA

loadAttendanceData(); // Reload attendance data after marking


} else {
Toast.makeText(this, "Error marking attendance",
Toast.LENGTH_SHORT).show();
R

}
}
C

private void loadAttendanceData() {


attendanceList.clear();
Cursor cursor = database.query(DatabaseHelper.TABLE_ATTENDANCE,
null, null, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
int idIndex = cursor.getColumnIndex(DatabaseHelper.COLUMN_ID);
int dateIndex =
cursor.getColumnIndex(DatabaseHelper.COLUMN_DATE);
int nameIndex =
cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME);
int rollNumberIndex =
cursor.getColumnIndex(DatabaseHelper.COLUMN_ROLL_NUMBER);
int statusIndex =
cursor.getColumnIndex(DatabaseHelper.COLUMN_STATUS);

K
if (idIndex == -1 || dateIndex == -1 || nameIndex == -1 ||
rollNumberIndex == -1 || statusIndex == -1) {

R
Log.e("LoadAttendanceData", "One or more columns not found in
cursor");

A
continue;
}
Y
B
int id = cursor.getInt(idIndex);
String date = cursor.getString(dateIndex);
String name = cursor.getString(nameIndex);
TE

String rollNumber = cursor.getString(rollNumberIndex);


String status = cursor.getString(statusIndex);
EA

attendanceList.add(new Attendance(id, date, name, rollNumber,


status));
}
cursor.close();
R

}
attendanceAdapter.notifyDataSetChanged();
C

private String getCurrentDate() {


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}

@Override
protected void onDestroy() {
super.onDestroy();
database.close();
}
}
Attendance.java 
package com.project.attendancesystem;;

public class Attendance {

K
private int id;
private String date;

R
private String name;
private String rollNumber;

A
private String status;

public Attendance(int id, String date, String name, String rollNumber, String
Y
status) {
this.id = id;
B
this.date = date;
this.name = name;
TE

this.rollNumber = rollNumber;
this.status = status;
}
EA

public int getId() {


return id;
}
R

public String getDate() {


return date;
C

public String getName() {


return name;
}

public String getRollNumber() {


return rollNumber;
}
public String getStatus() {
return status;
}
}

AttendanceAdapter.java 
package com.project.attendancesystem;

K
import android.content.Context;

R
import android.view.LayoutInflater;
import android.view.View;

A
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
Y
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
B
public class AttendanceAdapter extends
RecyclerView.Adapter<AttendanceAdapter.ViewHolder> {
TE

private Context context;


private List<Attendance> attendanceList;
EA

public AttendanceAdapter(Context context, List<Attendance>


attendanceList) {
this.context = context;
this.attendanceList = attendanceList;
R

}
C

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view =
LayoutInflater.from(context).inflate(R.layout.item_attendance, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
{
Attendance attendance = attendanceList.get(position);
holder.dateTextView.setText(attendance.getDate());
holder.statusTextView.setText(attendance.getStatus());
}

K
@Override
public int getItemCount() {

R
return attendanceList.size();
}

A
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView dateTextView;
TextView statusTextView; Y
B
public ViewHolder(@NonNull View itemView) {
super(itemView);
dateTextView = itemView.findViewById(R.id.dateTextView);
TE

statusTextView = itemView.findViewById(R.id.statusTextView);
}
}
}
EA

item_attendance.xml 
<?xml version="1.0" encoding="utf-8"?>
R

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
C

android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textSize="16sp"
android:textColor="@android:color/black"/>

<TextView
android:id="@+id/statusTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status"
android:textSize="14sp"

K
android:textColor="@android:color/black"/>
</LinearLayout>

R
DatabaseHelper.java 

A
package com.project.attendancesystem;

import android.content.Context; Y
import android.database.sqlite.SQLiteDatabase;
B
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {


TE

private static final String DATABASE_NAME = "attendance.db";


private static final int DATABASE_VERSION = 1;
EA

public static final String TABLE_ATTENDANCE = "attendance";


public static final String COLUMN_ID = "_id";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_NAME = "name";
R

public static final String COLUMN_ROLL_NUMBER = "roll_number";


public static final String COLUMN_STATUS = "status";
C

// Database creation SQL statement


private static final String DATABASE_CREATE = "create table "
+ TABLE_ATTENDANCE + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_DATE
+ " text not null, " + COLUMN_NAME
+ " text not null, " + COLUMN_ROLL_NUMBER
+ " text not null, " + COLUMN_STATUS
+ " text not null);";

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);

K
}

R
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{

A
// Not required for now, as it's the first version
}
} Y
B
TE
EA
R
C

You might also like