-1

I have an editText and I set setOnFocusChangeListener to open a spinner. Once I select an item from the spinner, I want the selected item to show on the same edit Text.

Below is the code I have tried.

List<String> tal = new ArrayList<String>();
        tal.add("1");
        tal.add("2");
        tal.add("3");
        tal.add("4");
        tal.add("5");

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tal);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(hasFocus){
                        spinner.performClick();

                    }
                }
            });
            editText.setText(spinner.getSelectedItem().toString()); //this is taking the first value of the spinner by default. 

Xml:

<EditText
        android:hint="Select A Value"
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_edittext"
        android:layout_weight="0.05"
        android:singleLine="true"/>

<Spinner android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner" />
ASN
  • 1,655
  • 4
  • 24
  • 52
  • 1
    Possible duplicate of [How can I use onItemSelected in Android?](http://stackoverflow.com/questions/20151414/how-can-i-use-onitemselected-in-android) – Janki Gadhiya Jul 12 '16 at 04:05

1 Answers1

4

You can use the code like this. I done for you..

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean hasFocus) {
            // TODO Auto-generated method stub
            if(hasFocus){
                spinner.setVisibility(View.VISIBLE);

            }
        }
    });

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
            editText.setText(spinner.getSelectedItem().toString()); //this is taking the first value of the spinner by default.

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

You can only use OnItemSelectedListener on spinner in android.Red more from Android Developers. Good luck..

  • Thank you so much for the code. But I think you to set the spinner visibility to hide after the selection is made and it is reflected on edittext. – ASN Jul 12 '16 at 05:36
  • 1
    You can hide it by using "spinner.setVisibility(View.GONE);" You can use it in else part of if(hasFocus){ spinner.setVisibility(View.VISIBLE); }else {spinner.setVisibility(View.GONE);} –  Jul 12 '16 at 06:13