I have this code:
var firstKitList = mutableListOf<String>("test", "potato", "another item")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kit_list)
val mainKitList = kitListView
val mainListViewAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, firstKitList)
mainKitList.adapter = mainListViewAdapter
newKitListBtn.setOnClickListener {
// Handler code here.
val intent = Intent(this, NewKitListActivity::class.java)
startActivity(intent);
}
}
For this layout. How do I go around for when I click the button, to show up an alert dialog for me too add a name (as if I was creating an item to be added to that list) and then go to the next activity? (this part is already created as you can see on the code)
To set the action on alert dialog call the setPositiveButton(), setNeutralButton() and setNegativeButton() methods for positive, neutral and negative action respectively. The show() method of AlertDialog. Builder is used to display the alert dialog.
This example demonstrates how do I display a listView in an android aler dialog. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
There should be no more than three action buttons in a dialog.
I created function which hold alert dialog with editText. When your click on save name will be stored in multableList and redirected to new activity.
Modified Code
var firstKitList = mutableListOf<String>("test", "potato", "another item")
// Mutable List for holding names
val nameList = mutableListOf<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kit_list)
val mainKitList = kitListView
val mainListViewAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, firstKitList)
mainKitList.adapter = mainListViewAdapter
newKitListBtn.setOnClickListener {
// Show Alert Dialog
showNewNameDialog()
}
}
Alert Dialog Function
fun showNewNameDialog() {
val dialogBuilder = AlertDialog.Builder(this)
val inflater = this.layoutInflater
val dialogView = inflater.inflate(R.layout.custom_dialog, null)
dialogBuilder.setView(dialogView)
val editText = dialogView.findViewById<EditText>(R.id.editTextName)
dialogBuilder.setTitle("Custom dialog")
dialogBuilder.setMessage("Enter Name Below")
dialogBuilder.setPositiveButton("Save", { dialog, whichButton ->
//do something with edt.getText().toString();
// Add Name in list
nameList.add(editText.text.toString())
// Handler code here.
val intent = Intent(this, NewKitListActivity::class.java)
startActivity(intent);
})
dialogBuilder.setNegativeButton("Cancel", { dialog, whichButton ->
//pass
})
val b = dialogBuilder.create()
b.show()
}
Custom Dialog Layout: custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With