Created
October 5, 2019 12:24
-
-
Save bipinvaylu/16c5eca96919231b7b093906c5a95052 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
package dev.bipin.doit | |
import android.os.Bundle | |
import android.view.View | |
import android.widget.ArrayAdapter | |
import android.widget.EditText | |
import android.widget.LinearLayout.LayoutParams | |
import android.widget.ListView | |
import android.widget.TextView | |
import androidx.appcompat.app.AlertDialog | |
import androidx.appcompat.app.AlertDialog.Builder | |
import androidx.appcompat.app.AppCompatActivity | |
import com.google.android.material.floatingactionbutton.FloatingActionButton | |
class MainActivity : AppCompatActivity() { | |
private val doItList: MutableList<String> = ArrayList() | |
private val listItemAdapter: ArrayAdapter<String> by lazy { | |
ArrayAdapter(this, android.R.layout.simple_list_item_1, doItList) | |
} | |
private val listView: ListView by lazy { | |
findViewById<ListView>(R.id.listview) | |
} | |
private val emptyView: TextView by lazy { | |
findViewById<TextView>(R.id.empty_view) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setSupportActionBar(findViewById(R.id.toolbar)) | |
listView.apply { | |
this.adapter = listItemAdapter | |
} | |
findViewById<FloatingActionButton>(R.id.fab) | |
.setOnClickListener { showAddDoItItem() } | |
refreshUI() | |
} | |
private fun refreshUI() { | |
if (doItList.size > 0) { | |
listView.visibility = View.VISIBLE | |
emptyView.visibility = View.GONE | |
listItemAdapter.notifyDataSetChanged() | |
} else { | |
listView.visibility = View.GONE | |
emptyView.visibility = View.VISIBLE | |
} | |
} | |
private fun showAddDoItItem() { | |
val inputView = EditText(this) | |
inputView.hint = "Add Do Item Name" | |
val lp = LayoutParams( | |
LayoutParams.MATCH_PARENT, | |
LayoutParams.WRAP_CONTENT | |
) | |
lp.setMargins(10, 10, 10, 10) | |
inputView.layoutParams = lp | |
val alertDialog: AlertDialog = Builder(this) | |
.setTitle("Do It") | |
.setCancelable(true) | |
.setView(inputView) | |
.setNegativeButton("Cancel") { dialogInterface, _ -> dialogInterface.dismiss() } | |
.setPositiveButton("Add") { dialogInterface, _ -> | |
dialogInterface.dismiss() | |
doItList.add(inputView.text.toString()) | |
refreshUI() | |
} | |
.create() | |
alertDialog.show() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment