Android - Searchview With Auto Complete Feature Inside Action Bar
Solution 1:
For this I have Create one Layout with AutoCompleteTextView
and add it in ActionBar
its call Custom layout in ActionBar.
After that I have Create Adapter with android.R.layout.simple_dropdown_item_1line
. set it in AutoCompleteTextView
.
check Below Code:
package com.example.testapp;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
@TargetApi(11)publicclassMainActivityextendsActivity {
privatestaticfinal String[] COUNTRIES = newString[] { "Belgium",
"France", "France_", "Italy", "Germany", "Spain" };
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBaractionBar= getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
// actionBar.setDisplayShowTitleEnabled(false);// actionBar.setIcon(R.drawable.ic_action_search);LayoutInflaterinflator= (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Viewv= inflator.inflate(R.layout.actionbar, null);
actionBar.setCustomView(v);
ArrayAdapter<String> adapter = newArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextViewtextView= (AutoCompleteTextView) v
.findViewById(R.id.editText1);
textView.setAdapter(adapter);
}
}
Your Layout:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Action Bar:"android:textAppearance="?android:attr/textAppearanceMedium"android:textColor="#FFFFFF" /><AutoCompleteTextViewandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:imeOptions="actionSearch"android:inputType="textAutoComplete|textAutoCorrect"android:textColor="#FFFFFF" ><requestFocus /></AutoCompleteTextView></LinearLayout>
For More Details check this articals: one, two and three
Best Luck!
Solution 2:
I have a simple way to do this without using an autocomplete textview, just put a simple list adapter which contain a ArrayList(itemArrayList) of items, to suggest with search view.Set the adapter to your SearchAutoComplete and auto complete feature will work below is my code.With this, you don't have to use an extra Layout.
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItemsearchItem= menu.findItem(R.id.search);
SearchViewmSearchView= (SearchView) MenuItemCompat.getActionView(searchItem);
SearchAutoCompletesearchAutoComplete= (SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setTextColor(Color.WHITE);
ArrayAdapter<String> adapter = newArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, itemArrayList);
searchAutoComplete.setAdapter(adapter);
SearchManagersearchManager=
(SearchManager) getSystemService(this.SEARCH_SERVICE);
mSearchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
returntrue;
}
Clicking the suggested item the item should appear on the search view so add the below code just after you set your adapter to searchAutoComplete inside the onCreateOptionmenu(...) method.
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String searchString=(String)parent.getItemAtPosition(position);
searchAutoComplete.setText(""+searchString);
Toast.makeText(MainActivity.this, "you clicked "+searchString, Toast.LENGTH_LONG).show();
}
});
You can also see the complete code in this Link
Post a Comment for "Android - Searchview With Auto Complete Feature Inside Action Bar"