Created
October 5, 2019 10:59
-
-
Save bipinvaylu/62bf17f0175ac0f308b8600588031bff 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
class MainActivity : AppCompatActivity() { | |
internal var doItList: MutableList<String> = ArrayList() | |
internal var adapter: ArrayAdapter<String>? = null | |
internal var listView: ListView? = null | |
internal var emptyView: TextView? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(layout.activity_main) | |
val toolbar: Toolbar = findViewById(id.toolbar) | |
setSupportActionBar(toolbar) | |
listView = findViewById<ListView?>(id.listview) | |
emptyView = findViewById<TextView?>(id.empty_view) | |
adapter = ArrayAdapter(this, R.layout.simple_list_item_1, | |
doItList) | |
listView!!.adapter = adapter | |
val fab: FloatingActionButton = findViewById(id.fab) | |
fab.setOnClickListener { showAddDoItItem() } | |
} | |
override fun onResume() { | |
super.onResume() | |
refreshUI() | |
} | |
protected fun refreshUI() { | |
if (doItList.size > 0) { | |
listView!!.visibility = View.VISIBLE | |
emptyView!!.visibility = View.GONE | |
adapter!!.notifyDataSetChanged() | |
} else { | |
listView!!.visibility = View.GONE | |
emptyView!!.visibility = View.VISIBLE | |
} | |
} | |
protected 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, i -> dialogInterface.dismiss() } | |
.setPositiveButton("Add") { dialogInterface, i -> | |
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