Android showDropDown onCreate not working

So, I have an autocomplete view that shows a drop as you type ... But I want the dropdown to show when avtivity starts. So I found this answer , which says using showDropDown()

should work. And it works in my case when called on any TouchListener or any other user-initiated event. But it doesn't work if I directly use it only in onCreate () ... The following code in my onCreate () works

    final AutoCompleteTextView actv = (AutoCompleteTextView)findViewById(R.id.autoCompleteUserName);
    String[] users = getResources().getStringArray(R.array.users);
    ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this,R.layout.compose_ac_list_item,users);
    actv.setAdapter(adapter);

    actv.setOnTouchListener(new View.OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // WORKS IF USED ON TOUCH
            actv.showDropDown();
              return false;
        }
    });

      

And the following doesn't work

    final AutoCompleteTextView actv = (AutoCompleteTextView)findViewById(R.id.autoCompleteUserName);
    String[] users = getResources().getStringArray(R.array.users);
    ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this,R.layout.compose_ac_list_item,users);
    actv.setAdapter(adapter);

    actv.showDropDown();

      

+3


source to share


1 answer


Because when you call setAdapter

it takes some time to inflate all the list items. During this time, if you call showDropDown()

, the list is not yet inflated, so it won't be able to display the dropdown. You can give some delay before calling showDropDown()

. But I'm not sure if this is an efficient solution, since we won't know exactly how long it will take to inflate the list items.



    actv.setAdapter(adapter);
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            actv.showDropDown();
        }
    }, 500);

      

+13


source







All Articles