Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 335
TimonYT Avatar asked Oct 02 '17 14:10

TimonYT


People also ask

How do I show alert dialog in Kotlin?

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.

How do I show a list in alerts?

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.

How many action button can a dialog have?

There should be no more than three action buttons in a dialog.


1 Answers

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>
like image 69
Rajesh Dalsaniya Avatar answered Sep 23 '22 23:09

Rajesh Dalsaniya