TimonYT
TimonYT

Reputation: 121

Android Kotlin button + alert dialog + list

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)

Upvotes: 12

Views: 26083

Answers (2)

Christian
Christian

Reputation: 26387

I changed the code from Rajesh Dalsaniya's answer to be a bit more concise using kotlin's features:

fun showNewNameDialog(activity: Activity) {
    AlertDialog.Builder(activity).apply {
        val dialogView = activity.layoutInflater.inflate(R.layout.custom_dialog, null)
        val editText = dialogView.findViewById<EditText>(R.id.editTextName)

        setView(dialogView)

        setTitle("Custom dialog")
        setMessage("Enter Name Below")
        setPositiveButton("Save") { _, _ ->
            //do something with edt.getText().toString();

            // Add Name in list
            activity.nameList.add(editText.text.toString())
            // Handler code here.
            val intent = Intent(activity, NewKitListActivity::class.java)
            activity.startActivity(intent);
        }

        setNegativeButton("Cancel") { _, _ ->
            //pass
        }
    }.create().show()
}

The xml stays:

<?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>

Upvotes: 0

Rajesh Dalsaniya
Rajesh Dalsaniya

Reputation: 2127

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>

Upvotes: 18

Related Questions