I want to change the Settings logo to a Search logo. I tried doing the following changes in the xml but it doesn't seem to work. Can anyone suggest me what to do?
My xml is as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:icon="#android:drawable/btn_minus"
android:title="#android:string/search_go"/>
</menu>
1) Go to : https://developer.android.com/design/downloads/index.html
2) Download "Action Bar Icon Pack"
3) Put ic_action_search in your drawable directory
4) Then : android:icon="#drawable/ic_action_search"
if it doesn't work try : your_app_name:showAsAction="always"
As you can read in the documentation :
Notice that the showAsAction attribute above uses a custom namespace
defined in the tag. This is necessary when using any XML
attributes defined by the support library, because these attributes do
not exist in the Android framework on older devices. So you must use
your own namespace as a prefix for all attributes defined by the
support library.
Use this if you ar using action bar
<item
android:id="#+id/grid_default_search"
android:icon="#android:drawable/ic_menu_search"
android:title="search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
/>
<!-- other items or whatever -->
In your activity
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.grid_default, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.grid_default_search).getActionView();
searchView.setOnQueryTextListener(queryListener);
}
final private OnQueryTextListener queryListener = new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
//Logic
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
//Logic
return false;
}
};
Related
Short: When I click on my SearchViewIcon, the SearchView doesn't collapse/expand.
Long:
I"m using a SearchView to filter a RecyclerView in a Fragment that is in my MainActivity.
When I click on the SearchViewIcon (SearchView is iconified by default). I open the tab with the correct Fragment with this code:
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(2, false);
}
});
The Tab with the correct Fragment is opened like expected. Also the text input is shown, but the SearchView stays iconified. (See picture below.)
Application
My SearchView in XML:
<item
android:id="#+id/action_search"
android:orderInCategory="1"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/menu_item_search_title"
app:showAsAction="ifRoom"
app:queryHint="Search name or function"
app:actionViewClass="android.support.v7.widget.SearchView" />
Things I already tried:
Setting my showAsAction to always or ifRoom|collapseActionView
app:showAsAction="always"
app:showAsAction="ifRoom|collapseActionView"
Request focus on my SearchView:
searchView.requestFocus();
Expanding my SearchViewItem:
MenuItem searchViewItem = menu.findItem(R.id.action_search);
searchViewItem.expandActionView();
But non of these things worked...
EDIT
Like the title says, if I click the SearchViewIcon again, the SearchView does expand.
I've also encountered a similar problem, and was able to solve it.
The main problems I've needed to solve were:
searchView was not expanding by default
the search EditText didn't get a focus until a second click
even after solving (2), the keyboard didn't show up until a second click
solutions:
in menu.xml need to define app:showAsAction as "collapseActionView" + in java code need also to call searchView.setIconifiedByDefault(false) for the expansion to take priority over the iconification (that is - when the icon is pressed, expand and don't stay in icon mode)
add a MenuItem.OnActionExpandListener for your search menu-item object and use a handler to post a runnable that will request focus to your search-view object.
(why runnable? because requesting focus when the menu is still not fully inflated is not guaranteed to work. When requesting focus in a handler-runnable, I'm making sure the focus request happens AFTER all the work in the onCreateOptionsMenu() is ready and finished.
in The same defined runnable from (2), also ask android OS to show the keyboard.
complete code solving all those problems:
menu.xml:
<item
android:id="#+id/searchContacts"
android:icon="#drawable/ic_search_white_24dp"
android:title="search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.widget.SearchView"
/>
<!-- and of course your other menu items -->
searchable configuration:
Need to create such xml file.
right-click your res folder and choose new --> android resource file. put whatever you want as file name ("searchable" will work ok, for example), and choose XML as resource type.
Then copy & paste this code in the created file (replace the hint string with your own):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="put here your hint string to be shown"
/>
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
implementSearch(menu);
return true;
}
private void implementSearch(final Menu menu) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final MenuItem searchMenuItem = menu.findItem(R.id.searchContacts);
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener(){
#Override
public boolean onMenuItemActionExpand(MenuItem item){
// the search view is now open. add your logic if you want
new Handler().post(new Runnable() {
#Override
public void run() {
searchView.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) { // it's never null. I've added this line just to make the compiler happy
imm.showSoftInput(searchView.findFocus(), 0);
}
}
});
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item){
// the search view is closing. add your logic if you want
return true;
}
});
}
Also, if you want to use your own callbacks for the search's text change and submitted (instead of Android intents), in the implementSearch() method add a call to searchMenuItem.setOnActionExpandListener(new SearchView.OnQueryTextListener({...})).
view this SO question for more details
The simplest solution is to use always option for app:showAsAction:
app:showAsAction="always"
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView"
/>
I'm having big problems opening and closing the keyboard using a searchview in my toolbar as actionbar. When I hit the searchbutton it expends as a action and the edit text appears, but it has no focus and they keyboards doesnt open. I have to manually click on the edittext to open the keyboard.
The same for closing the keyboard. When i hit the close searchaction button, they keyboard wont close.
I'm sure that this already worked without using the toolbar.... but I'm not able to fix it with toolbar.
Does anybody have an idea ?
This is 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="com.example.toolbar.MainActivity" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView"
android:title="#string/action_search"/>
<item
android:id="#+id/action_contact"
android:icon="#drawable/ic_action_settings"
app:showAsAction="ifRoom"
android:title="#string/action_settings"/>
And this is my onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// Do not iconify the widget;expand it by default
searchView.setIconifiedByDefault(false);
setSearchIcons();
return super.onCreateOptionsMenu(menu);
}
Thanks for any advice.
Cheers!
EDIT1:
Well obviously after 10 hours of struggle I ask this question and 30min later I find a solution.
It was the app:showAsAction="ifRoom|collapseActionView"
collapseActionView. Deleting it did the trick.
But now I have another problem. When the search view expands the settings button looks very bad. The settings button is half visible/ half cut out.
On API 21 Nexus 4 Android 5 the search view cuts out the settings button.
On a THL 5000 with Android 4.4.2 the search view seems to work fine, but the icons are not white as on the Nexus but grey.
Any idea?
If your searchView is not always in expanded state, then you can try below code:
mSearchView.setIconifiedByDefault(true);
mMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Add code here
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Add code here
return false;
}
});
To hide keyboard after clicking enter/done on keyboard:
#Override public boolean onQueryTextSubmit(String query) {
mSearchView.clearFocus();
return true;
}
I'm busy with changing my existing app to material design, but i'm having trouble with the menu items inside the ActionBar.
First of all in some activities i'm using a searchview, But i have read that you need to use a custom prefix inside the searchview menu xml. So i did:
<?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:icon="#drawable/ic_action_search_light"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
But everytime I'm getting the error:
"Should use android:showAsAction when not using the appcompat library"
if i change app:showAsAction="ifRoom|collapseAction" to android:showAsAction="ifRoom|collapseActionView"
I'm not getting a searchview that take the whole lenght of the actionbar, just some strange square in the right corner where the items are placed. Here is my code of the onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchview, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView search = (SearchView) MenuItemCompat.getActionView(searchItem);
if(search != null)
{
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
try {
new LoadResultsTask().execute(URLEncoder.encode(query, "UTF-8"));
} catch (UnsupportedEncodingException e) {
Toast.makeText(IndexerOverviewActivity.this, "Could not parse Query", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return true;
}
#Override
public boolean onQueryTextChange(String search) {
return true;
}
});
}
search.setIconifiedByDefault(false);
return super.onCreateOptionsMenu(menu);
}
I noticed aswell that all my actionbar menu items that has android:showsAsAction="always" are shown as android:showsAsAction="never" when i start my application on my device. So my first guess is it has everything to do with the prefix to use in the menu xml files.
Pffff, after just a single "clean" it works perfectly now...
It took me freaking 2 hours to fix this.
I have an ActionBar using ActionBar Sherlock where I need it to display overflow because I have more actions than room. But, it doesn't show the overflow icon. Here is my configuration:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/action_search"
android:title="#string/menu_search"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_library"
android:icon="#drawable/hardware_headphones"
android:title="#string/my_music"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_downloads"
android:icon="#drawable/av_download"
android:title="#string/downloads"
android:showAsAction="ifRoom|withText"/>
</menu>
And here is code to set it up:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.shopping_menu, menu);
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
searchMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(ShopActivity.this, SearchDialog.class));
return false;
}
});
MenuItem downloadMenuItem = menu.findItem(R.id.menu_downloads);
downloadMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity( new Intent(ShopActivity.this, DownloadQueueActivity.class) );
return false;
}
});
MenuItem myMusicItem = menu.findItem(R.id.menu_library);
myMusicItem.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(ShopActivity.this, MyMusicActivity.class));
return false;
}
});
return true;
}
I've looked over the demos in ActionBar Sherlock, but I can't tell what they do differently to get the overflow than what I'm doing. So what's happening here why its not showing?
If you have a physical menu key, the overflow indicator does not show. That is a behaviour by design. See here for more details on another question asked.
Hmm I think there are two issues here. First, as t0mm13b states, if the device has a physical menu key, the overflow indicator does NOT show. This is by design. Although in my experience, it doesn't apply to every device (unfortunately...)
The second issue is that, if you want to force an item to the overflow, you need to set the showAsAction to "never". Otherwise, the only elements that appear in the overflow are ones that simply "don't fit" in the action bar. And given that you have 3 items that you want to display with text.. you're pretty much guranteed to have at least one overflow item, and therefore the overflow icon (with the caveat of the first paragraph)
Try by changing the android:showAsAction tag to app:showAsAction according to the Android guide in the menu_main.xml file as shown below
Add this line if not present
menu xmlns:app="http://schemas.android.com/apk/res-auto"
<item android:id="#+id/action_search"
android:icon="#drawable/search_icon"
android:title="#string/action_search"
app:showAsAction="always"/> <!--change here android: to app:-->
<item android:id="#+id/action_location"
android:icon="#drawable/location_icon"
android:title="#string/action_locate"
app:showAsAction="always"/>
</menu>
I'm a begginer in android development and I have a problem. My first menu has no separator lines in portreit and landscape modes. Also I've no separator in action bar in portreit mode while I have it in landscape one. Separator for action bar is a pic 1x32 placed at ./drawable. At the same time under AVDs and Android-x86 (VirtualBox) is fine. Please, see the fig1: .
Thanks in advance!
Update 1:
Create and show menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.main_menu_sync:
Toast("Refresh menu");
return true;
case R.id.main_menu_more:
Toast("More menu");
return true;
// ...
default:
return super.onOptionsItemSelected(item);
}
}
Menu layout (./res/menu/main_menu.xml):
<item
android:id="#+id/main_menu_undo"
android:icon="#drawable/ic_menu_undo"
android:title="Undo"/>
<item
android:id="#+id/main_menu_multiselect"
android:icon="#drawable/ic_menu_multiselect"
android:title="Multiselect"/>
<item
android:id="#+id/main_menu_sync"
android:icon="#drawable/ic_menu_refresh"
android:title="Sync"/>
<item
android:id="#+id/main_menu_more"
android:icon="#drawable/ic_menu_more"
android:title="More">
<menu>
<item
android:id="#+id/main_menu_preferences"
android:icon="#drawable/ic_menu_preferences"
android:title="Preferences"/>
<item
android:id="#+id/main_menu_tab_order"
android:icon="#drawable/ic_menu_tab_order"
android:title="Tab order"/>
</menu>
</item>
If your device is QVGA and you set the anyDensity attribute to false, some separators on the UI will be missing.
See more details at
http://developer.android.com/guide/practices/screens_support.html
I think your screen is supporting the view that you are using. You should add the support screen tag to your manifest.xml file. It works for me. Or may be you can check the following link to get some more info.