1

I am new to Kotlin, Java, and Android Studio, but need to develop a survey-like Android application for a summer internship. I have gotten past some seemingly tough obstacles through the help of the SO community (I'm the only "developer" here), but this one has puzzled me and I haven't found anything on SO about it.

I have a custom spinner that contains values for different types of roofs (NoDefaultSpinner class found here), and on the selection of "Other," I would like for a Plain Text view to become visible on the same layout, if possible. Is there any way to make this happen or will I have to handle the case of "Other" by adding another activity screen?

Here is what I have so far:

class PollSectionInfoActivity : AppCompatActivity() {
    private var otherSelected = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_poll_section_info)

        val spinner = findViewById<Spinner>(R.id.roofType)
        var roofTypes : Array<String> = emptyArray()
        roofTypes += "BUR"
        roofTypes += "Liquid Applied"
        roofTypes += "Modified Bitumen"
        roofTypes += "Protective Membrane Assembly"
        roofTypes += "Single Ply"
        roofTypes += "Shingles"
        roofTypes += "Tile"
        roofTypes += "Other"
        roofTypes += "Unknown"

        val nextButton = findViewById<Button>(R.id.next_button)
        nextButton.isEnabled = false

        val adapter : ArrayAdapter<String> = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, roofTypes)
        spinner.adapter = adapter
        spinner.onItemSelectedListener

        spinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val selectedItem = parent!!.getItemAtPosition(position).toString()
                this@PollSectionInfoActivity.otherSelected = selectedItem == "Other"
                nextButton.isEnabled = true
            }
        }
    }

    //TODO: update code to pass selection to next layout.
    fun nextButton() {
        val spinner = findViewById<Spinner>(R.id.roofType)
        val selection = spinner.selectedItem.toString()
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".PollSectionInfoActivity">

<Button
    android:id="@+id/next_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/button_next"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<com.testapp.NoDefaultSpinner
    android:id="@+id/roofType"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:prompt="@string/select_roof_type"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:checked="false"
    android:text="@string/surfacing"
    android:visibility="visible"
    app:layout_constraintStart_toStartOf="@+id/roofType"
    app:layout_constraintTop_toBottomOf="@+id/roofType" />

Also, any additional help or advice you want to throw in is more than welcome! I've only taken one (pretty dull) software course in college, so I don't know too much about all of this. Plus, I'm more of a theory person anyway. Thanks in advance!

P.S. I apologize if this has been asked elsewhere; I honestly could not find a solution to this, or at least what appeared to be a solution to this.

WLoumakis
  • 23
  • 5

1 Answers1

0

You can add TextView to your layout and set visibility to invisible.

<TextView
    android:id = "@+id/textView"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:visibility = "invisible"/>

In OnItemSelected method if user chose "Other" you can set TextView visibility to visible like this

textView.visibility = View.VISIBLE
Igor Staszewski
  • 401
  • 2
  • 8