Android Drop Down List Tutorial

Share
Embed
  • Published on Aug 14, 2016
  • In this tutorial you will learn what is android spinner (android drop down list), how to create spinner, android spinner adapter and how to use it inside your app.
    - First open up activity_main.xml file and then replace the hello world textview with a spinner, then we will position the spinner to the top-center of the screen like this:
    Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    - Now open up MainActivity.java class and declare the spinner like this:
    Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
    - Next is we need some data to populate the spinner adapter, we can do that by adding an array of items inside the strings.xml file which you can find it in the (Res) folder, then we can reference that array by it's ID and use it for the adapter.
    That's it...Run the app and you will see a spinner with values being populated whenever the user taps on it
    Source code is here: github.com/codingdemos/SpinnerExample

Comments • 115

  • Jiao Qunzhi
    Jiao Qunzhi 5 days ago

    Why there is no simple_list_item for me, I wonder when did you created this

  • ANALYN BICO
    ANALYN BICO 22 days ago

    What if the data is coming from the server and needs to be set up as dynamic. ?

    • Coding Demos
      Coding Demos  15 days ago

      Hi, you can initialize an array that will hold the data coming from the server, then you will use this array inside the spinner to show the result

  • Dj Venom
    Dj Venom Month ago +1

    Thank you for the tutorial!
    But would you also please tell how to make them work?
    So for instance, if I choose "Work" - first thing happens
    "home" - second and so on.

  • alramdein
    alramdein 3 months ago +1

    Thank you very much! You're a good tutor. Clear instructions make this tutorial so excellent.

  • Sachin Patel
    Sachin Patel 3 months ago

    This is an excellent tutorial. Very useful. Thank you so much.

  • Delia
    Delia 4 months ago

    when a user select a data from the spinner, how can i insert his choose in sqlite database?

    • Delia
      Delia 4 months ago

      @Coding Demos i will try, thank you so much ❤️

    • Coding Demos
      Coding Demos  4 months ago +1

      Hi, first you get the user selected value from spinner like this:
      String selectedValue = spinner.getSelectedItem().toString();
      To write data in SQLite db you will use ContentValues like this:
      SQLiteDatabase database = this.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put("spinner value", selectedValue);
      database.insert("table_name", null, values);
      database.close();
      I hope that helps :)

  • Raramainashe Wadaya
    Raramainashe Wadaya 5 months ago

    thank you for the video!

  • rahali badr eddine
    rahali badr eddine 7 months ago

    thanks for the help !

  • Princess Serah
    Princess Serah 7 months ago

    Thank you for sharing. This is an excellent tutorial.

  • Ravi Kumar
    Ravi Kumar 8 months ago

    suppose if I have multiple spinners with different values, based upon selected values I have to open the new activity So how can I do that??
    Can anyone Help me!?

    • Sean Online
      Sean Online 2 months ago

      Whether or not this question still needs answered, to do so you would need to select the data from the strings array, and depending on which data is selected, a switch be used to push the user to a different activity via an intent.

  • Marxt123
    Marxt123 9 months ago

    Please help me, I get an error when I build that says: error: cannot find symbol variable array.
    I did everything correctly, please help.

    • Marxt123
      Marxt123 8 months ago

      @Coding Demos Nope, does not work, this is a very weird bug, it causes the error here:


      Build: build failed
      Run Build
      Run Tasks
      :Instant_App:compileDebugFeatureJavaWithJavac

      Execute Compile:
      org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler
      error output for details.

      at com.android.build.gradle.tasks.AndroidJavaCompile.compile(AndroidJavaCompile.kt:233)
      at java.util.concurrent.ThreadPoolExecutor.runWorker
      at java.util.concurrent.ThreadPoolExecutor$Worker.run
      at java.lang.Thread.run


      Java Compiler:
      (All of the folders which my project and Java file are)
      error: cannot find symbol variable array

    • Coding Demos
      Coding Demos  9 months ago

      The code you shared with me works fine. Try to clean and rebuild your project

    • Marxt123
      Marxt123 9 months ago

      @Coding Demos This is my Java file code:

      Spinner langSpinner = (Spinner) findViewById(R.id.langSpin);
      ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
      R.array.languagesSett, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      langSpinner.setAdapter(adapter);

      This is from the Android Developers website code by the way.
      (Sorry if I'm not showing you what you actually wanted, I am just thinking that I have to show you the code.)

    • Coding Demos
      Coding Demos  9 months ago

      Your welcome :)
      Can you please show me how you reference that string-array inside the java file?

    • Marxt123
      Marxt123 9 months ago

      @Coding Demos Yes I did (Thanks for the reply):

      English
      Espanol

  • yoogesh a
    yoogesh a 10 months ago

    Can you tell me how to do.."from and to date" validation

  • Neetin Khedkar
    Neetin Khedkar 10 months ago

    Ok, thanks. Now, I want to use the chosen value to open a particular pdf file, which is available in resources. I have written:-
    // attempting to use spinner value
    selectedPlan=mySpinner.getSelectedItem().toString();
    // extracted value selectedPlan may be used to display appropriate plan
    if (selectedPlan=="Open Spaces");
    Intent i=new Intent(ArchPlans.this,OSPlan.class);
    startActivity(i);
    why doesn't this work?

  • Neetin Khedkar
    Neetin Khedkar 11 months ago +1

    Excellent tutorial. Thanks. I was successful at the first trial, thanks to your lucid explanation.
    Now suppose the user has chosen an item in the spinner. I want to use his input for further work. How do I do it? Do you have a tutorial for that?

    • Coding Demos
      Coding Demos  11 months ago +3

      Hi, great i'm happy to hear that :)
      You can get the selected value from the spinner like this:
      String spinnerValue = spinner.getSelectedItem().toString();

  • 94narcotic
    94narcotic Year ago

    in final, mySpinner.toString()

  • Jefferson de deus leite

    Excellent Tutorial!!

  • RebornChiekoGaming

    where the fuck do u get MyAdapter from?

  • The LKs
    The LKs Year ago

    how to save this spinner option into firebase database?

  • Terrance Low
    Terrance Low Year ago

    Do you know how to make it store in sqlite database?

  • The way forward.
    The way forward. Year ago

    sir, Thank you for the wonderful explanation.
    I am doing it on java itself instead of xml, just because it found it easier.
    Now, I have another spinner immediately after this . If the user selects "home' in this spinner i want to show "Clean, cook, wash or sleep" in the second spinner and if the user selects "office" I want to show "meeting, project, resources" only and not the things that are to be done at home. Please help me.
    I have 38 different items in the first spinner and the second will have 576 items divided into these 38 items.

  • Ankit Kundu
    Ankit Kundu Year ago

    Will spinner be able to scroll when the options are too many to many to be shown in one view

    • Ankit Kundu
      Ankit Kundu Year ago

      Could you upload a video showing how to validate spinner in form filling

    • Coding Demos
      Coding Demos  Year ago +1

      Hi, yes it will scroll

  • Cameron Chapman
    Cameron Chapman Year ago

    Great video, but why did you not initially set the "android.R.layout.simple_list_item_1" instead of initially using "simple_spinner_dropdown_item" ?? Thank you! This video and I am sure the following will help me greatly.

  • Z Zike
    Z Zike Year ago

    does anybody know how to add a spinner on a google map activity? i cant seem to put it on the google map.xml

  • meysam Bagheri
    meysam Bagheri Year ago

    Thank you

  • Deepak Nema
    Deepak Nema Year ago

    Hi its Good Tutorials but how to do separator for every item like line .

    • Vangari Venu Gopal
      Vangari Venu Gopal Year ago

      use View and make layout height as 1 dp or as per ur requirement

  • Tejashree Devale
    Tejashree Devale Year ago

    Beautifully explained..!

  • brian k
    brian k Year ago

    Thank you, very hard to find tutorials with someone that speaks clearly.

  • Jhon Abonales
    Jhon Abonales Year ago

    Nice video tutorial sir, but i have a question how to add border line like in Plain Text?

  • Ulukmyrza Zhakypbek Tegin

    Conversion. For example: Two edittext and two spinners. On the first edittext you write down the value (42), and you select the spinner by the kilometer. And on the second spinner you choose the meter, and immediately on the second edittext shows the answer: (42000) and immediately saved the data in the database. How to do this?

  • Ulukmyrza Zhakypbek Tegin

    In the spinner, how to make an optional spinner stored the data in a database

  • Dpzs Biswa
    Dpzs Biswa Year ago

    Great one....

  • Shikhar Tyagi
    Shikhar Tyagi Year ago

    Great Video!
    My ques is how to use the same if I want suggestions of my contacts?

  • Humberto G
    Humberto G Year ago +3

    When I try to reach the data used to populate the spinner on the third context @5:25 after I type getResources().getStringArray(R.array.myarray)); the array right after the array R. is highlighted red. I don't seem to have the class array. How can I fix that?

    • Coding Demos
      Coding Demos  Year ago

      Awesome, i'm glad you got it fixed and thanks for sharing the solution :)

    • Humberto G
      Humberto G Year ago +1

      Hi, thank you for your prompt reply! Your tutorials are awesome. I fixed the issue, turns out I just had to invalidate the cache/restart, and that did it for me.

    • Coding Demos
      Coding Demos  Year ago

      Hi, did you create the string-array items inside strings.xml file?

  • Felix Hoste
    Felix Hoste Year ago +2

    Thank you very much, I am just getting into app development and wanted to make a unit conversion app. This tutorial helped a lot!

  • Ashraf Uzzaman Saleh

    How could I add a label to the spinner which is not selectable?
    which is appear innitially when no item is selected.
    thanks in advance.

    • Coding Demos
      Coding Demos  Year ago +1

      Hi, here is how you can do it:
      1- Add another value to the Spinner string-array and name it "Please select a value". The string-array will look like this:
      Please select a value
      Home
      Work
      Other
      Custom

      2- Let's say later when a user taps on a button, inside onClick method you check the spinner value. If the spinner value is equal to "Please select a value", you will show a Toast message informing the user like this:
      if(mySpinner.getSelectedItem().toString().equalsIgnoreCase("Please select a value")){Toast.makeText(MainActivity.this, "Please select a value", Toast.LENGTH_SHORT).show();
      }

  • Francisco de Solminihac Urmeneta

    Excellent Tutorial, Simple and clean commands!! Thank you very much.

    • Coding Demos
      Coding Demos  Year ago

      Hi, yeah the accepted answer looks simple but i can't say it's the best way to do it :)

    • Francisco de Solminihac Urmeneta
      Francisco de Solminihac Urmeneta Year ago +1

      Hi Coding Demo,I would like to know if this is the best way(Clean and simple) to center text on a spinner.
      stackoverflow.com/questions/24317634/how-do-i-center-text-in-a-spinner-in-android
      Thank you in advanced!

    • Coding Demos
      Coding Demos  Year ago

      Your welcome, happy coding :)

  • moby boy
    moby boy Year ago +1

    I followed exactly the code, But my spinner looks white blank. no arrow, no line bellow the items. I tried also other R.layouts but none worked. why is that?

    • Coding Demos
      Coding Demos  Year ago +1

      Hmm that is strange. Can you please email me (codingdemos@gmail.com) your project source code? I need to see what is causing this issue

  • Mataro X
    Mataro X 2 years ago +2

    No matter what or how I do it I keep getting unsupported type string-array error... how can I fix that?

    • Coding Demos
      Coding Demos  2 years ago

      Hi, can you please show me the string array that you want to create?

  • ahsan syed
    ahsan syed 2 years ago +1

    how can i show data from the spinner ? for example if i select home so i need to display that selected option . kindly help me ASAP

    • Coding Demos
      Coding Demos  Year ago +1

      That's correct, thanks for sharing :)

    • Coding Demos
      Coding Demos  2 years ago +1

      Hi, you need to use spinner.setOnItemSelectedListener, this will allow you to select an item from the spinner. So if you want to select home you do it like this:
      mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
      switch (i) {
      case 0:
      Toast.makeText(MainActivity.this, "Selected home", Toast.LENGTH_SHORT).show();
      break;

      }
      }
      @Override
      public void onNothingSelected(AdapterView adapterView) {
      }
      });
      I hope that helps :)

  • Bisma Wirawan
    Bisma Wirawan 2 years ago +1

    nice video sir
    by the way, i want to make a spinner, and set error on it, can you teach me how to do it?

    • Coding Demos
      Coding Demos  2 years ago

      Hi, thank you :)
      Now about your question: You can just show a simple Toast message when the user for example selected an invalid item from your spinner

  • MD.FAISAL KABIR
    MD.FAISAL KABIR 2 years ago +1

    Thank You.

    • Coding Demos
      Coding Demos  2 years ago

      Hi, your welcome :)
      Yeah i'm interested, please email me (codingdemos@gmail.com) the details about the app and any challenges that you face during development.
      Thanks.

    • Eltayeb Mohamed
      Eltayeb Mohamed 2 years ago +1

      Hi. Thank you for the video about the spinner.
      I am learning android programming. I am Sudanese but I live and work in KSA. I teach English in a private institute. I write programs that convert the English exercise in the textbook into interactive mobile apps to be installed on my students mobile phones. Sometimes I get stuck. That’s why I need a mentor. If you can help, let me know so that we can agree on a price and a method of payment. If you are not interested, please recommend someone who can help me when I get stuck.

    • Coding Demos
      Coding Demos  2 years ago

      Your welcome :)

  • Uvek Prvi Uvek Ispred Svih

    How to position text so it does not go over the background.
    i.stack.imgur.com/YlGPO.png

    • Coding Demos
      Coding Demos  2 years ago

      Can you please send me (codingdemos@gmail.com) the code for the activity_main.xml?

    • Uvek Prvi Uvek Ispred Svih
      Uvek Prvi Uvek Ispred Svih 2 years ago +1

      I add this line in activity_main.xml layout and there is still text over the background of the spinner

    • Coding Demos
      Coding Demos  2 years ago +1

      You can add this line to position it below the background:
      android:layout_below="@+id/background"

  • Erasmo JF Da Silva
    Erasmo JF Da Silva 2 years ago

    Coding Demos Thank you very much for this awesome tutorial, Demo

  • Bryan Ng
    Bryan Ng 2 years ago

    Anyone knows where to put the code if you have individual tabs java classes?

  • valtira gessa
    valtira gessa 2 years ago

    how i can do this in navigation menu?

  • George Dorrington
    George Dorrington 2 years ago

    When I run this on my phone it crashes anyone have any idea why?

    • Coding Demos
      Coding Demos  2 years ago

      I need to see some code to be able to help you, you can email me (codingdemos@gmail.com) and i'll see what i can do

    • George Dorrington
      George Dorrington 2 years ago +1

      Any solutions?

    • Coding Demos
      Coding Demos  2 years ago

      Hi, i guess the crash is related to the pdf viewer that you are trying to use inside the fragment

    • George Dorrington
      George Dorrington 2 years ago

      Hi I'm to implement the pdf viewer into a fragment, my error is, E/Qmage: isQIO : stream is not a QIO file

    • Coding Demos
      Coding Demos  2 years ago

      Can you please tell me what is the error that you see in the logcat?

  • George Dorrington
    George Dorrington 2 years ago

    Anyone help me on why I have no myAdapter option?

  • Omal Mannapperuma
    Omal Mannapperuma 2 years ago

    Your turorial is so good. Only issue is, the dark background you have used. You could have changed the dark background color of the IDE to a brighter one, at least for the turorial sake.
    Thank you.

  • soosXD
    soosXD 2 years ago

    holy shit i just needed to put it in the method, FACEPALM. Thanks man

  • Manisha Agrawal
    Manisha Agrawal 2 years ago +1

    AWSM..THANKYOU

  • Chethan Bhandarkar
    Chethan Bhandarkar 2 years ago +1

    great video !!! excellent!!!! bt here i am stuck with a problem
    in my app ,i want to use two spinners
    first spinner selects departure place
    second spinner selects destination place
    I want to get source and destination
    and compare those with the predifined place
    eg spinner1 newyork
    spinner2 paris
    nw i want to use
    if (spiner1value is equal to newyork and spiner2 value is equal to paris)
    open an activity

    it works fine qith one spinner

    but when using two spinners
    i dont know how to use this,
    please make a tutorial on how to use two spiner and when u press a button
    it should start a new activity
    if the places match

    when im using only one spinner in app
    then

    • Chethan Bhandarkar
      Chethan Bhandarkar 2 years ago +1

      Coding Demos thanku soo much
      i have another problem i couldnt find exact solution in web
      i am absolutly new to android designing
      when i design ,i design in AIDE app for android
      i design for a 5.5 inch phone
      so i use fixed dp size for some of layouts and button so as to fit in perfectly for my phone
      but when i install in diffrent phones
      the fixed dp creates a problem
      the layout gets croped in smaller phones

      please make a video on how to make
      complex layouts that will fit in for all mobile phones bt at the same time
      i want to adjust the buttons position at some place that i cannot use wrapcontent and matchparent
      it would be a great help

    • Coding Demos
      Coding Demos  2 years ago +1

      That's great...Good luck with your app and let me know if you need any help :)

    • Chethan Bhandarkar
      Chethan Bhandarkar 2 years ago +1

      Coding Demos thanku for the help
      i just found the solution in net which is same as you told now ,i found it after 8hours
      u r equaly genious thanku

      package com.mycompany.myapp;
      import android.app.*;
      import android.os.*;
      import android.widget.*;
      import android.widget.Spinner.*;
      import android.widget.SpinnerAdapter.*;
      import android.view.*;
      import android.content.*;
      public class MainActivity extends Activity
      {
      Spinner mSpinner1,mSpinner2;
      String category[]={"kalpane","B.C.Road","Mallur","Mangalore"};




      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      mSpinner1=(Spinner)findViewById(R.id.spinner1);
      mSpinner2=(Spinner)findViewById(R.id.spinner2);
      ArrayAdapter adapter1= new ArrayAdapter(this,R.layout.test,category);
      ArrayAdapter adapter2= new ArrayAdapter(this,R.layout.test,category);
      mSpinner1.setAdapter(adapter1);
      mSpinner2.setAdapter(adapter2);


      }

      public void onButtonClick(View l)
      {
      if(l.getId() ==R.id.button)

      {
      //First get the selected country name
      String sp1Val = mSpinner1.getSelectedItem().toString();
      String sp2Val = mSpinner2.getSelectedItem().toString();
      // Then compare both spinner values:
      if(sp1Val.equalsIgnoreCase("mallur") && sp2Val.equalsIgnoreCase("kalpane")){
      Intent i = new Intent(MainActivity.this,mallur.class);
      startActivity(i);
      }

      }
      }
      }

    • Coding Demos
      Coding Demos  2 years ago

      Hi, you can define a spinner for departure and destination like you did there, then inside a button onClickListener you can check for the value before starting a new activity for example:
      if(spinnerDepart.getSelectedItem.toString.equalIgnoreCase(value) && spinnerDestin.getSelectedItem.toString.equalIgnoreCase(value)){
      Intent mIntent = new Intent(MainActivity.this, DetailActivity.class)
      startActivity(mIntent);
      }else{
      //You can show a toast message informing the user that there was something wrong
      }
      Please do let me know if you need more help, happy coding :)

  • Lordz Lordz
    Lordz Lordz 2 years ago

    Very helpful, thank you for that!

  • Ruma Jreij
    Ruma Jreij 2 years ago

    Very concise, good explanation, friendly. Thank you.

  • Praveen P
    Praveen P 2 years ago

    Tq for this video.. Can i have video that how can i add item to Spinner dynamically?

  • Yashwanth K L
    Yashwanth K L 2 years ago

    hello... so how can I access the data which has been selected in the spinner...

  • ArUk MuKh
    ArUk MuKh 2 years ago

    each time i reopen my spinner application
    i get the option i selected previously. how to reset it?
    n change the color of the text selected

  • Andrea Rosario-Gborie
    Andrea Rosario-Gborie 2 years ago +1

    Wonderful!

  • Miten Kashiyani
    Miten Kashiyani 2 years ago +1

    Thank you for awesome tutorial. :) i want to know that how can we add image or icon before the text in spinner. Like in spinner it should show the country names with its flags. it would be great if you can help me out. thanks!

    • Coding Demos
      Coding Demos  2 years ago

      Thanks for watching :)
      Here is a tutorial that will help you with creating custom spinner to be able to show country names with it's flags: abhiandroid.com/ui/custom-spinner-examples.html

  • Andreas Gratzer
    Andreas Gratzer 2 years ago

    Great you should have more Subscribers!