Search code examples
javaandroidandroid-spinnerandroid-textinputlayoutmaterial-components-android

How do I capture the value of a Material drop down menu selection in Java?


I am using Material to design a form with drop down lists in android studio using Java.

I want to save the selection the user makes from the drop down list to a string so I can save this to Firebase, but when I try and log the result, it says the selection is null.

Does anyone know how I can capture what the user selects from this type of drop down menu? I did previously have a Spinner which worked but this doesn't seem to work in the same manner.

My XML code is as follows

 <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/cpdTypeLayout"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:hint="Activity Type">

            <AutoCompleteTextView
                android:id="@+id/cpdType"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="none" />
        </com.google.android.material.textfield.TextInputLayout>

My AddAdactivity.java code is

//CPD TYPE DROPDOWN MENU
    cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
    cpdType = findViewById(R.id.cpdType);

    String[] type = new String[]{
            "Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
    };

    ArrayAdapter<String> adapterType = new ArrayAdapter<>(
            AddActivity.this,
            R.layout.dropdown_item,
            type
    );

    cpdType.setAdapter(adapterType);

    cpdType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedTypeResult = parent.getItemAtPosition(position).toString();
        }

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

        }
    });

    Log.d("TAG", "You have selected " + selectedTypeResult);

EDIT: have since tried this but with no luck.

((AutoCompleteTextView)cpdTypeLayout.getEditText()).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            adapterType.getItem(position);
            String typeItem = ((AutoCompleteTextView)cpdTypeLayout.getEditText()).getText().toString();
            Log.d("TAG", "selected type is:" + typeItem);

        }

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

        }
    });

Solution

  • To get the String you can just use:

    String selectedValue =((AutoCompleteTextView)textInputLayout.getEditText()).getText();
    

    Otherwise if you want to use a listener you can use something like:

        ((AutoCompleteTextView)textInputLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String selectedValue = arrayAdapter.getItem(position);
            }
        });