Reputation: 113
I have implemented SearchView
in the ActionBar
, in which I'm setting a SimpleCursorAdapter
with some random data using setSuggestionAdapter()
.
The problem I'm facing is that I'm unable to get the setDropDownWidth()
working on the SearchView
's custom AutoCompleteTextView
. I'm able to change the background resource and almost everything, except horizontaloffset and dropdownwidth. Here's the code:
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
searchAutoComplete.setDropDownAnchor(R.id.anchorView);
searchAutoComplete.setDropDownWidth(LinearLayout.LayoutParams.MATCH_PARENT);
searchAutoComplete.setDropDownBackgroundResource(R.drawable.white_bg);
searchAutoComplete.setMinimumWidth(AppUtil.getDeviceWidth(activityContext));
Please help me out! The device I've been testing on is Nexus 4 with Lollipop.
Edit:
I'm confused as to why setDropDownHeight()
is working but setDropDownWidth()
is not working, even while setting a int value (Say 50). The width of the dropdown remains intact. I'm neither able to decrease nor increase the width of the dropdown. Is it a bug in the ActionBar with AppCompat Theme?
Edit 2:
I'm able to get the method setDropDownWidth()
working. I used View.onLayoutChangeListener
on the Anchor View and in the onLayout()
inner method, I set the width of the drop down. It works fine now. MY only question now is why we have to wait for layout change listener of the anchor view to set the drop down width?
Upvotes: 3
Views: 1140
Reputation: 258
From documentation
public void setDropDownWidth (int width) Added in API level 3 Sets the current width for the auto-complete drop down list. This can be a fixed width, or MATCH_PARENT to fill the screen, or WRAP_CONTENT to fit the width of its anchor view
I had the same problem & i think this is because it changes the current withd so it needs to be invoked every time the TextChangeListener is being invoked.So I placed the method in the text watcher & it worked perfectly:
auto.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
// auto.setDropDownWidth(240);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
auto.setDropDownWidth(240);
}
Upvotes: 1