Sherlock ActionBar SearchView not working in multiple fragments - android

I am in great need of you as i am stuck in this issue from the last 4 days.
So,here is the thing....
I have one sherlock fragment activity from which i calling the six fragments inside viewpager.
Now i want to manipulate the sherlock action bar depends on the selected tab.
So for the first 3 tab i want to show the searchview and overflow menu icon.
For that what i have tried so far ::
I am in great need of you as i am stuck in this issue from the last 4 days.
So,here is the thing....
I have one sherlock fragment activity from which i calling the six fragments inside viewpager.
Now i want to manipulate the sherlock action bar depends on the selected tab.
So for the first 3 tab i want to show the searchview and overflow menu icon.
For that what i have tried so far ::
1) Called the setHasOptionsMenu(true); in every fragment.
2) Added the onCreateOptionsMenu() in every fragment. and
then i have achieved what i want.
Now i want that onClick of the searchview it should get expanded and collapse when pressed the back. So to achieve that i have added this in my first 3 fragment and also implement the puru's logic .(Create the onPrepareOptionMenu() in every fragment and add the onQueryTextChange(""); method).
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().supportInvalidateOptionsMenu();
setHasOptionsMenu(true);
}
Fragment Code ::
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_item_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search for Products");
searchView.setOnQueryTextListener(this);
searchView.setIconifiedByDefault(false);
}
#Override
public void onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
onQueryTextChange("");
}
#Override
public boolean onQueryTextSubmit(String query)
{
return true;
}
#Override
public boolean onQueryTextChange(String newText)
{
return false;
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_search"
android:icon="#drawable/abs__ic_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:title="Search Products">
</item>
<item
android:id="#+id/root_menu"
android:icon="#drawable/abs__ic_menu_moreoverflow_normal_holo_light"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/menu_Home"
android:icon="#drawable/home"
android:showAsAction="never"
android:title="Home"/>
<item
android:id="#+id/menu_favourite"
android:icon="#drawable/favourite"
android:showAsAction="never"
android:title="Favourite"/>
<item
android:id="#+id/menu_Balance"
android:icon="#drawable/balance"
android:showAsAction="never"
android:title="Balance"/>
<item
android:id="#+id/menu_logout"
android:icon="#drawable/btn_logout"
android:showAsAction="never"
android:title="Logout"/>
</menu>
</item>
</menu>
Problem :: (on Pre-HoneyComb Device)
Now when i click of the first fragment's searchview,it not get expanded,but works well for fragment 2 and fragment 3.
Steps ::
1)go to directly fragment 1,click on searchview,not expanding
2)go to directly on fragment 2 tab(not clicking onsearchview in second fragment),back to fragment 1,searchview expanding
3)go to directly on fragment 3 tab(not clicking onsearchview in third fragment),back to fragment 1,searchview expanding
Is there any issue related to the focus of searchview? or am i calling the supportInvalidateOptionsMenu() in some unusual way?
Let me know if you need anything more from my side...
Please guide me in my implementation... Any clue why this strange behaviour happens? Any suggestion/explnation will be appreciated....
Plz plz help me on this issue....
Thanks in Advance...

Related

How to add only one item from menu.xml to overflow menu in android?

I have menu xml file in menu->main.xml in which two item are available. I have two activities and i want to add this two item in overflow menu in first activity but in the second activity i want to add only one item in overflow menu.
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_refresh"
android:title="#string/action_refresh"
app:showAsAction="never"/>
<item
android:id="#+id/action_setting"
android:title="#string/action_setting"
app:showAsAction="never"/>
</menu>
So how to do that?
you can do it in two ways:
Have separate menu XMLs for both activities. one with both items and other with only one that you require.
remove the unneeded menu item in code inside 2nd activity, by overriding onCreateOptionsMenu e.g:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
menu.removeItem(R.id.action_setting);
return super.onCreateOptionsMenu(menu);
}
you could do it programmatically by hiding it...For example
MenuItem refreshIcon = menu.findItem(R.id. action_refresh);
refreshIcon.setVisible(false);
Get a reference to your menu item in onCreateOptionsMenu(menu) and update show as action as below in your second activity.
MenuItem menuitem = menu.finditem(your_id);
menuitem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
You can use the onCreateOptionsMenu that you'll have to override. You could try something like that, depending on what you need to show here:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

Android swipe views with independent actionbar on each view

I'm trying to implement swipe views with viewpager, on the samples I saw the container view has an actionbar , the problem here is that each of the classes I want to use as fragment and want to swipe between them has their own actionbar. I couldn't find a way to implement actionbar on each fragment independently , I'll appreciate if you can help me with this.
Thanks
You can change title, MenuItems or icon, but You can't change actionbar.
This way:
getActionBar().setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
Fragments has onCreateOptionsMenu() method that can be overriden. Implement a custom action bar menu on this method.
Here's a sample:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Where the R.menu.menu is a xml file containing a custom menu for that fragment.
Also dont forget to add setHasOptionsMenu(true) just incase the menu wont show up.
EDIT:
this is a sample of the xml file used for 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">
<item android:id="#+id/action_search" android:title="Search"
android:icon="#mipmap/icon_search"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_filter" android:title="Filter"
android:icon="#mipmap/icon_filter"
app:showAsAction="ifRoom" />
</menu>

ActionBar item does not shown in Option Menu

I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.
This is my onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.opt_menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
This is my opt_menu_main layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="ifRoom"/>
</menu>
I'm not using support library (my target API is 17).
If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar.
I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.
I've also tried with showAsAction="ifRoom|withText"but doesn't work.
I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?
Thanks
Try this:
app:showAsAction="always"
but don't forget to add this line at the begin of definition:
xmlns:app="http://schemas.android.com/apk/res-auto"
The Id's for menu items must be unique. The best option is to create another menu item with another Id and set its showAsAction to "never" to force it always into the overflow menu. But their is a chance if you are also displaying other primary options it might force both into the overflow in which case you will see two menu items with the same title.
For the moment I solved like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always"/>
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
So in the onOptionsItemSelected method :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add_schedule:
case R.id.menu_act_add_schedule:
//some code
break;
}
return true;
}
I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar,
since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.
The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.
This behavior can not be obtained with showAsAction="always" .
It would be great if someone could find a better solution.
Thanks anyway.

Action View in android l

I have an application with a search widget in the action bar. The application is not creating the search widget as an actionview when I switch the theme to use Material.Light in android L. When debugging I can see that the search view is null.
The menu item:
<item
android:id="#+id/search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/title"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
The creation of the menu:
Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.homescreen, menu);
return true;
}
The initialization of a variable for the search view:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setOnQueryTextListener(queryListener);
return super.onPrepareOptionsMenu(menu);
}
Any ideas?
Update 1:
Ok so I stopped the app from crashing by replacing app:actionViewClass="android.widget.SearchView" with android:actionViewClass="android.widget.SearchView". But now the search item shows up in the overflow menu rather than the action bar even though it is set to show as action always.
I had this same problem with setting it to show "always" and it ended up always being in the overflow. If you are using the support library make sure you are extending ActionBarActivity, if not, the regular Activity should work.

How to update the text of menu in android actionbar

What i have::
I have a menu in actionbar it has two sub-items in it
buffet_map_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_cart"
android:icon="#drawable/ic_action_camera"
android:showAsAction="ifRoom"
android:title="cart">
<menu>
<item
android:id="#+id/action_cart_buy"
android:icon="#drawable/ic_action_camera"
android:title="Sort by newest"/>
<item
android:id="#+id/action_cart_reserve"
android:icon="#drawable/button_reserve_selector"
android:title="Sort by rating"/>
</menu>
</item>
</menu>
What i am trying to do::
I want to update the text dynamically for the menu buttons
action_cart_buy and action_cart_reserve
How to find this resource from the Java code and update the text ?
You can use onPrepareOptionsMenu(Menu) for dynamic updates:
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
You can use it like:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem buyItem = menu.findItem(R.id.action_cart_buy);
MenuItem reserveItem = menu.findItem(R.id.action_cart_reserve);
buyItem.setTitle("New buyItem String");
reserveItem.setTitle("New reserveItem String");
return super.onPrepareOptionsMenu(menu);
}
Take a look at findItem from Menu and than MenuItem, there is a setTitle.
Edit--
There is a topic called "Changing menu items at runtime" in this article which may help you as well.
You have access to the Menu object in onCreateOptionsMenu or onPrepareOptionsMenu (better).
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_cart_buy);
item.setTitle("Any title");
return super.onPrepareOptionsMenu(menu);
}
To make the UI update your ActionBar at the right time, you have to call:
invalidateOptionsMenu();

Categories

Resources