Capstone Project
Capstone Project
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PackYourBag"
tools:targetApi="31">
<activity
android:name=".CheckList"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.packyourbag.CheckList;
import com.example.packyourbag.Constants.MyConstants;
import com.example.packyourbag.R;
import java.util.List;
List<String> titles;
List<Integer> images;
LayoutInflater inflater;
Activity activity;
public Adapter(Context context, List<String> titles , List<Integer> images , Activity activity)
{
this.titles=titles;
this.images= images;
this.activity = activity;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
holder.title.setText(titles.get(position));
holder.img.setImageResource(images.get(position));
holder.linearLayout.setAlpha(0.8F);
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(activity, "Clciked on card", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(), CheckList.class);
intent.putExtra(MyConstants.HEADER_SMALL,titles.get(position));
if(MyConstants.MY_SELECTIONS.equals(titles.get(position))) {
intent.putExtra(MyConstants.SHOW_SMALL,MyConstants.FALSE_STRING);
}else{
intent.putExtra(MyConstants.SHOW_SMALL,MyConstants.TRUE_STRING);
}
view.getContext().startActivity(intent);
}
});
@Override
public int getItemCount() {
return titles.size();
}
TextView title;
ImageView img;
LinearLayout linearLayout;
}
[10/04, 7:53 am] .: adapter
[10/04, 7:54 am] .: package com.example.packyourbag.Adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.view.ActionBarPolicy;
import androidx.recyclerview.widget.RecyclerView;
import com.example.packyourbag.Constants.MyConstants;
import com.example.packyourbag.Database.RoomDB;
import com.example.packyourbag.Models.Items;
import com.example.packyourbag.R;
import java.util.List;
Context context;
List<Items> itemsList;
RoomDB database;
String show;
public CheckListAdapter(){
}
public CheckListAdapter(Context context ,List<Items> itemsList , RoomDB database ,
String show){
this.context=context;
this.itemsList=itemsList;
this.database=database;
this.show=show;
if(itemsList.size()==0)
Toast.makeText(context.getApplicationContext(), "Nothing to show",
Toast.LENGTH_SHORT).show();
@NonNull
@Override
public CheckListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
return new
CheckListViewHolder(LayoutInflater.from(context).inflate(R.layout.check_list_item,parent,f
alse));
}
@Override
public void onBindViewHolder(@NonNull CheckListViewHolder holder, int position) {
holder.checkBox.setText(itemsList.get(position).getItemname());
holder.checkBox.setChecked(itemsList.get(position).getChecked());
if(MyConstants.FALSE_STRING.equals(show)){
holder.btnDelete.setVisibility(View.GONE);
holder.layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bord
er_one));
}else{
if(itemsList.get(position).getChecked()){
holder.layout.setBackgroundColor(Color.parseColor("#8e546f"));
}else {
holder.layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bord
er_one));
}
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Boolean check = holder.checkBox.isChecked();
ActionBarPolicy itemList;
database.mainDao().checkUncheck(itemsList.get(position).getID(),check);
if(MyConstants.FALSE_STRING.equals(show)){
itemsList= database.mainDao().getAllSelected(true);
notifyDataSetChanged();
}
else{
itemsList.get(position).setChecked(check);
notifyDataSetChanged();
Toast tostMessage = null;
if(tostMessage!=null){
tostMessage.cancel();
}
if(itemsList.get(position).getChecked()){
tostMessage=
Toast.makeText(context,"("+holder.checkBox.getText()+")packed",Toast.LENGTH_SHORT)
;
}else{
tostMessage = Toast.makeText(context,"("+holder.checkBox.getText()+")
Un-Packed",Toast.LENGTH_SHORT);
}
tostMessage.show();
}
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(context)
.setTitle("Delete(" + itemsList.get(position).getItemname() + ")")
.setMessage("Are you sure")
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
database.mainDao().delete(itemsList.get(position));
itemsList.remove(itemsList.get(position));
notifyDataSetChanged();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}
@Override
public int getItemCount() {
return itemsList.size();
}
}
class CheckListViewHolder extends RecyclerView.ViewHolder{
LinearLayout layout;
CheckBox checkBox;
Button btnDelete;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.view.ActionBarPolicy;
import androidx.recyclerview.widget.RecyclerView;
import com.example.packyourbag.Constants.MyConstants;
import com.example.packyourbag.Database.RoomDB;
import com.example.packyourbag.Models.Items;
import com.example.packyourbag.R;
import java.util.List;
Context context;
List<Items> itemsList;
RoomDB database;
String show;
public CheckListAdapter(){
}
public CheckListAdapter(Context context ,List<Items> itemsList , RoomDB database ,
String show){
this.context=context;
this.itemsList=itemsList;
this.database=database;
this.show=show;
if(itemsList.size()==0)
Toast.makeText(context.getApplicationContext(), "Nothing to show",
Toast.LENGTH_SHORT).show();
}
@NonNull
@Override
public CheckListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
return new
CheckListViewHolder(LayoutInflater.from(context).inflate(R.layout.check_list_item,parent,f
alse));
}
@Override
public void onBindViewHolder(@NonNull CheckListViewHolder holder, int position) {
holder.checkBox.setText(itemsList.get(position).getItemname());
holder.checkBox.setChecked(itemsList.get(position).getChecked());
if(MyConstants.FALSE_STRING.equals(show)){
holder.btnDelete.setVisibility(View.GONE);
holder.layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bord
er_one));
}else{
if(itemsList.get(position).getChecked()){
holder.layout.setBackgroundColor(Color.parseColor("#8e546f"));
}else {
holder.layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bord
er_one));
}
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Boolean check = holder.checkBox.isChecked();
ActionBarPolicy itemList;
database.mainDao().checkUncheck(itemsList.get(position).getID(),check);
if(MyConstants.FALSE_STRING.equals(show)){
itemsList= database.mainDao().getAllSelected(true);
notifyDataSetChanged();
}
else{
itemsList.get(position).setChecked(check);
notifyDataSetChanged();
Toast tostMessage = null;
if(tostMessage!=null){
tostMessage.cancel();
}
if(itemsList.get(position).getChecked()){
tostMessage=
Toast.makeText(context,"("+holder.checkBox.getText()+")packed",Toast.LENGTH_SHORT)
;
}else{
tostMessage = Toast.makeText(context,"("+holder.checkBox.getText()+")
Un-Packed",Toast.LENGTH_SHORT);
}
tostMessage.show();
}
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(context)
.setTitle("Delete(" + itemsList.get(position).getItemname() + ")")
.setMessage("Are you sure")
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
database.mainDao().delete(itemsList.get(position));
itemsList.remove(itemsList.get(position));
notifyDataSetChanged();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}
@Override
public int getItemCount() {
return itemsList.size();
}
}
class CheckListViewHolder extends RecyclerView.ViewHolder{
LinearLayout layout;
CheckBox checkBox;
Button btnDelete;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import com.example.packyourbag.Models.Items;
import java.util.List;
@Dao
public interface ItemsDao {
@Insert(onConflict = REPLACE)
void saveItems(Items items);
@Delete
void delete(Items items);
AppData
import android.app.Application;
import android.content.Context;
import com.example.packyourbag.Constants.MyConstants;
import com.example.packyourbag.Database.RoomDB;
import com.example.packyourbag.Models.Items;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
RoomDB database;
String category;
Context context;
return basicItem;
}
}
public List<Items> getTechnologyData(){
String [] data = {"Smartphone", "Laptop", "Tablet", "Portable charger", "Travel adapter",
"Headphones", "Camera", "Power bank", "External hard drive", "Travel router", "Noise-
canceling headphones", "GPS device", "E-book reader", "Smartwatch", "Fitness tracker",
"Bluetooth earbuds", "Universal charging cable", "Travel-sized keyboard", "USB flash drive",
"Camera accessories (tripod, lens, etc.)", "Portable Wi-Fi hotspot", "Travel-sized tech
organizer", "Travel-friendly laptop stand", "Travel surge protector"};
return prePareItemsList(MyConstants.TECHNOLOGY_CAMEL_CASE,data);
}
for(int i=0;i<list.size();i++){
dataList.add(new Items(list.get(i),category,false));
}
return dataList;
}
roomdb
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import com.example.packyourbag.Dao.ItemsDao;
import com.example.packyourbag.Models.Items;
database = Room.databaseBuilder(context.getApplicationContext(),
RoomDB.class,DATABASE_NAME).allowMainThreadQueries()
.fallbackToDestructiveMigration()
.build();
}
return database;
}
item.java
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.io.Serializable;
@Entity(tableName="items")
public class Items implements Serializable {
@PrimaryKey(autoGenerate = true)
int ID = 0;
@ColumnInfo(name ="itemname")
String itemname;
@ColumnInfo(name = "category")
String category;
@ColumnInfo(name ="addedby")
public String addedby;
@ColumnInfo(name="checked")
Boolean checked = false;
public Items() {
}
public Items(String itemname , String category ,Boolean checked){
this.addedby="system";
this.itemname=itemname;
this.category=category;
this.checked=checked;
}
checklist.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.room.Room;
import android.annotation.SuppressLint;
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.LinearLayout;
import android.widget.Toast;
import com.example.packyourbag.Adapter.CheckListAdapter;
import com.example.packyourbag.Constants.MyConstants;
import com.example.packyourbag.Database.RoomDB;
import com.example.packyourbag.Models.Items;
import java.util.ArrayList;
import java.util.List;
RecyclerView recyclerView;
CheckListAdapter checkListAdapter;
RoomDB database;
List<Items> itemsList = new ArrayList<>();
String header,show;
EditText textAdd;
Button btnAdd;
LinearLayout linearLayout;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_list);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
header = intent.getStringExtra(MyConstants.HEADER_SMALL);
show = intent.getStringExtra(MyConstants.SHOW_SMALL);
getSupportActionBar().setTitle(header);
textAdd = findViewById(R.id.txtAdd);
btnAdd= findViewById(R.id.btnAdd);
recyclerView = findViewById(R.id.recyclerView);
linearLayout= findViewById(R.id.linearLayout);
database = RoomDB.getInstance(this);
if(MyConstants.FALSE_STRING.equals(show)){
linearLayout.setVisibility(View.GONE);
itemsList=database.mainDao().getAllSelected(true);
}else {
itemsList=database.mainDao().getAll(header);
}
updateRecycler(itemsList);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String itemName= textAdd.getText().toString();
if(itemName!=null && !itemName.isEmpty()){
addNewItem(itemName);
Toast.makeText(CheckList.this,"Item added",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(CheckList.this,"Empty cant be
added",Toast.LENGTH_SHORT).show();
}
}
});
item.setAddedby(MyConstants.USER_SMALL);
database.mainDao().saveItems(item);
itemsList = database.mainDao().getAll(header);
updateRecycler(itemsList);
recyclerView.scrollToPosition(checkListAdapter.getItemCount()-1);
textAdd.setText("");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,
LinearLayoutManager.VERTICAL));
checkListAdapter= new CheckListAdapter(CheckList.this,itemsList,database,show);
recyclerView.setAdapter((checkListAdapter));
}
}
main.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;
import com.example.packyourbag.Adapter.Adapter;
import com.example.packyourbag.Constants.MyConstants;
import com.example.packyourbag.Data.AppData;
import com.example.packyourbag.Database.RoomDB;
import java.util.ArrayList;
import java.util.List;
RecyclerView recyclerView;
List<String> titles;
List<Integer> images;
Adapter adapter;
RoomDB database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
recyclerView = findViewById(R.id.recyclerView);
addAddTitles();
addAllImages();
persistAppData();
database = RoomDB.getInstance(this);
System.out.println("--------------------------------->"
+database.mainDao().getAllSelected(false).get(0).getItemname());
adapter = new Adapter(this,titles,images,MainActivity.this);
GridLayoutManager gridLayoutManager =new
GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
}
private static final int TIME_INTERVAL = 2000;
@Override
public void onBackPressed() {
if(mBackPressed+TIME_INTERVAL>System.currentTimeMillis()){
super.onBackPressed();
return;
}else {
mBackPressed =System.currentTimeMillis();
database = RoomDB.getInstance(this);
AppData appData = new AppData(database);
int last = prefs.getInt(AppData.LAST_VERSION,0);
if(!prefs.getBoolean(MyConstants.FIRST_TIME_CAMEL_CASE,false)) {
appData.persistAllData();
editor.putBoolean(MyConstants.FIRST_TIME_CAMEL_CASE,true);
editor.commit();
}else if (last<AppData.NEW_VERSION){
database.mainDao().deleteAllSystemItems(MyConstants.SYSTEM_SMALL);
appData.persistAllData();
editor.putInt(AppData.LAST_VERSION,AppData.NEW_VERSION);
editor.commit();
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:background="@drawable/background3"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@drawable/wave2"
android:id="@+id/title_view"
android:textAlignment="center"
android:text="PACK YOUR BAG"
android:textSize="35sp"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:textColor="@color/white"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:layout_below="@+id/title_view"
/>
</RelativeLayout>
mainitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:gravity="center"
android:background="#E3ACAC"
android:padding="10dp"
android:layout_gravity="center_vertical|center_horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/img"
android:src="@drawable/shopping_bag"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:text="Basic Needs"
android:textSize="20sp"
android:textColor="@color/black"
android:textStyle="bold"
android:textAlignment="center"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:padding="10dp"
android:textAlignment="center"
android:id="@+id/linearLayout"
android:orientation="horizontal">
<CheckBox
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:id="@+id/checkbox"
android:layout_gravity="center"
android:textSize="20dp"
android:text="This is just for test"
/>
<Button
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:layout_gravity="center"
android:background="@drawable/delete_white"
/>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E3ACAC"
tools:context=".CheckList">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:layout_marginBottom="60dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:id="@+id/linearLayout"
android:background="#FFD6D6">
<EditText
android:id="@+id/txtAdd"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:hint="Enter item to add"
android:textSize="22sp"
android:layout_marginLeft="8dp"
android:textStyle="bold"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="0dp"
android:layout_weight="0.7"
android:layout_height="wrap_content"
android:background="@drawable/ic_circle"
android:layout_marginEnd="8dp"
/>
</LinearLayout>
</RelativeLayout>