A dropdown or pull-down menu, also known as spinner, is one of the most essential UI elements for an app. In this tutorial I'll tell you how to add one to your Android app using Java.
Dropdown menus organize an app and improve the user experience. Almost every app has a dropdown menu integrated into its user interface.
Even though adding a dropdown menu in your Android app is as simple as dragging and dropping it, it sure can get tricky, especially if you are new to using Android Studio. If you're a fresh developer making your initial apps on Android Studio, this tutorial is for you.
Setting Up the Environment
Once you have created a project on Android Studio, open these files:
(The MainActivity.java file is opened by default when you create the project.)
With all the files open, your IDE should look like this:
Adding the Dropdown Menu Layout
Now, it's time to add the dropdown menu layout. In Android Studio, the layouts are added to the layout XML files. For that, head over to activity_main.xml. While you’re in the activity_main.xml file, open the Design tab. You’ll find it in the upper right corner of your IDE.
Android pull-down menus in Android Studio are added using Spinners.
If there is default text present on your application screen, head back to the code section and remove all the
TextViews. Now, from the design palette, select Containers. You’ll find Spinner there.
Once you’ve located the Spinner, drag and drop it on top of your mobile application. Android Studio will do the relevant coding for you, and you can later check it out by getting back to the code screen.
Depending on where you’ve dropped your Spinner, the layout code should look something like this:
Spinner android:id="@+id/spinner_languages" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="338dp" android:spinnerMode="dropdown" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" />
However, Android Studio allows you to customize your dropdown menu by changing its height, width, and margins through the attributes panel without having to code all of it from scratch.
While you’re at it, make sure that
spinnerMode is set to
dropdown to create a dropdown menu. You’ll find this setting in the attributes panel.
Positioning the dropdown menu on your application screen is quite easy. The Android Studio GUI will provide you with all the constraints to let you specify where your dropdown menu sits.
Once you’re satisfied with the styling of your dropdown menu, switch to the code view and edit the Spinner id. This will be required later on, when we integrate the Spinner into the Java file.
The Spinner id is found in the first line of the
<Spinner> tag. Give this an id that you'll remember for use elsewhere in your app code.
Lastly, head back to the design section and press the Infer Constraints button—which I personally call the "magic button"—on the top to take care of all the missing constraints in our code:
Adding Elements to the Dropdown Menu
Now that you’ve added a dropdown menu to your screen, it’s time to populate it with a bunch of choices for the user to choose from.
For this, you need to open up the strings.xml file. This file will be empty initially, and it should look something like this:
To add elements to your Android dropdown menu, you need to declare a string array and give it a name. Declare a
string-array under the already declared
string using the following syntax:
The string array has to be declared within the resources tag. Otherwise, you will get a Syntax error. The string array can be declared within the main Java file as well, but putting it in a separate XML file increases the reusability of the code and enhances the efficiency of your application. There’s no limit to the number of items that you can have in your dropdown menu.
Calling the Spinner in the Java File
Before we begin the coding, have the following classes imported into your code:
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast;
It’s best that these classes are imported beforehand to avoid any syntax errors later on. However, if you still get a Syntax error, you can always hover your mouse over it and then press Alt-Enter to import the relevant class for your code.
To pass the Android dropdown menu to Java, you have to define a
Spinner object. Use the
Spinner class name and give the object an appropriate name. Next, instantiate the spinner by looking it up with the same id you declared in the activity_main.xml file:
In the next step, you’ll have to create an
ArrayAdapter will be responsible for rendering every item in the languages string array to the screen when the Java dropdown menu is accessed.
ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this, R.array.languages, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
createFromResources() is a built-in method for the
ArrayAdapter class which takes three input parameters:
- the environment of the application—within an Activity, you can just use
- the name of the
StringArraythat you declared in the strings.xml file
- the layout type
For this particular example, we’re using a basic spinner layout.
The adapter that we declared above is useless unless it is attached to our dropdown menu (spinner). Therefore, set the spinner to use that adapter.
ArrayAdapter declared and successfully bound to the
spinner, you’ve successfully integrated your very first Android pull-down menu into your application.
You can now run your application on the emulator. It will look something like this:
With the Java dropdown menu all set, you can now play around with it a little in the activity_main.xml file. Try altering the height attribute and see how the dropdown menu moves across the screen.
You can add a dropdown menu to your Android application in a few simple steps.
For starters, you need to edit the XML files. Integrate the dropdown menu into them using Android Studio’s drag-and-drop feature. Next, you have to create a string array to add all the relevant items to your dropdown menu. Then, you need to get the
Spinner instance in the main Java file, along with an
ArrayAdapter. Lastly, you set your spinner up to use that adapter.
With all three code files working simultaneously, you’ll have an Android dropdown menu fully functional in your first Android app.
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post