Search code examples
androidandroid-spinnerautocompletetextview

How to swap values of two AutoCompleteTextView


I have added two autoCompleteTextView with simple spinner and now I want to interchange their values by clicking an Imageview. But Whenever I click that ImageView, my app restarts. There is no error or warning.
Here's the Java code (UPDATED) :-

public class location extends AppCompatActivity {
    private AutoCompleteTextView autoText1,autoText2,autoText3;
    private int selected,selected1;
   String[] locnames,locnames1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bus_route);

    locnames = getResources().getStringArray(R.array.Loc_names);
    locnames1 = getResources().getStringArray(R.array.Loc_names);

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
            this, android.R.layout.simple_spinner_dropdown_item,
            locnames);

    autoText1 =(AutoCompleteTextView) findViewById(R.id.actv1);
    autoText1.setAdapter(arrayAdapter);
    autoText1.setThreshold(1);
    autoText1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //autoText1.showDropDown();
            hideKeyBoard(view);//move here
            //String selection = (String) parent.getItemAtPosition(position);
            selected = position;
             }
    });
    autoText1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg0) {
            autoText3.setText(null);
            autoText1.showDropDown(); }
    });
    final ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(
            this, android.R.layout.simple_spinner_dropdown_item,
            locnames1);


    autoText2 =(AutoCompleteTextView) findViewById(R.id.actv2);
    autoText2.setAdapter(arrayAdapter1);
    autoText2.setThreshold(1);
    autoText2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //autoText2.showDropDown();
            hideKeyBoard(view);//move here
           // String selection = (String) parent.getItemAtPosition(position);
             selected1 = position;
            }
    });
    autoText2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg1) {
            autoText3.setText(null);
            autoText2.showDropDown(); }
    });


         final ImageView arrow = (ImageView) findViewById(R.id.arrow1);    
        arrow.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(final View arg3)
            {

                autoText1.setText(locnames[selected1]);
            autoText1.clearFocus();
            autoText2.setText(locnames1[selected]);

            autoText2.clearFocus();
            int temp = selected1;
           selected1 = selected;
           selected = temp;


            //arrow.setRotation(arrow.getRotation() + 180);

            mCurrRotation %= 360;
            float fromRotation = mCurrRotation;
            float toRotation = mCurrRotation += 180;

            final RotateAnimation rotateAnim = new RotateAnimation(
                    fromRotation, toRotation, arrow.getWidth()/2, arrow.getHeight()/2);

            rotateAnim.setDuration(500);
            rotateAnim.setFillAfter(true);

            arrow.startAnimation(rotateAnim);

            }

        });
    }

Also check that Rotation out, it has also no error but still nothing is happening. Thank you for your help.

EDIT:
I have figured out the rotation. It can be done by

arrow.setRotation(arrow.getRotation() + 180);

or if want to animate then,
First declare a variable
private int mCurrRotation = 0;
then

            mCurrRotation %= 360;
            float fromRotation = mCurrRotation;
            float toRotation = mCurrRotation += 180;

            final RotateAnimation rotateAnim = new RotateAnimation(
                    fromRotation, toRotation, arrow.getWidth()/2, arrow.getHeight()/2);

            rotateAnim.setDuration(1000);
            rotateAnim.setFillAfter(true);

            arrow.startAnimation(rotateAnim);

Solution

  • As there is no direct method available to set selection on AutoCompleteTextView, you need to access the array and set the text on AutoCompleteTextView.
    Here is the solution.

    public class StackOverflowActivity extends AppCompatActivity {
    AutoCompleteTextView actvFrom,actvTo;
    ImageView ivSwap;
    private static final String[] COUNTRIES = new String[] {
            "Belgium", "France", "Italy", "Germany", "Spain"
    };
    
    int fromIndex,toIndex;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stackoverflow);
        actvFrom = (AutoCompleteTextView) findViewById(R.id.actvFrom);
        actvTo = (AutoCompleteTextView) findViewById(R.id.actvTo);
        ivSwap = (ImageView) findViewById(R.id.ivSwap);
    
        final ArrayAdapter<String> fromAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,COUNTRIES);
        ArrayAdapter<String> toAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,COUNTRIES);
    
        actvFrom.setAdapter(fromAdapter);
        actvFrom.setThreshold(1);
        actvTo.setAdapter(toAdapter);
        actvTo.setThreshold(1);
    
    
        actvFrom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                fromIndex = position;
            }
        });
        actvFrom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actvFrom.showDropDown();
            }
        });
        actvTo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                toIndex = position;
            }
        });
        actvTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actvTo.showDropDown();
            }
        });
    
        ivSwap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actvFrom.setText(COUNTRIES[toIndex]);
    
                actvTo.setText(COUNTRIES[fromIndex]);
    
                int temp = toIndex;
                toIndex = fromIndex;
                fromIndex = temp;
                actvTo.clearFocus();
                actvFrom.clearFocus();
            }
        });
    
    }
    }