Search code examples
androidkotlinandroid-spinner

Showing Spinner items horizontally


Is it possible to show Spinner items horizontally or as grid? It's very difficult to reach the top of the screen with 1 hand.

class MainActivity : AppCompatActivity() {
    private lateinit var adapterPlaces: AdapterPlaces
    private lateinit var adapterAlphabet: AdapterAlphabet
    private val arrayItemsPlaces = ArrayList<ItemPlaces>()
    var list_of_items = arrayOf("A", "G", "H", "J", "S", "T", "V", "W")

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

        val mToolbar = findViewById<Toolbar>(R.id.myToolbar)
        val mRecyclerViewV = findViewById<RecyclerView>(R.id.mRecyclerViewWithToolbarV)
        val mSpinner = findViewById<Spinner>(R.id.mSpinner)

        // ...Do other stuff here
        setSupportActionBar(mToolbar)

        val mTitle = findViewById<TextView>(R.id.myToolbar_title)
        mTitle.text = "App"

        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.atlantic_avenue_barclays_center)
                )
        )
        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.grand_central_42nd_street)
                )
        )
        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.howard_beach_jfk_airport)
                )
        )
        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.jackson_heights_roosevelt_avenue_74th_street)
                )
        )
        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.south_ferry_whitehall_street)
                )
        )
        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.sutphin_boulevard_archer_avenue_jfk_airport)
                )
        )
        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.times_square_42nd_street_port_authority_bus_terminal)
                )
        )
        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.van_cortlandt_park_242nd_street)
                )
        )
        arrayItemsPlaces.add(
                ItemPlaces(
                        getString(R.string.west_farms_square_east_tremont_avenue)
                )
        )

        // Set Vertical RecyclerView
            mRecyclerViewV.layoutManager =
                    LinearLayoutManager(this)

            val mListener = AdapterView.OnItemClickListener { _, _, _, _ -> }
            adapterPlaces = AdapterPlaces(arrayItemsPlaces, mListener)

            mRecyclerViewV.addItemDecoration(
                    androidx.recyclerview.widget.DividerItemDecoration(
                            this,
                            LinearLayout.VERTICAL
                    )
            )
            mRecyclerViewV.adapter = adapterPlaces

        val mListener = AdapterView.OnItemClickListener { _, _, _, _ -> }
        adapterAlphabet = AdapterAlphabet(arrayItemsBtns, mListener)

        // Set spinner items
        val aa = ArrayAdapter(this, android.R.layout.select_dialog_item, list_of_items)
        aa.setDropDownViewResource(android.R.layout.select_dialog_item)
        mSpinner!!.setAdapter(aa)
    }
}

enter image description here


Solution

  • I believe this is what you're looking for: HorizontalSpinner. java

    NOTE. This creates all the views on creation (no recycling) so larger lists would most likely need a solution with view recycling.

    However, it's really not that complicated (and might be an interesting topic to explore) to create a simple custom view yourself Codelab by Google on Custom Views. In this case you could inherit from RecyclerView and create a well performing horizontal spinner yourself (and then release it as a library to share with the world :)