Search code examples
androidcustom-controlsautocompletetextview

Custom AppCompatAutoCompleteTextView dropdown size changes


I have a requirement to show dropdown in the AutoCompleteTextView as soon as it gains focus. For this i created a class extending AppCompatAutoCompleteTextView.

public class DropDownAutoComplete extends AppCompatAutoCompleteTextView 
    implements View.OnFocusChangeListener , View.OnClickListener
{
        @Override
        public boolean enoughToFilter() {
            return true;
        }

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
            {
                if(getText().length()==0)
                    showDropDown();
            }
        }

        @Override
        public void onClick(View v) {
            if(getText().length()==0)
                showDropDown();
        }
}

in addition i used a custom layout for the dropdown item by supplying it in the adapter.

customAdapter.setAdapter(new ArrayAdapter<String>(AddExpense.this,
    R.layout.custom_autocomplete_dropdown_item, stringArray)); 



<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_divider"
    android:textColor="@color/autocomplete_text_color"
    android:textAppearance="?android:attr/textAppearanceLarge"
    style="?android:attr/dropDownItemStyle"
    android:maxLines="1"
    android:padding="8dp"
/>

background_divider.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/black" />
        </shape>
    </item>
    <item android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>

During the onFocus Event the dropdown loads properly.But after wards when i click the autocompletetextview when the dropdown is visible, the dropdown reloads but the layout is totally messed up like it loads some default dropdown item, instead of the one it designed. How can i fix this. I would really appreciate any help.

This is the DropDown i Expect taht appears onFocusEvent

This is what happens when i click the view when dropdown is visible


Solution

  • The Solution that i have used is as mentioned in my comment above. Just calling the code in the below order seems to reload the view.

    dismissDropDown();
    showDropDown();
    

    I can guess that this forces a redraw hence the UI is drawn properly. I am leaving this as an answer in case some one else is in my situation. Please feel free to edit.