Home > DeveloperSection > Forums > Show DropDown onCreate not working in android
Pawan Shukla
Pawan Shukla

Total Post:29

Points:203
Posted on    November-07-2014 3:51 AM

 Android Autocomplete 
Ratings:


 1 Reply(s)
 645  View(s)
Rate this:
So I have an autocomplete view which shows a drop down when I type... But I want the dropdown to be shown when the avtivity starts. So I found this answer which says that using showDropDown() should work. And it does work in my case when called on any TouchListener or any other user triggered event. But it doesn't work if I directly just use it 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();



sandy taylor
sandy taylor

Total Post:46

Points:324
Posted on    November-07-2014 6:09 AM

Because when you call setAdapter it takes some time to inflate all the list items. During this time if you call showDropDown() the listview hasn't inflated yet so it won't be able to show the drop down. You could give some delay before calling the showDropDown(). But I'm not sure if this is the efficient solution as we won't be knowing for sure that how much time it is going to take to inflate the list items.

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

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

Don't want to miss updates? Please click the below button!

Follow MindStick