Android Populating Spinner with JSON Tutorial

In this tutorial, you will learn how to populate a spinner with data remotely using a JSON file in your Android application. A spinner displays a dropdown menu with available values, from which the user can select or navigate. We will create a spinner on the main view and populate it with data remotely using a JSON file and on spinner click will show a selectable dropdown menu. On selected dropdown menu will show the selected results below the spinner. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project JSONSpinnerTutorial.

Application Name : JSONSpinnerTutorial

Project Name : JSONSpinnerTutorial

Package Name : com.androidbegin.jsonspinnertutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

In this activity, we have hosted a JSON file in our server and used the JSON functions to retrieve the JSON array and objects from the URL address. We have created an AsyncTask as a background task to load the JSON objects into a string array. Then we populated the spinner with the selected JSON data and on spinner dropdown item click will show the selected data on the textviews.

JSON File Link : http://www.androidbegin.com/tutorial/jsonparsetutorial.txt

Output:

Next, create an JSON functions class. Go to File > New > Class and name it JSONfunctions.java. Select your package named com.androidbegin.jsonspinnertutorial and click Finish.

Open your JSONfunctions.java and paste the following code.

JSONfunctions.java

Next, create an XML graphical layout for the MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

In your AndroidManifest.xml, we need to declare permissions to allow the application to connect to the Internet. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

Output:

JSONSpinnerTutorial ScreenShots

Source Code

JSONSpinnerTutorial (1.1 MiB, 405 downloads)
  • Umar

    Error establishing a database connection

    :/ cant download the project

    • http://www.AndroidBegin.com/ AndroidBegin

      Yea, I’m contacting my hosting account. it seems there are too many users accessing this site. please wait

    • http://www.AndroidBegin.com/ AndroidBegin

      It seems ok now. try downloading it now. thanks

      • Umar

        no its not still :/

        • http://www.AndroidBegin.com/ AndroidBegin

          It seems there are too many connections to the database. Seriously, giving me a headache. :(

          • Umar

            Finally !
            Got it thanks :)

          • http://www.AndroidBegin.com/ AndroidBegin

            Thats nice. Glad you had it. I’m bringing the site down for a quick maintenance. I’ll try to move my database to another location. :)

  • Yulius Ardian Febrianto

    Thanks

  • shruthi

    error occured in world(array declaration)

    • Steph

      me to having a same error

      • http://www.AndroidBegin.com/ AndroidBegin

        I apologize, I left out the JSONfunctions class. Thanks for your comment.

        • Stephen

          its ok.Everything will be fine now.

    • Stephen

      There will be a incomplete coding in these tutorial.they didn’t add JSONFunction class.With the help of Http request only we can connect to the URL.That also they are not included.

  • Steph

    In main activity WorldPopulation cannot be resolved error occured and then Error occured in world

  • shruthi

    i got succefully

    • Stephen

      ok. I am also got a output successfully with the help of stackoverflow

  • http://www.AndroidBegin.com/ AndroidBegin

    I apologize for my mistake, I left out the JSONfunctions class. I’ve just edited this post, should be working fine now.