I have and autocompletetextview
set inside a toolbar. when user provides search query i have an adapter(with some dummy data added for testing) that shows the results based on search query.
The problem is with adapter width as it is set to autocompletetextview
width.
But here i want the adapter width to set to toolbar width and a margin between toolbar and adapter.
This is the code i have written:
searchView = (AutoCompleteTextView) findViewById(R.id.searchView);
SearchAdapter searchAdapter = new SearchAdapter(NavigationDrawerActivity.this,searchResults,searchView);
searchView.setAdapter(searchAdapter);
Here is my adapter:
public class SearchAdapter extends ArrayAdapter<SearchResult> {
private boolean mAnimate;
private AutoCompleteTextView mSearch;
public SearchAdapter(Context context, List<SearchResult> options, AutoCompleteTextView search) {
super(context, 0, options);
mSearch = search;
}
int count = 0;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchResult option = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.search_option, parent, false);
/* if (true) {
Animation anim = AnimationUtils.loadAnimation(getContext(),
R.anim.anim_down);
anim.setDuration(400);
convertView.startAnimation(anim);
if (count == this.getCount()) {
mAnimate = false;
}
count++;
}*/
}
View border = convertView.findViewById(R.id.border);
if (position == 0) {
border.setVisibility(View.VISIBLE);
} else {
border.setVisibility(View.GONE);
}
final TextView title = (TextView) convertView
.findViewById(R.id.title);
title.setText(option.title);
ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
icon.setImageDrawable(option.icon);
ImageView up = (ImageView) convertView.findViewById(R.id.up);
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSearch.setText(title.getText().toString());
mSearch.setSelection(mSearch.getText().length());
}
});
return convertView;
}
}
Here is my adapter row layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@android:color/white"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:paddingLeft="3dp"
android:adjustViewBounds="true"
android:cropToPadding="true" />
<TextView
android:id="@+id/title"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#212121"
android:layout_centerVertical="true"
android:layout_marginLeft="62dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/up"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/up"
android:layout_alignParentRight="true"
android:layout_width="32dp"
android:layout_margin="10dp"
android:layout_marginRight="24dp"
android:layout_gravity="center|right"
android:layout_height="32dp"
android:src="@drawable/ic_up" />
<View
android:id="@+id/border"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E8E8E8" />
</RelativeLayout>
With the above code i am getting like this: Need the adapter aligned to toolbar width I have tried setting adapter width like this:
Rect screenSize = new Rect();
getWindowManager().getDefaultDisplay().getRectSize(screenSize);
// screen width
int screenWidth = screenSize.width();
int adapterMargin = (int)getResources().getDimension(R.dimen.adapter_margin);
searchView.setDropDownWidth(screenWidth - adapterMargin);
But the problem here is i'm getting not getting right margin as shown below: screen with adapter width set
Please help me on how to tackle this problem.
It looks like you want to set adapter width to be set to toolbar width add the following line to your code it will work:
searchView.setDropDownAnchor(toolbar.getId());
Comment below if you have any further doubts