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
Related
I'd like to add an Item in the Menu of my App to display the version number of the app.
Of course the item should not be clickable.
And I really don't know how to do that.
I tried with "Appname\nVersion" but it does not work (just display the \n). And I could find any option to disable the click on the item.
Even if the item is disabled I can click it...
Any idea?
My code:
PopupMenu popup = new PopupMenu(this, view);
popup.setOnMenuItemClickListener(this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu, popup.getMenu());
MenuItem appVersion = popup.getMenu().findItem(R.id.appVersion);
appVersion.setTitle(String.format(getString(R.string.appVersion), BuildConfig.VERSION_NAME));
popup.show();
My menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/manageMaps"
android:title="#string/manageMaps" />
<item
android:id="#+id/appVersion"
android:title="#string/appVersion" />
<item android:id="#+id/exit"
android:title="#string/exit" />
</menu>
Did you tried
MenuItem item = menu.findItem(R.id.your_item);
item.setVisible(true);
item.setEnabled(false);
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
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'm building an Android app using the AppCompat_v7 library and I'm having troubles with the action bar.
I have an AppBarActivity which extends ActionBarActivity - I'm using the same actionbar for all of my activities. MainActivity extends AppBarActivity. I've defined four actionbar icons, two of which I'd like to be displayed at a time. I've been careful to define my own namespace, ie. app:showAsAction="always"/>
In my app, I toggle the visibility of these icons in onPrepareOptionsMenu()
MenuItem contactOn = menu.findItem(R.id.contact_toggle_button_on);
MenuItem contactOff = menu.findItem(R.id.contact_toggle_button_off);
contactOn.setVisible(!useContacts);
contactOn.setEnabled(!useContacts);
contactOff.setVisible(useContacts);
contactOff.setEnabled(useContacts);
Do I need to specify a namespace here somehow? Because it doesn't seem that these methods are doing anything, except, curiously, rearranging their names in the overflow list. Also, only three of the four buttons show up there, which I don't understand either but I expect that's a different problem.
UPDATE: posting code & xml
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inf = new MenuInflater(this);
inf.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem contactOn = menu.findItem(R.id.contact_toggle_button_on);
MenuItem contactOff = menu.findItem(R.id.contact_toggle_button_off);
contactOn.setVisible(!useContacts);
contactOn.setEnabled(!useContacts);
contactOff.setVisible(useContacts);
contactOff.setEnabled(useContacts);
MenuItem locationOn = menu.findItem(R.id.location_toggle_button_on);
MenuItem locationOff = menu.findItem(R.id.location_toggle_button_off);
locationOn.setVisible(useLocation);
locationOn.setEnabled(useLocation);
locationOff.setVisible(!useLocation);
locationOff.setEnabled(!useLocation);
return true;
}
And the XML
<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.skortchm.strangerapp.MainActivity" >
<!-- Contacts Toggle, should appear as action button -->
<item
android:id="#+id/contact_toggle_button_on"
android:icon="#drawable/ic_action_call"
android:title="#string/contact_toggle_on"
app:showAsAction="always"/>
<item
android:id="#+id/contact_toggle_button_off"
android:icon="#drawable/ic_action_call_off"
android:title="#string/contact_toggle_off"
app:showAsAction="always"/>
<!-- Location Toggle, should appear as action button -->
<item
android:id="#+id/location_toggle_button_on"
android:icon="#drawable/ic_action_location_searching"
android:title="#string/location_toggle_on"
app:showAsAction="always"/>
<item
android:id="#+id/location_toggle_button_off"
android:icon="#drawable/ic_action_location_off"
android:title="#string/location_toggle_off"
app:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<!-- Add gear icon here -->
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
You need to use getMenuInflater to inflate the menu.
You are creating an instance for MenuInflater, that is not right . you have to use the instance associated with the Activity
Change your onCreateOptionsMenu as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
I'm having trouble creating a radio-button sub-menu that is shown when a menu item from the options menu that appears when a user presses the menu button is selected. This is what I have so far:
// Expand the options menu when the user taps their menu button
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch item.getItemId() {
case R.id.expandRadioMenu:
// Show the sub-menu and collapse the initial menu
return true;
}
}
The XML of res/menu/menu.xml is as follows:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:visible="true"
android:enabled="true"
android:checkable="false"
android:icon="#drawable/ic_menu_mark"
android:id="#+id/expandRadioMenu"
android:title="Select"
android:titleCondensed="select">
<menu>
<group
android:enabled="true"
android:visible="false"
android:checkableBehavior="single"
android:id="#+id/radio">
<item
android:enabled="true"
android:visible="true"
android:title="Foo"
android:titleCondensed="Foo"
android:id="#+id/foo">
</item>
<item
android:enabled="true"
android:visible="true"
android:title="Bar"
android:titleCondensed="Bar"
android:id="#+id/bar">
</item>
</group>
</menu>
</item>
</menu>
I know it's not particularly appealing, but have you tried moving the sub menu into a separate .xml file? I've seen it work before, but it's not a terribly hopeful solution. I suppose it might further identify the problem though.