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

1

6

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) {

        }
    });
Tenuto answered 22/7, 2020 at 16:52 Comment(2)
What is the value of selectedType from your log????Soy
Logcat says "You have selected null"Tenuto
E
18

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);
        }
    });
Essequibo answered 22/7, 2020 at 17:20 Comment(5)
Thanks for you reply on this. I've had a go at this using this code (in my edit to original question), and assuming I've understood what you mean, nothing is logging.Tenuto
@Tenuto arrayAdapter.getItem(position); is the value selected. You can use it as you prefer. The 2 options are alternative. Don't use getEditText().getText inside the listener.Essequibo
I have tried casting the arrayAdapter.getItem(position) to a string and logging that as well, but not getting any result in my log cat and I'm not sure why.Tenuto
@Tenuto You are using setOnItemSelectedListener. In the answer I'am using setOnItemClickListener. They are different.Essequibo
Ah, didn't spot that! Thank you very much, it now works!Tenuto

© 2022 - 2024 — McMap. All rights reserved.