Last active
March 4, 2024 01:49
-
-
Save aldiskatel/f79377dbae6fdafdf40453f1b34acca9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<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" | |
tools:context=".MainActivity"> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/rvTodoItems" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
app:layout_constraintBottom_toTopOf="@+id/btnAddTodo" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<EditText | |
android:id="@+id/etTodoTitle" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:hint="Enter Todo Title" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toStartOf="@+id/btnAddTodo" | |
app:layout_constraintStart_toStartOf="parent" /> | |
<Button | |
android:id="@+id/btnAddTodo" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Add Todo" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toStartOf="@+id/btnDeleteDoneTodo" /> | |
<Button | |
android:id="@+id/btnDeleteDoneTodo" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Delete Done" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'com.android.application' | |
id 'kotlin-android' | |
id 'kotlin-android-extensions' | |
} | |
android { | |
compileSdkVersion 30 | |
buildToolsVersion "30.0.3" | |
defaultConfig { | |
applicationId "com.example.todolist" | |
minSdkVersion 21 | |
targetSdkVersion 30 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
compileOptions { | |
sourceCompatibility JavaVersion.VERSION_1_8 | |
targetCompatibility JavaVersion.VERSION_1_8 | |
} | |
kotlinOptions { | |
jvmTarget = '1.8' | |
} | |
} | |
dependencies { | |
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | |
implementation 'androidx.core:core-ktx:1.3.1' | |
implementation 'androidx.appcompat:appcompat:1.2.0' | |
implementation 'com.google.android.material:material:1.2.1' | |
implementation 'androidx.constraintlayout:constraintlayout:2.0.1' | |
testImplementation 'junit:junit:4.+' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.2' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | |
implementation 'com.google.android.material:material:1.3.0-alpha02' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="80dp" | |
android:paddingStart="8dp" | |
android:paddingEnd="8dp"> | |
<TextView | |
android:id="@+id/tvTodoTitle" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:text="Example" | |
android:textSize="24sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toStartOf="@+id/cbDone" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<CheckBox | |
android:id="@+id/cbDone" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.todolist | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
private lateinit var todoAdapter : TodoAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
todoAdapter = TodoAdapter(mutableListOf()) | |
rvTodoItems.adapter = todoAdapter | |
rvTodoItems.layoutManager = LinearLayoutManager(this) | |
btnAddTodo.setOnClickListener { | |
val todoTitle = etTodoTitle.text.toString() | |
if(todoTitle.isNotEmpty()) { | |
val todo = Todo(todoTitle) | |
todoAdapter.addTodo(todo) | |
etTodoTitle.text.clear() | |
} | |
} | |
btnDeleteDoneTodo.setOnClickListener { | |
todoAdapter.deleteDoneTodos() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.todolist | |
data class Todo ( | |
val title: String, | |
var isChecked: Boolean = false | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.todolist | |
import android.graphics.Paint.STRIKE_THRU_TEXT_FLAG | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.TextView | |
import androidx.recyclerview.widget.RecyclerView | |
import kotlinx.android.synthetic.main.activity_main.view.* | |
import kotlinx.android.synthetic.main.item_todo.view.* | |
class TodoAdapter( | |
private val todos: MutableList<Todo> | |
) : RecyclerView.Adapter<TodoAdapter.TodoViewHolder>() { | |
class TodoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder { | |
return TodoViewHolder( | |
LayoutInflater.from(parent.context).inflate( | |
R.layout.item_todo, | |
parent, | |
false | |
) | |
) | |
} | |
private fun toggleStrikeThrough(tvTodoTitle: TextView, isChecked: Boolean) { | |
if(isChecked) { | |
tvTodoTitle.paintFlags = tvTodoTitle.paintFlags or STRIKE_THRU_TEXT_FLAG | |
} else { | |
tvTodoTitle.paintFlags = tvTodoTitle.paintFlags and STRIKE_THRU_TEXT_FLAG.inv() | |
} | |
} | |
fun addTodo(todo: Todo) { | |
todos.add(todo) | |
notifyItemInserted(todos.size -1) | |
} | |
fun deleteDoneTodos() { | |
todos.removeAll { todo -> | |
todo.isChecked | |
} | |
notifyDataSetChanged() | |
} | |
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) { | |
val curTodo = todos[position] | |
holder.itemView.apply { | |
tvTodoTitle.text = curTodo.title | |
cbDone.isChecked = curTodo.isChecked | |
toggleStrikeThrough(tvTodoTitle, curTodo.isChecked) | |
cbDone.setOnCheckedChangeListener { _, isChecked -> | |
toggleStrikeThrough(tvTodoTitle, isChecked) | |
curTodo.isChecked = !curTodo.isChecked | |
} | |
} | |
} | |
override fun getItemCount(): Int { | |
return todos.size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Waww