How to use asyncspinner kotlin

45 Views Asked by At
**MainActivity.kt**
package com.example.form

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Spinner

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var etinput1= findViewById<EditText>(R.id.etinput1)
        var etinput2=findViewById<EditText>(R.id.etinput2)

        var checkbox1=findViewById<CheckBox>(R.id.checkbox1)
        var checkbox2=findViewById<CheckBox>(R.id.checkbox2)

        var btn_next_activity=findViewById<Button>(R.id.next_activity)

        val spinnerOptions = findViewById<Spinner>(R.id.spinnerOptions)
        val options = resources.getStringArray(R.array.spinner_options)

        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, options)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinnerOptions.adapter = adapter

        var selectedSpinnerOption: String? = null // Variable to hold the selected spinner option

        spinnerOptions.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                // Retrieve the selected item from the spinner
                selectedSpinnerOption = options[position]
            }`your text`

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // Handle when nothing is selected
            }
        }


        btn_next_activity.setOnClickListener(){
            val intent = Intent(this, MainActivity2::class.java)
            val checkbox1Value = checkbox1.isChecked
            val checkbox2Value = checkbox2.isChecked
            intent.putExtra("INPUT1", etinput1.text.toString())
            intent.putExtra("INPUT2", etinput2.text.toString())
            intent.putExtra("CHECKBOX1", checkbox1Value)
            intent.putExtra("CHECKBOX2", checkbox2Value)
            intent.putExtra("SPINNER_OPTION", selectedSpinnerOption) // Pass the selected spinner option
            startActivity(intent)
        }

    }
}

**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height=`your text`"wrap_content"
        >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etinput1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="15dp"
            android:gravity="center"
            android:hint="Input 1" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etinput2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="15dp"
            android:gravity="center"
            android:hint="Input 2" />

    </com.google.android.material.textfield.TextInputLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Checkbox 1"
            android:layout_margin="15dp">

        </CheckBox>

        <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Checkbox 2"
            android:layout_margin="15dp">

        </CheckBox>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Spinner
            android:id="@+id/spinnerOptions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">



    <Button
        android:id="@+id/next_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="next activity"
        android:layout_margin="15dp"
        />

    </LinearLayout>
</LinearLayout>

**strings.xml**
<resources>
    <string name="app_name">Form</string>
    <string-array name="spinner_options">
        <item>Option 1</item>
        <item>Option 2</item>
        <item>Option 3</item>
    </string-array>

</resources>

**MainActivity2.kt**
package com.example.form

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText

class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        val etoutput=findViewById<EditText>(R.id.etoutput)

        val input1 = intent.getStringExtra("INPUT1")
        val input2 = intent.getStringExtra("INPUT2")
        val checkbox1Value = intent.getBooleanExtra("CHECKBOX1", false)
        val checkbox2Value = intent.getBooleanExtra("CHECKBOX2", false)
        val selectedSpinnerOption = intent.getStringExtra("SPINNER_OPTION")


        val receivedData = "Input 1: $input1\nInput 2: $input2\n Checkbox1: $checkbox1Value\nCheckbox2: $checkbox2Value\nSpinnerOption: $selectedSpinnerOption"
        etoutput.setText(receivedData)

    }
}

**activity_main2.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">


    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etoutput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="15dp"
            android:gravity="center"
            android:text="OUTPUT PAGE" />
    </com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

lollipop112-asyncspinner

I tried getting asyncspinner to work but didnt work. I included implementation for different types of inputs like checkboxes,spinner, editable text. I am trying to send it from one activity to another but the results arent getting displayed on the second page. I have tried different ways, using intents and intent extras but that did not work either. Are there any other ways or intents I can use? I am using Kotlin entirely, the options for the spinner are mentioned in the strings.xml file.

0

There are 0 best solutions below