

ANDROID STUDIO SPINNER CUSTOM DROPDOWN ANDROID
With the ArrayAdapter declared and successfully bound to the spinner, you’ve successfully integrated your very first Android pull-down menu into your application. Therefore, set the spinner to use that adapter. The adapter that we declared above is useless unless it is attached to our dropdown menu (spinner). The name of the StringArray that you declared in the strings.xml fileįor this particular example we’re using a basic spinner layout. Within an Activity you can just use this.

The ArrayAdapter will be responsible for rendering every item in the languages string array to the screen when the Java dropdown menu is accessed.ĪrrayAdapteradapter=ArrayAdapter.createFromResource(this, R.array.languages, android.R.layout.simple_spinner_item) ĪtDropDownViewResource(android.R.layout.simple_spinner_item) ĬreateFromResources() is a built-in method for the ArrayAdapter class which takes three input parameters: Now in the next step, you’ll have to create an ArrayAdapter. Spinner spinnerLanguages=findViewById(R.id.spinner_languages) Next, instantiate the spinner by looking it up with the same id you declared in the activity_main.xml file: Use the Spinner class name and give the object an appropriate name. To pass the Android dropdown menu to Java, you have to define a Spinner object. 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. It’s best that these classes are imported beforehand to avoid any syntax errors later on. Calling the Spinner in the Java fileīefore we begin with the coding, have the following classes imported into your code: There’s no limit to the number of items that you can have in your dropdown menu.
ANDROID STUDIO SPINNER CUSTOM DROPDOWN CODE
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. Otherwise, you will be get a Syntax error. The string array has to be declared within the resources tag. Declare a string-array underneath the already declared string using the following syntax: To add elements to your Android dropdown menu, you need to declare a string array and give it a name. This file will be empty initially and it should look something like this: 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.įor this, you need to open up the strings.xml file. 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 Give this an id that you’ll remember for use elsewhere in your app code. The Spinner id is found in the first line of the tag. 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 Android Studio GUI will provide you with all the constraints to let you specify where your dropdown menu sits. Positioning the dropdown menu on your application screen is quite easy. You’ll find this setting in the attributes panel. While you’re at it, make sure that spinnerMode is set to dropdown to create a dropdown menu. 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. Android Studio will do the relevant coding for you and you can later check it out by getting back to the code screen.ĭepending on where you’ve dropped your Spinner, the layout code should look something like /> Once you’ve located the Spinner, drag and drop it on top of your mobile application.

In case you are unable to find it, you can simply click on the search icon and search for Spinner. In the older versions of Android Studio Spinner might be located under the widgets’ section. Please note here that we are using Android Studio 4.2.2. Now, from the design palette, select Containers. If there is default text present on your application screen, head back to the code section and remove all the TextViews. You’ll find it in the upper right corner of your IDE.Īndroid pull down menus in Android Studio are added using Spinners. While you’re in the activity_main.xml file, open the Design tab. For that, head over to activity_main.xml. In Android Studio, the layouts are added to the layout XML files. Now, it is time to add the dropdown menu layout. With all the files open, your IDE should look like this: Adding the Dropdown Menu Layout (The file MainActivity.java is opened by default when you create the project.) Once you have created a project on Android studio, open these files:Īpp/java//MainActivity.java
