Kotlin Code Finals
Kotlin Code Finals
XML
<TextView
android:id="@+id/tasksText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Tasks"
android:textColor="@android:color/black"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:textSize="32sp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tasksRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tasksText"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:backgroundTint="@android:color/holo_green_dark"
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="32dp"
android:backgroundTint="@android:color/holo_green_dark"
android:src="@drawable/ic_baseline_add"/>
</RelativeLayout>
TASK_LAYOUT.XML
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:tool="http://schemas.android.com/tools"
app:cardElevation="4dp"
app:cardCornerRadius="8dp"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="8dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp">
<CheckBox
android:id="@+id/todoCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tool:text="This is an Sample Task inside the application."
android:paddingStart="8dp"
android:buttonTint="@android:color/holo_green_dark"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
NEW_TASK.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/newTaskText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="New Task"
android:textappearance="@style/TextAppearance.AppCompat.Medium" />
<Button
android:id="@+id/newTaskButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/newTaskButton"
android:layout_alignParentEnd="true"
android:textSize="16sp"
android:text="Save"
android:textAllCaps="false"
android:background="@android:color/transparent"
android:textColor="@android:color/darker_gray"/>
</RelativeLayout>
ACTIVITY_CODE.XML
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src"@drawable/icon"
android:layout_centerInParent="true"/>
</RelativeLayout>
CODEACTIVITY.JAVA ( new, activity, new activity)
package com.example.doit;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_code);
getSupportActionBar().hide();
package com.example.doit;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import com.example.doit.Adapter.ToDoAdapter;
import com.example.doit.Model.ToDoModel;
import com.example.doit.Utils.DatabaseHandler;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
db = new DatabaseHandler(this);
db.openDatabase();
tasksRecyclerView = findViewById(R.id.tasksRecyclerView);
tasksRecyclerView.setLayoutManager(new LinearLayoutManager(this));
tasksAdapter = new ToDoAdapter(db,this);
tasksRecyclerView.setAdapter(tasksAdapter);
fab = findViewById(R.id.fab);
taskList = db.getAllTasks();
Collections.reverse(taskList);
tasksAdapter.setTasks(taskList);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AddNewTask.newInstance().show(getSupportFragmentManager(),AddNewTask.TAG);
}
});
}
@Override
public void handleDialogClose(DialogInterface dialog){
taskList = db.getAllTasks();
Collections.reverse(taskList);
tasksAdapter.setTasks(taskList);
tasksAdapter.notifyDataSetChanged();
}
}
ToDoAdapter.java (com.dsakdg right click, new, package, adapter, javaclass, todoadapter)
package com.example.doit.Adapter;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import androidx.recyclerview.widget.RecyclerView;
import com.example.doit.AddNewTask;
import com.example.doit.MainActivity;
import com.example.doit.Model.ToDoModel;
import com.example.doit.R;
import com.example.doit.Utils.DatabaseHandler;
import java.util.List;
ViewHolder(View view){
super(view);
task = view.findViewById(R.id.todoCheckBox);
}
}
}
ToDoModel.java (model right click, new, java class, todomodel)
package com.example.doit.Model;
package com.example.doit.Utils;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.doit.Model.ToDoModel;
import java.util.ArrayList;
import java.util.List;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TODO_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
//Drop the older tables
db.execSQL("DROP TABLE IF EXISTS" + TODO_TABLE);
//Create tables again
onCreate(db);
}
@SuppressLint("Range")
public List<ToDoModel> getAllTasks(){
List<ToDoModel> taskList = new ArrayList<>();
Cursor cur = null;
db.beginTransaction();
try {
cur = db.query(TODO_TABLE, null, null, null, null, null, null, null);
if (cur != null) {
if (cur.moveToFirst()) {
do {
ToDoModel task = new ToDoModel();
task.setId(cur.getInt(cur.getColumnIndex(ID)));
task.setTask(cur.getString(cur.getColumnIndex(TASK)));
task.setStatus(cur.getInt(cur.getColumnIndex(STATUS)));
taskList.add(task);
} while (cur.moveToNext());
}
}
}
finally{
db.endTransaction();
cur.close();
}
return taskList;
}
package com.example.doit;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.example.doit.Adapter.ToDoAdapter;
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target){
return false;
}
@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction){
final int position = viewHolder.getAdapterPosition();
if(direction == ItemTouchHelper.LEFT){
AlertDialog.Builder builder = new AlertDialog.Builder(adapter.getContext());
builder.setTitle("Delete Task");
builder.setMessage("Are you sure you want to delete this Task?");
builder.setPositiveButton("Confirm",
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
adapter.deleteItem(position);
}
});
builder.setNegativeButton(android.R.string.cancel, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
adapter.notifyItemChanged(viewHolder.getAdapterPosition());
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else{
adapter.editItem(position);
}
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder
viewHolder, float dX, float dY, int actionState, boolean isCurretlyActive){
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurretlyActive);
Drawable icon;
ColorDrawable background;
if(dX>0) {
icon = ContextCompat.getDrawable(adapter.getContext(), R.drawable.ic_baseline_edit);
background = new ColorDrawable(ContextCompat.getColor(adapter.getContext(),
R.color.colorPrimaryDark));
} else{
icon = ContextCompat.getDrawable(adapter.getContext(),
R.drawable.ic_baseline_delete);
background = new ColorDrawable(Color.RED);
}
background.setBounds(itemView.getLeft(), itemView.getTop(),
itemView.getLeft() + ((int)dX) + backgroundCornerOffset, itemView.getBottom());
}
else if(dX<0){
int iconLeft = itemView.getRight() - iconMargin - icon.getIntrinsicWidth();
int iconRight = itemView.getRight() - iconMargin;
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);
package com.example.doit;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.core.content.ContextCompat;
import com.example.doit.Model.ToDoModel;
import com.example.doit.Utils.DatabaseHandler;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, R.style.DialogStyle);
}
@Override
public View onCreateView(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
newTaskText = getView().findViewById(R.id.newTaskText);
newTaskSaveButton = getView().findViewById(R.id.newTaskButton);
db = new DatabaseHandler(getActivity());
db.openDatabase();
newTaskSaveButton.setTextColor(ContextCompat.getColor(getContext(),R.color.colorPrimaryD
ark));
}
newTaskText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().equals("")){
newTaskSaveButton.setEnabled(false);
newTaskSaveButton.setTextColor(Color.GRAY);
}
else{
newTaskSaveButton.setEnabled(true);
newTaskSaveButton.setTextColor(ContextCompat.getColor(getContext(),R.color.colorPrimaryD
ark));
}
@Override
public void afterTextChanged(Editable s) {
}
});
@Override
public void onDismiss(DialogInterface dialog) {
Activity activity = getActivity();
if (activity instanceof DialogCloseListener) {
((DialogCloseListner) activity).handlerDialogClose(dialog);
}
}
}
DialogCloseListener.java ( com.dasidga, new , java class, interface , dialogcloselistener)
package com.example.doit;
import android.content.DialogInterface;
IsUpdate.java
package com.example.doit;
<resources>
<item name=”colorPrimary”>@android:color/holo_green_dark</item>
<item name=”colorPrimaryDark”>@android:color/holo_green_dark</item>
<item name=”colorAccent”>@android:colorwhite</item>
</style>
</resources>