0

I'm trying to make an app and I want to make the menu or anything else to choose a language like shown below. Can you help me what should I do list or what ..?

enter image description here

Community
  • 1
  • 1
The King
  • 1
  • 3
  • 1
    You can use Spinners provided by Android: https://developer.android.com/guide/topics/ui/controls/spinner – Shobhit Puri Sep 05 '18 at 20:34
  • 2
    Do some research and get started. If you have a specific issue, come ask. Until then, just asking "how do I make a language selector" is far too broad. – TheWanderer Sep 05 '18 at 20:36
  • ok sorry i didn't know i still new here :) @TheWanderer – The King Sep 05 '18 at 20:38
  • @ShobhitPuri Thank you :) – The King Sep 05 '18 at 20:38
  • I believe the answer you are looking for is here: https://stackoverflow.com/a/12954037/4587155 – Vytautas Berankis Sep 05 '18 at 20:39
  • @VytautasBerankis this what i lookt for but is there is method in kotlin ? – The King Sep 05 '18 at 20:46
  • you can paste java code into android studio, it will ask for confirmation and then convert it to kotlin for you. – leonardkraemer Sep 05 '18 at 21:07
  • Java and Kotlin are very similar. Kotlin is just a different syntax for the JVM, and it's still heavily based on C++/Java. It shouldn't be too hard to manually convert, especially since Kotlin is often simpler to write. And, as @leonardkraemer said, you can usually paste Java code into a Kotlin file and have Android Studio convert it for you. This doesn't always work, however. – TheWanderer Sep 05 '18 at 21:17
  • @TheWanderer Good information thank you :) – The King Sep 05 '18 at 21:23

1 Answers1

2

It is important you learn the very basics when you're trying to find answers. Since this community is designed to help people, I have shared the quickest solution to your question here. I once again encourage you to learn the basics.

Add spinner code in activity layout file.

<Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

Kotlin code

    //Lanuages array.
    val languages = arrayOf("English", "French", "Spanish", "Italian", "Portuguese")

    val spinner = findViewById<Spinner>(R.id.spinner)
        if (spinner != null) {
            val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, languages)
            spinner.adapter = arrayAdapter

            spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                    Toast.makeText(this@MainActivity, getString(R.string.selected_item) + " " + languages[position], Toast.LENGTH_SHORT).show()
                }

                override fun onNothingSelected(parent: AdapterView<*>) {
                    // Code to perform some action when nothing is selected
                }
            }
        }

In strings.xml

<string name="selected_item">Selected Item</string>
Mbuodile Obiosio
  • 1,463
  • 1
  • 15
  • 19