Android: ActionBarCompat (Support library) - java

i have a problem.
I'm using support library for my app.
I add some Action items on my ActionBar by XML-file.
From docs:
https://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom|collapseActionView" />
...
</menu>
It works. But the item stays on all fragments.
I need use different action items in ActionBar.
I created a few menu-files and tried to use them on different frŠ°gments, but it doesn't work.
I see only the first Action item from class extends ActionBarActivity.
From fragment:
public class Foods extends Fragment implements View.OnClickListener, SearchView.OnQueryTextListener
...
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.showinfo_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
searchView.setQueryHint(getResources().getString(R.string.search));
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}

I have just found my mistake.
If you want ActionBar items of fragmets to work, you should add it:
http://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
...

Related

Android: Searchview opens even onClick of other menu items with different id

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

Change Menuitems when Sliding Tab

I followed this tutorial to build up a Sliding Tab application :
http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
Now my Question:
How can I change my Menu icons in my actionbar when I change to another Tab ?For example in Tab1 you can see a Search Icon and when you change to Tab2 this gets invisible but there comes a Add icon.
My code till now :
#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);
menu.add("menu_search").setIcon(R.drawable.ic_search_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("menu_contact").setIcon(R.drawable.ic_add_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
You can define menu in Fragment instead of Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_1:
// handling menu item click
break;
}
return super.onOptionsItemSelected(item);
}
Of course each Fragment will have its own menu with corresponding icons.
This is an example (menu_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item_1"
android:showAsAction="ifRoom"
android:title="#string/ic_menu_label"
android:icon="#drawable/ic_menu_icon"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>

Toggling Icons In ActionBarActivity

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;
}

Android: ActionBar (Android support library)

i have a problem with ActionBarCompat (from support library).
I have no idea how I can add a few buttons on ActionBar where I drew black circle (on screenshot) together with menu in the left of the screen.
Please, I need help!
Code of menu in ActionBar in the left of the screen.
private DrawerLayout mDrawerLayout;
private ListView mDrawer;
private ActionBarHelper mActionBar;
private ActionBarDrawerToggle mDrawerToggle;
...
linearLayout = (LinearLayout)findViewById(R.id.fragment_container);
linearLayout.setId(LAYOUT_ID);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerListener(new DDrawerListener());
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
...
fragments = new Fragment[NUMBER_OF_TABS];
mDrawer.setAdapter(new CustomAdapter(this));
mDrawer.setOnItemClickListener(new DrawerItemClickListener());
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
actionBar.setDisplayHomeAsUpEnabled(true);
mActionBar = createActionBarHelper();
mActionBar.init();
mDrawer.setBackgroundColor(Color.parseColor("#e5c391"));
mDrawer.setCacheColorHint(Color.parseColor("#e5c391"));
initArrays(this);
mDrawerToggle = new ActionBarDrawerToggle
(this, mDrawerLayout,R.drawable.ic_drawer,R.string.app_drawer_open, R.string.app_drawer_close);
if (savedInstanceState == null)
{
addFragment(0);
}
Welcome other ways too =)
Thank you!
Happy New Year!
From https://developer.android.com/training/basics/actionbar/adding-buttons.html:
Specify the Actions in XML
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory.
Add an element for each item you want to include in the action bar. For example:
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Add the Actions to the ActionBar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Respond to Action Buttons
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Please refer to this note in the Android Developer portal.

Menu inside fragment not getting called

#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
inflater.inflate(R.menu.menuitem, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Below is my onCreateView method, where i am calling
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.layout1, container, false);
}
I don't get the log statements or the menu getting called in my action-bar.
Update: I tried calling this from onCreate method of fragment, yet the menu is not shown.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/section" android:title="#string/section"
android:icon="#drawable/ic_section"
android:showAsAction="always" />
<item android:id="#+id/refresh" android:title="#string/refresh"
android:icon="#drawable/ic_refresh"
android:showAsAction="always" />
<item android:id="#+id/edit_patient" android:title="#string/edit_patient"
android:icon="#drawable/ic_editpatient"
android:showAsAction="always" />
<item android:id="#+id/about" android:title="#string/about"
android:showAsAction="never" />
<item android:id="#+id/help" android:title="#string/help"
android:showAsAction="never" />
<item android:id="#+id/signout" android:title="#string/signout"
android:showAsAction="never" />
</menu>
You'll need to make a call of setHasOptionsMenu(true); from within one of the starting lifecycle methods of the Fragment. Preferably from within onCreate(...).
In a minimalistic case the onCreate method of your Fragment looks like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Also, calling super.onCreateOptionsMenu(menu, inflater); after you have inflated your custom menu will reset the menu you just have inflated to an empty menu.
So either call:
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menuitem, menu);
}
or:
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Does", "get called");
//no super call
inflater.inflate(R.menu.menuitem, menu);
}
Also, if you're testing on a Gingerbread device, the menu might not be displayed if the hosting Activity does not contain a menu item of it's own.

Categories

Resources