To Do List
TO DO LIST
<color name="colorPrimary">#0b6c7e</color>
<color name="colorPrimaryDark">#11262a</color>
<color name="colorAccent">#03DAC5</color>
activity_main.xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/img_add"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/add_button_shape"
android:elevation="9dp"
android:src="@drawable/ic_plus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
Create Vector Assets in Drawable for creating Add Button Name it ic_plus
Create new resource file name it add_button_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorPrimary"/>
<corners android:bottomLeftRadius="90dp" android:topLeftRadius="90dp" android:topRightRadius="90dp"/>
</shape>
Create New resource file, background_gradient.xml and add it to background of main activity
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="@color/colorPrimaryDark"
android:centerColor="@color/colorPrimary"
android:endColor="@color/colorPrimaryDark"
android:angle="45"/>
</shape>
Create New Layout Name it note_holder.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:elevation="9dp"
app:cardCornerRadius="9dp"
android:layout_marginHorizontal="14dp"
android:layout_marginTop="14dp"
android:transitionName="expanding_transition"
app:cardBackgroundColor="@color/colorPrimary"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_note_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/txt_note_description"
tools:text="Title" />
<TextView
android:id="@+id/txt_note_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginBottom="28dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/white"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/img_edit"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
tools:text="Some long long description" />
<ImageView
android:id="@+id/img_edit"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_edit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Create Vector Assets in Drawable for creating edit Button Name it ic_edit
Create a Class NoteAdpater.java
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
ArrayList<Note> notes;
Context context;
ItemClicked itemClicked;
ViewGroup parent;
private int lastSelectedPosition = -1;
public NoteAdapter(ArrayList<Note> arrayList, Context context, ItemClicked itemClicked){
notes = arrayList;
this.context = context;
this.itemClicked = itemClicked;
}
@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.note_holder,parent,false);
this.parent = parent;
return new NoteHolder(view);
}
@Override
public void onBindViewHolder(@NonNull NoteHolder holder, int position) {
if (position == lastSelectedPosition){
holder.title.setTextColor(Color.parseColor("#000000"));
}else {
holder.title.setTextColor(Color.parseColor("#FFFFFF"));
}
holder.title.setText(notes.get(position).getTitle());
holder.description.setText(notes.get(position).getDescription());
}
@Override
public int getItemCount() {
return notes.size();
}
class NoteHolder extends RecyclerView.ViewHolder{
TextView title;
TextView description;
ImageView imgEdit;
public NoteHolder(@NonNull final View itemView) {
super(itemView);
title = itemView.findViewById(R.id.txt_note_name);
description = itemView.findViewById(R.id.txt_note_description);
imgEdit = itemView.findViewById(R.id.img_edit);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
title.setTextColor(Color.parseColor("#000000"));
lastSelectedPosition = getAdapterPosition();
if (description.getMaxLines() == 1){
description.setMaxLines(Integer.MAX_VALUE);
}else {
description.setMaxLines(1);
}
notifyDataSetChanged();
TransitionManager.beginDelayedTransition(parent);
}
});
imgEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClicked.onClick(getAdapterPosition(),itemView);
}
});
}
}
interface ItemClicked{
void onClick(int postion, View view);
}
}
Crete a Class Note for getter and Setter
public class Note {
private int id;
private String title;
private String description;
public Note(String title, String description) {
this.title = title;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Create a New Class DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "NotesDatabase";
public DatabaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sqlQuery = "CREATE TABLE Note ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, description TEXT)";
sqLiteDatabase.execSQL(sqlQuery);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
String sqlQuery = "DROP TABLE IF EXISTS Note";
sqLiteDatabase.execSQL(sqlQuery);
onCreate(sqLiteDatabase);
}
}
Create a Class NoteHandle and extend DatabaseHelper
public class NoteHandler extends DatabaseHelper {
public NoteHandler(Context context) {
super(context);
}
//CRUD C create, R read, U update, D delete
public boolean create(Note note) {
ContentValues values = new ContentValues();
values.put("title", note.getTitle());
values.put("description", note.getDescription());
SQLiteDatabase db = this.getWritableDatabase();
boolean isSuccessfull = db.insert("Note", null, values) > 0;
db.close();
return isSuccessfull;
}
public ArrayList<Note> readNotes() {
ArrayList<Note> notes = new ArrayList<>();
String sqlQuery = "SELECT * FROM Note ORDER BY id ASC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(sqlQuery, null);
if (cursor.moveToFirst()) {
do {
int id = Integer.parseInt(cursor.getString(cursor.getColumnIndex("id")));
String title = cursor.getString(cursor.getColumnIndex("title"));
String description = cursor.getString(cursor.getColumnIndex("description"));
Note note = new Note(title, description);
note.setId(id);
notes.add(note);
} while (cursor.moveToNext());
cursor.close();
db.close();
}
return notes;
}
public Note readSingleNote(int id) {
Note note = null;
String sqlQuery = "SELECT * FROM Note WHERE id=" + id;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(sqlQuery, null);
if (cursor.moveToFirst()) {
int noteId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("id")));
String title = cursor.getString(cursor.getColumnIndex("title"));
String description = cursor.getString(cursor.getColumnIndex("description"));
note = new Note(title, description);
note.setId(noteId);
}
cursor.close();
db.close();
return note;
}
public boolean update(Note note) {
ContentValues values = new ContentValues();
values.put("title", note.getTitle());
values.put("description", note.getDescription());
SQLiteDatabase db = this.getWritableDatabase();
boolean isSuccessfull = db.update("Note", values, "id='" + note.getId() + "'", null) > 0;
db.close();
return isSuccessfull;
}
public boolean delete(int id) {
boolean isDeleted;
SQLiteDatabase db = this.getWritableDatabase();
isDeleted = db.delete("Note", "id='" + id + "'", null) > 0;
db.close();
return isDeleted;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
ArrayList<Note> notes;
RecyclerView recyclerView;
NoteAdapter noteAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = findViewById(R.id.img_add);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewInput = inflater.inflate(R.layout.note_input, null, false);
final EditText edtTitle = viewInput.findViewById(R.id.edt_title);
final EditText edtDesription = viewInput.findViewById(R.id.edt_description);
new AlertDialog.Builder(MainActivity.this)
.setView(viewInput)
.setTitle("Add note")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String title = edtTitle.getText().toString();
String description = edtDesription.getText().toString();
Note note = new Note(title, description);
boolean isInserted = new NoteHandler(MainActivity.this).create(note);
if (isInserted) {
Toast.makeText(MainActivity.this, "Note saved", Toast.LENGTH_SHORT).show();
loadNotes();
} else {
Toast.makeText(MainActivity.this, "Unable to save the note", Toast.LENGTH_SHORT).show();
}
dialogInterface.cancel();
}
}).show();
}
});
recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
ItemTouchHelper.SimpleCallback itemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
new NoteHandler(MainActivity.this).delete(notes.get(viewHolder.getAdapterPosition()).getId());
notes.remove(viewHolder.getAdapterPosition());
noteAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
loadNotes();
}
public ArrayList<Note> readNotes() {
ArrayList<Note> notes = new NoteHandler(this).readNotes();
return notes;
}
public void loadNotes() {
notes = readNotes();
noteAdapter = new NoteAdapter(notes, this, new NoteAdapter.ItemClicked() {
@Override
public void onClick(int postion, View view) {
editNote(notes.get(postion).getId(), view);
}
});
recyclerView.setAdapter(noteAdapter);
}
private void editNote(int noteId, View view) {
NoteHandler noteHandler = new NoteHandler(this);
Note note = noteHandler.readSingleNote(noteId);
Intent intent = new Intent(this, EditNote.class);
intent.putExtra("title", note.getTitle());
intent.putExtra("description", note.getDescription());
intent.putExtra("id", note.getId());
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(this, view, ViewCompat.getTransitionName(view));
startActivityForResult(intent, 1, optionsCompat.toBundle());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
loadNotes();
}
}
}
Create a Layout note_input
<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">
<EditText
android:id="@+id/edt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Note description"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_title" />
<EditText
android:id="@+id/edt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Note title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Create a New Activity - EditNote
activity_editnote.xml
<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"
android:background="@drawable/background_gradient"
tools:context=".EditNote">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginHorizontal="10dp"
android:elevation="9dp"
app:cardCornerRadius="9dp"
app:cardBackgroundColor="@color/colorPrimary"
android:transitionName="expanding_transition"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="10dp" >
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt_edit_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Title" />
<EditText
android:id="@+id/edt_edit_descrption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_edit_title"
tools:text="Description" />
<LinearLayout
android:id="@+id/btn_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="15dp"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="@drawable/button_shape"
android:text="Save"
android:textAllCaps="false"
android:textColor="@android:color/white" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="@drawable/button_shape"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="@android:color/white" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
create new resource file button_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="12dp"/>
<solid android:color="@color/colorPrimaryDark"/>
</shape>
EditNote.java
public class EditNote extends AppCompatActivity {
EditText edtTitle, edtDescription;
Button btnCancel, btnSave;
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_note);
final Intent intent = getIntent();
linearLayout = findViewById(R.id.btn_holder);
edtDescription = findViewById(R.id.edt_edit_descrption);
edtTitle = findViewById(R.id.edt_edit_title);
btnCancel = findViewById(R.id.btnCancel);
btnSave = findViewById(R.id.btnSave);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Note note = new Note(edtTitle.getText().toString(), edtDescription.getText().toString());
note.setId(intent.getIntExtra("id",1));
if (new NoteHandler(EditNote.this).update(note)){
Toast.makeText(EditNote.this, "Note updated", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(EditNote.this, "Failed updating", Toast.LENGTH_SHORT).show();
}
onBackPressed();
}
});
edtDescription.setText(intent.getStringExtra("description"));
edtTitle.setText(intent.getStringExtra("title"));
}
@Override
public void onBackPressed() {
btnSave.setVisibility(View.GONE);
btnCancel.setVisibility(View.GONE);
TransitionManager.beginDelayedTransition(linearLayout);
super.onBackPressed();
}
}
Comments
Post a Comment