I want to make my SearchView full width something like Whatsapp. This is my result.
Example
And this is my code
<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:icon="?searchAppBarButton"
android:title="#string/buscar"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.agregar_servicio_menu, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.clearFocus();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint(getString(R.string.buscar_empresa));
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
and style
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="searchHintIcon">#null</item>
<item name="android:maxWidth">1000dp</item>
</style>
this is my wanted result for the widht of the SearchView
Example2
Use this library for creating search view like whatsapp "com.miguelcatalan:materialsearchview:1.4.0"
Link of this library MaterialSearchView
Related
I am currently working on an app that i implemented a toolbar and a search functionality on the toolbar with a searchview to filter list views which is working fine.
I also have a navigation drawer whereby i also want one of its items to do exactly what the search functionality is doing on the toolbar. How do implement an intent to lead me to SearchViewon toolbar when searchitem is clicked from navigation drawer.
menu_main for toolbar
<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=".MainActivity">
<item
android:id="#+id/viewMode"
android:orderInCategory="100"
app:showAsAction="never"
android:icon="#drawable/ic_view_compact_black_24dp"
android:title="#string/view" />
<item
// my search functionality
android:id="#+id/search"
android:orderInCategory="100"
app:actionViewClass= "android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_search"
android:title="#string/search" />
</menu>
activity_navdrawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_notes"
android:icon="#drawable/ic_note_add_black_24dp"
android:title="#string/first_layout" />
<item
// search functionality which should lead me to searchview on toolbar
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/third_layout" />
</group>
<item android:title="#string/app_communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_share_black_24dp"
android:title="#string/app_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_email_black_24dp"
android:title="#string/app_send" />
</menu>
</item>
</menu>
from main activity
public boolean onNavigationItemSelected(MenuItem item) {
// Handling navigation view item clicks here.
int id = item.getItemId();
if(id == R.id.nav_notes) {
Intent noteIntent = new Intent(this, MainActivity.class);
startActivity(noteIntent);
} else if(id == R.id.nav_search) {
MenuItem searchMenuItem = mOptionsMenu.findItem(R.id.search);
searchMenuItem.expandActionView();
// what should i implement here?
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public boolean onCreateOptionsMenu(`final` Menu menu) {
mOptionsMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
ArrayAdapter<Note> arrayAdapter = (ArrayAdapter<Note>) mListNotes.getAdapter();
arrayAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
Try the following code below:
private Menu mOptionsMenu;
...
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
mOptionsMenu = menu;
...
}
...
public boolean onNavigationItemSelected(MenuItem item) {
...
} else if(id == R.id.nav_search) {
MenuItem searchMenuItem = mOptionsMenu.findItem(R.id.menu_search);
searchMenuItem.expandActionView();
}
...
Add <category android:name="android.intent.category.DEFAULT" /> to your search activity in manifest. Then do this:
else if(id == R.id.nav_search) {
startActivity(new Intent(Intent.ACTION_SEARCH));
}
If this works, courtesy - Start Activity Using Custom Action
I'm working in my app and have a searchview inside toolbar but the hint has a small padding after the cursor.
How can I remove the padding?
Here is my searchview:
and I would like that:
here is my onCreateOptionsMenu:
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem item=menu.findItem(R.id.procura);
Drawable drawable = menu.findItem(R.id.procura).getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
}
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
try {
Field mDrawable = SearchView.class.getDeclaredField("mSearchHintIcon");
mDrawable.setAccessible(true);
Drawable drw = (Drawable) mDrawable.get(searchView);
drw.setBounds(0, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
My styles:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">#color/colorPrimary</item>
<item name="queryBackground">#color/colorPrimary</item>
<item name="android:colorFocusedHighlight">#color/colorAccent</item>
</style>
My menu:
<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=".MainActivity">
<item
android:id="#+id/procura"
android:title="#string/app_hint"
android:icon="#mipmap/ic_search_black_24dp"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
I found the solution for my problem.
Just add this in the styles:
<item name="searchHintIcon">#null</item>
The styles now is that:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">#color/colorPrimary</item>
<item name="queryBackground">#color/colorPrimary</item>
<item name="searchHintIcon">#null</item>
<item name="android:colorFocusedHighlight">#color/colorAccent</item>
</style>
And onCreateOptionsMenu now is that:
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
MenuItem item=menu.findItem(R.id.procura);
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
return true;
My SearchView is working fine.. but it is accepting the same behavior when other menuItems are clicked. When I click cart icon then too SearchView is getting called. How do I avoid that?
Here is my code for the menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<!-- Search Widget -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/action_wishlist"
android:icon="#drawable/ic_shopping_cart"
android:title="#string/action_cart"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/action_cart"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
and code for SearchView:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem= menu.findItem(R.id.action_search);
SearchView searchView =(SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setIconifiedByDefault(false);
return super.onCreateOptionsMenu(menu);
}
I want a different behavior when cart icon is clicked but it just does the same action as when clicked on the search icon. I want to limit the SearchView only to the SearchView icon. On the cart Icon i want to perform something else. Please help
I just had to remove:
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
from other menu items which are not SearchView
I am learning about action bar in Android Studio from Android Hive. Unfortunatelly i use v7 action bar and androidhive use built-in action bar that might makes a little different. The problem is at search view part.
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
System.out.println("Search Manager : "+searchManager);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
System.out.println("Search View : "+searchView);
//searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
mainmenu.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:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
The command line says that the searchView is null.
I have changed android:actionViewClass to app:actionViewClass but it said android.widget.SearchView cannot be cast to android.support.v7.widget.SearchView
For full code, you can see here
How to fix?
you should use in
app:actionViewClass="android.support.v7.widget.SearchView"
instead of
android:actionViewClass="android.widget.SearchView"
I hope This would be help you.
I have a search view in my activity. When I am trying to attach an OnQueryTextListener, it gives me a NullPointerException. Its Java code is as follows
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.allpurpose, menu);
MenuItem searchItem = menu.findItem(R.id.apmenu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {return true;}
public boolean onQueryTextSubmit(String query) {return true;}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
XML Code:
<?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/apmenu_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/srch"
android:showAsAction="ifRoom"
android:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
All "Compat" items are used to provide backward compatibility. So in this case MenuItemCompat supports action bar items for versions above 7 (instead of the normal 11). In the java code, if there is MenuItemCompat, then in the xml too there must be a "Compat" item. Now, the "app" namespace supports these "Compat" items. So change the xml like this:
<?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/apmenu_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/srch"
app:showAsAction="ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>