I have searchView on actionbar and I here is codes for it
public class container extends AppCompatActivity {
private ListView drawerList;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private ActionBarDrawerToggle toggle;
private TextView textContainer, titleContainer;
private ImageView imageContainer;
private drawerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
toolbar = (Toolbar) findViewById(R.id.mainToolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xffffffff);
titleContainer = (TextView) findViewById(R.id.containerTitle);
textContainer = (TextView) findViewById(R.id.textContainer);
imageContainer = (ImageView) findViewById(R.id.imageContainer);
titleContainer.setText(R.string.UtrujjTitle);
textContainer.setText(R.string.UtrujjContent);
imageContainer.setImageResource(R.drawable.utrujj);
final Animation animation = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
drawerLayout = (DrawerLayout) findViewById(R.id.mainDrawer);
drawerList = (ListView) findViewById(R.id.drawerList);
final SearchView searchView = (SearchView)findViewById(R.id.searchable);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
Spannable spannable = new SpannableString(newText);
spannable.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 100, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textContainer.setText(spannable);
}
return false;
}
});
and in the end I added these overrides
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.searchable:
return false;
default:
break;
}
return super.onOptionsItemSelected(item);
}
search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/setting"
android:icon="#drawable/ic_setting"
app:showAsAction="ifRoom"
android:title="#string/setting"/>
<item android:id="#+id/searchable"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/search"/>
</menu>
when activity start I get this error in logcat
Unable to start activity ComponentInfo{com.example.tina.tibbenabvi/com.example.tina.tibbenabvi.container}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference
====================================================
This view It's a menu item so you should initialize onCreateOptionsMenu().
Example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
Spannable spannable = new SpannableString(newText);
spannable.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 100, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textContainer.setText(spannable);
}
return false;
}
});
return true;
}
EDIT
Manifest file:
<activity ... >
...
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Create res/xml/searchable.xml file:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="search..." />
Create a Searchable Activity
public class SearchResultsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
...
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
...
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
...
}
And add it in the manifest file:
<activity android:name=".SearchResultsActivity" ... >
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
</activity>
Info from:
http://developer.android.com/intl/es/training/search/setup.html
Related
I implemented a search bar in my toolbar and it currently looks like this:
But I want to change the background to white with a shadow so that it looks more like this (taken from Material Design guidelines):
How can I do this?
Here is my implementation of the SearchView:
menu_main.xml:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_search_white_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
</menu>
MainActivity.java:
public abstract class MainActivity extends AppCompatActivity {
protected Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchActivity.class)));
searchView.setQueryHint("Search");
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
setMenuItemVisibility(menu, searchItem, false);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
return true;
}
});
return true;
}
private void setMenuItemVisibility(Menu menu, MenuItem searchItem, boolean isVisible) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
Log.d(TAG, item.toString());
if (item != searchItem) {
item.setVisible(isVisible);
}
}
}
}
How can I style the SearchView to change the background color?
I am trying to add the search view to the app bar in Android. Followed by this tutorial:http://developer.android.com/training/search/setup.html#add-sv
but when i run the program, it is only a icon could be clicked, the search view did not appear, but the tutorial says it should be appear. Can anyone help me?
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/abc_search_hint"
android:icon="#drawable/ic_search_white_48dp"
app:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
MainActivity
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
return true;
}
here is the screenshot
XML :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/ic_search_white"
android:textColor="#color/white"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
final SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String queryText) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
if (mSearchTerm == null && newFilter == null) {
return true;
}
if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
return true;
}
mSearchTerm = newFilter;
mSearchQueryChanged = true;
searchText(newText); //handle this
return true;
}
});
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
return true;
}
};
MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);
return super.onCreateOptionsMenu(menu);
}
I have been trying to redirect my searchview results to another activity upon onquerysubmit but the app just shows me a blank page. I have been trying to debug it but after the onquerysubmit returns false, the debugger just keeps on displaying that the app is running.
Here is my AndroidManifest.xml
<activity
android:name=".UniversalSearchActivity"
android:label="#string/title_activity_universal_search"
android:theme="#style/AppTheme"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".UniversalSearchResultActivity"
android:label="#string/title_activity_report_error"
android:parentActivityName=".UniversalSearchActivity"
android:theme="#style/AppTheme">
</activity>
UniversalSearchActivity.java
public class UniversalSearchActivity extends AppCompatActivity {
private RelativeLayout searchSuggestionsLayout;
private ListView searchListView;
private SearchView searchView;
private UniversalSearchAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_universal_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar_universal_search);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
searchSuggestionsLayout = (RelativeLayout) findViewById(R.id.search_suggestions_container);
searchListView = (ListView) findViewById(R.id.search_suggestions_list);
adapter = new UniversalSearchAdapter(this, this.getSearch());
searchListView.setAdapter(adapter);
searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You clicked an item in the list view!",
Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
searchItem.expandActionView();
SearchManager searchManager =
(SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setSearchableInfo(
searchManager.getSearchableInfo(new ComponentName(this,
UniversalSearchResultActivity.class)));
searchView.setQueryHint(getString(R.string.placeholder_universal_search));
searchView.setSubmitButtonEnabled(true);
searchView.setIconified(false);
searchView.requestFocus();
menu.add("Search")
.setIcon(R.drawable.ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
if(query == "") {
Toast.makeText(getApplicationContext(), "Please type a hint", Toast.LENGTH_SHORT)
.show();
} else {
Intent intent = new Intent(getApplicationContext(),
UniversalSearchResultActivity.class);
startActivity(intent);
}
return false;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
public ArrayList<UniversalSearch> getSearch() {
ArrayList<UniversalSearch> searches = new ArrayList<UniversalSearch>();
UniversalSearch universalSearch;
for (int i=0; i<names.length; i++) {
universalSearch = new UniversalSearch(names[i], address[i], images[i]);
searches.add(universalSearch);
}
return searches;
}
}
UniversalSearchResultActivity.java
public class UniversalSearchResultActivity extends AppCompatActivity {
private RelativeLayout searchResultLayout;
private ListView searchResultListView;
private UniversalSearchAdapter universalSearchAdapter;
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_universal_search_result);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar_universal_search_result);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Toast.makeText(getApplicationContext(), "I'm heeeere!!!", Toast.LENGTH_SHORT).show();
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Intent resultIntent = new Intent(getApplicationContext(),
UniversalSearchResultActivity.class);
resultIntent.putExtra(SearchManager.QUERY, query);
}
}
}
The searchview works fine when the onquerytextchange is used but in the case of onquerytextsubmit, it doesn't. Please tell me what I am doing wrong. Thank you!
The code on which I am working, is using a Toolbar and inflating a menu.
Here is the code
private Toolbar mToolbar;
mToolbar.inflateMenu(R.menu.chat_screen_menu);
setupMenu ();
private void setupMenu ()
{
mMenu = mToolbar.getMenu();
if (mMenu != null)
{
if (mChatPager != null && mChatPager.getCurrentItem() > 0)
{
mMenu.setGroupVisible(R.id.menu_group_chats, true);
mMenu.setGroupVisible(R.id.menu_group_contacts, false);
}
else
{
mMenu.setGroupVisible(R.id.menu_group_chats, false);
mMenu.setGroupVisible(R.id.menu_group_contacts, true);
mMenu.setGroupVisible(R.id.menu_group_otr_verified,false);
mMenu.setGroupVisible(R.id.menu_group_otr_unverified,false);
mMenu.setGroupVisible(R.id.menu_group_otr_off,false);
}
}
mToolbar.setOnMenuItemClickListener(new OnMenuItemClickListener ()
{
..........
}
}
But now, they require a Search button in the tool_bar.
I managed to put it, I followed a guide here
When I try to write something to search, the toast I had put to test the listener never shown.
which indicates listener is not working
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.chat_screen_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_menu_search));
final Toast toast = new Toast(mApp);
if (mSearchView != null )
{
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String newText)
{
mSearchString = newText;
//doFilterAsync(mSearchString);
toast.makeText(getApplicationContext(), "Test1", Toast.LENGTH_LONG).show();
return true;
}
public boolean onQueryTextSubmit(String query)
{
mSearchString = query;
//doFilterAsync(mSearchString);
toast.makeText(getApplicationContext(), "Test2", Toast.LENGTH_LONG).show();
return true;
}
};
mSearchView.setOnQueryTextListener(queryTextListener);
}
return true;
}
You have to use Appcompat library for that. Which is used like below:
dashboard.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:title="Search"/>
</menu>
Activity file (in Java):
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
Activity file (in Kotlin):
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_search, menu)
val searchItem: MenuItem? = menu?.findItem(R.id.action_search)
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
val searchView: SearchView? = searchItem?.actionView as SearchView
searchView?.setSearchableInfo(searchManager.getSearchableInfo(componentName))
return super.onCreateOptionsMenu(menu)
}
manifest file:
<meta-data
android:name="android.app.default_searchable"
android:value="com.apkgetter.SearchResultsActivity" />
<activity
android:name="com.apkgetter.SearchResultsActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
searchable xml file:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name" />
And at last, your SearchResultsActivity class code. for showing result of your search.
If you would like to setup the search facility inside your Fragment, just add these few lines:
Step 1 - Add the search field to you toolbar:
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
Step 2 - Add the logic to your onCreateOptionsMenu()
import android.support.v7.widget.SearchView; // not the default !
#Override
public boolean onCreateOptionsMenu( Menu menu) {
getMenuInflater().inflate( R.menu.main, menu);
MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Toast like print
UserFeedback.show( "SearchOnQueryTextSubmit: " + query);
if( ! searchView.isIconified()) {
searchView.setIconified(true);
}
myActionMenuItem.collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String s) {
// UserFeedback.show( "SearchOnQueryTextChanged: " + s);
return false;
}
});
return true;
}
If you want to add it directly in the toolbar.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SearchView
android:id="#+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:queryHint="Search"
android:layout_centerHorizontal="true" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Integrating SearchView with RecyclerView
1) Add SearchView Item in Menu
SearchView can be added as actionView in menu using
app:useActionClass = "android.support.v7.widget.SearchView"
.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="rohksin.com.searchviewdemo.MainActivity">
<item
android:id="#+id/searchBar"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
</menu>
2) Implement SearchView.OnQueryTextListener in your Activity
SearchView.OnQueryTextListener has two abstract methods. So your activity skeleton would now look like this after implementing SearchView text listener.
YourActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
public boolean onQueryTextSubmit(String query)
public boolean onQueryTextChange(String newText)
}
3) Set up SerchView Hint text, listener etc
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.searchBar);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search People");
searchView.setOnQueryTextListener(this);
searchView.setIconified(false);
return true;
}
4) Implement SearchView.OnQueryTextListener
This is how you can implement abstract methods of the listener.
#Override
public boolean onQueryTextSubmit(String query) {
// This method can be used when a query is submitted eg. creating search history using SQLite DB
Toast.makeText(this, "Query Inserted", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return true;
}
5) Write a filter method in your RecyclerView Adapter.
You can come up with your own logic based on your requirement. Here is the sample code snippet to show the list of Name which contains the text typed in the SearchView.
public void filter(String queryText)
{
list.clear();
if(queryText.isEmpty())
{
list.addAll(copyList);
}
else
{
for(String name: copyList)
{
if(name.toLowerCase().contains(queryText.toLowerCase()))
{
list.add(name);
}
}
}
notifyDataSetChanged();
}
Full working code sample can be found > HERE
You can also check out the code on SearchView with an SQLite database in this Music App
Implementing the SearchView without the use of the menu.xml file and open through button
In your Activity we need to use the method of the onCreateOptionsMenumethod in which we will programmatically inflate the SearchView
private MenuItem searchMenu;
private String mSearchString="";
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
SearchManager searchManager = (SearchManager) StoreActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView mSearchView = new SearchView(getSupportActionBar().getThemedContext());
mSearchView.setQueryHint(getString(R.string.prompt_search)); /// YOUR HINT MESSAGE
mSearchView.setMaxWidth(Integer.MAX_VALUE);
searchMenu = menu.add("searchMenu").setVisible(false).setActionView(mSearchView);
searchMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
assert searchManager != null;
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
mSearchString = newText;
return true;
}
public boolean onQueryTextSubmit(String query) {
mSearchString = query;
searchMenu.collapseActionView();
return true;
}
};
mSearchView.setOnQueryTextListener(queryTextListener);
return true;
}
And in your Activity class, you can open the SearchView on any button click on toolbar like below
YOUR_BUTTON.setOnClickListener(view -> {
searchMenu.expandActionView();
});
I search and implement so much code but that not worked for me .
Then I implement custom toolbar into my XML file and then inside toolbar tag I use searchview tag.
Hope it work for you.
I've noticed that when I'm using action bar in my apps, there is a blank screen showing for couple of seconds when I start the app, then the activity layout is loaded. Does anyone know how can I resolve this?
If I dont use action bar this blank scren is now showing
Here is an example acyivity that I have:
public class Login extends Activity implements OnClickListener {
private Button signInFacebook;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
initWidgets();
}
private void initWidgets() {
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.nav_bg));
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
signInFacebook = (Button) findViewById(R.id.login_fb);
signInFacebook.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.login_fb:
intent = new Intent(this, ChangePassword.class);
startActivity(intent);
break;
}
}
}
I dont get any erros or warnings
Just add this line in your launcher activity
android:theme="#android:style/Theme.NoTitleBar" like below example
<activity
android:name="com.example.app.Login"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Use requestWindowFeature
public class Login extends Activity implements OnClickListener {
private Button signInFacebook;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
initWidgets();
}
private void initWidgets() {
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.nav_bg));
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
signInFacebook = (Button) findViewById(R.id.login_fb);
signInFacebook.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.login_fb:
intent = new Intent(this, ChangePassword.class);
startActivity(intent);
break;
}
}
}
Hope it is useful.
For Proper action bar with all functionality like search you can have the below code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search_action_provider);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
MenuItemCompat.setOnActionExpandListener(searchItem, new OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
});
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
if (adapter != null) {
adapter.getFilter().filter(newText);
}
return true;
}
public boolean onQueryTextSubmit(String query) {
if (adapter != null) {
adapter.getFilter().filter(query);
}
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
In serch.xml you can have
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/search_action_provider"
android:icon="#drawable/abc_ic_search_api_holo_light"
android:title="#string/abc_action_bar_home_description"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="ifRoom|collapseActionView"/>