Dynamic and Static Dropdown Menu(Spinner) in Android

Dynamic and Static Dropdown Menu(Spinner) in Android. In this article, we will look at an example to create a Dynamic and Static Dropdown Menu(Spinner) in Android.

In Android, we use Spinner for DropDown Menu. Spinner is a View that is similar to the dropdown list .it is used to select one item from the list of items it shows all the items present in the Spinner from which we can select one. We can bind values to the spinner or add item items to the spinner through ArrayAdapter.

Step 1: Create a project with Empty Activity

Static DropDown

  • Static DropDown is Assigned during Compile time
  • Spinner is used for creating a drop-down menu
  • First, we create an object of spinner class
  • FindViewById is used to assign Spinner object to Spinner View in XML
  • EditText is an Edit text view in XML and we assign the object to view so we can change or modify views
  • ArrayAdapter is an Android SDK class for adapting an array of objects as a Datasource
  • CreateFromResource will assign the array of fruits form R.array.fruits in res -> value -> strings.xml to adapter
  • setDropDownViewResource is a custom Style for DropDown
  • R.Layout.simple_list_item_activated_1 is a predefined Spinner Style
  • An Adapter object acts as a bridge between an AdapterView and the underlying data for that view
  • setAdapter method is used to set an Array of data to view
  • setOnItemSelectedListener method invoked when an item in this view has been selected
  • onItemSelected will invoke when an Item is selected on Spinner View
  • getItemAtPosition(i).toString will return the item which selected to fruit Variable
  • setText(fruit) will change the text in EditText view.

Dynamic DropDown

  • String item[] is an array of String.
  • Dynamic DropDown is Assigned during Run time.

MainActivity.class

package com.example.dropdown;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spinner = findViewById(R.id.static_spinner);
        EditText editText = findViewById(R.id.editTextStatic);
        EditText editText1 = findViewById(R.id.editTextDyna);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.Fruits,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String fruit = adapterView.getItemAtPosition(i).toString();
                editText.setText(fruit);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        Spinner dynamicSpinner = findViewById(R.id.dynamic_spinner);
        String item[] = {"Carrot","Corn","Cucumber","Broccoli","Radish"};
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, item);
        dynamicSpinner.setAdapter(adapter1);
        dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String s =adapterView.getItemAtPosition(i).toString();
                editText1.setText(s);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}

activity_main.xml

  • Parent Node is LinearLayout
  • Orientation is used to assign an orientation to the view
  • We have two Child nodes of LinearLayout inside Parent Node
  • This Child node Orientation is set to Horizontal
  • Each Child node Has a Spinner and an EditText view
  • Spinner is used to creating to the dropdown list.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/home_page">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Static and Dynamic DropDown "
        android:textSize="20dp"
        android:gravity="center"
        android:textColor="#FFFFFF"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="20dp">
    <Spinner
        android:id="@+id/static_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"

        />

        <EditText
            android:id="@+id/editTextStatic"
            android:layout_width="104dp"
            android:layout_height="50dp"
            android:editable="false"
            android:textColor="#FFFFFF"
            android:textSize="20dp"
            android:textStyle="bold" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Spinner
        android:id="@+id/dynamic_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editTextDyna"
            android:layout_width="104dp"
            android:layout_height="wrap_content"
            android:editable="false"
            android:textColor="#FFFFFF"
            android:textSize="20dp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

Step 2: Create Array for Static DropDown

  • Go to -> res and then string.xml

  • We will create an array for Static Dropdown
  • String-array is when we create an array with the name Fruits
  • item is the list of items for Static Dropdown.

Run the Application

In this way, we have created a Dynamic and Static Dropdown Menu(Spinner) in Android.

Android practice tasks

https://codebun.com/login-and-registration-in-android-and-sqlite/

https://codebun.com/login-and-registration-in-android-using-firebase/

https://codebun.com/crud-operation-using-sqlite-in-android/

https://codebun.com/crud-operation-in-android-using-firebase-database/

https://codebun.com/how-to-get-data-from-api-in-android/

https://codebun.com/how-to-create-custom-alert-dialog-in-android/

https://codebun.com/search-and-sort-records-in-android-with-recycler-view/

https://codebun.com/create-a-custom-notification-with-custom-message-in-android/