I'm making some application with action bar. But when I try to run my application on my emulator I get only optional menu with items from my activity_main_actions.xml. If anybody help me what I need to do fix my problem and to get Action Bar instead of Optional Menu when I try to rum my application. This is my code:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_launcher_search"
android:showAsAction="ifRoom"
android:title="#string/action_search"/>
<!-- Location Found -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_launcher_location_found"
android:showAsAction="ifRoom"
android:title="#string/action_location_found"/>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
And My Main Java Class is :
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
I think you confused names here. Progress bar is used for reporting/showing progress to user. While the action bar (I think this is the one you want) is a window feature for navigation, actions, etc.
I would suggest reading the following link http://developer.android.com/guide/topics/ui/actionbar.html#Adding
There you will find what is needed to add the action bar.
After that, if you need to add a Progress bar in your action bar, you need to read this tutorial:
Refresh item inside ActionBar using circular ProgressBar
Related
I am using a custom toolbar which has a menu icon. Now on clicking this menu icon i want to show the options menu. How can this be done.
I tried adding a onclicklistener to this menu icon
#Override
public void onClick(View v) {
if(v.getId() == R.id.toolbarMenuIcon){
openOptionsMenu();
}
}
This didnt work. Then i added these lines
setSupportActionBar(mBinding.customSelectToolbar.selectionModeToolbar);
in my activitys oncreate() . Also did override
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.selection_mode_menu, menu);
return super.onCreateOptionsMenu(menu);
}
With this i can see the overflow menu when i click the icon. But the problem is that, it adds the default menu icon also to the tool bar and thus my tool bar has two menu now. How can i have only my custom toolbar icon open the options menu
If you want to customize the default overflow menu icon..
Use setOverflowIcon method of your toolbar.
like:
toolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.your_icon));
I got it working using theme
<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="android:src">#drawable/ic_menu_overflow</item>
<item name="android:tint">...</item>
<item name="android:width">..dp</item>
<item name="android:height">..dp</item>
</style>
<style name="OnArrival.toolbarTheme" parent="Theme.OnArrival.Light">
<item name="actionOverflowButtonStyle">#style/OverFlow</item>
</style>
I'm trying to understand what is wrong with what I'm doing.
I'm currently following the android action bar tutorial and for some reason it's not showing me the search icon in the action bar on my device, it is going straight into my overflow.
I'm running 4.4.4 version on my device, Nexus 5. I've tried to follow several tutorials and the result was no different. I've set up new project by default using API 11 as told, I've tried to set the showAsAction to "Always".
What else could I do? Thank you for any help!
My XML menu file:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_new"
android:title="new"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="Settings"
android:showAsAction="never" />
Main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Try using a namespace
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:{anynamespace}="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_new"
android:title="new"
{anynamespace}:showAsAction="always" />
</menu>
Replace the {anynamespace} parts with any word
EDIT: The tutorial suggests that you use namespaces in case you are using the Support library.
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.
I have 4 items defined in xml as secondary then later I create one more in the code defined as container. I'm trying to get the one created in code (share) to be first, it's not working. It is always added to the end of the menu. I was under the impression that the menu category is what decided the action bar order. What am I missing?
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_unlock"
android:menuCategory="secondary"
android:title="Unlock"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_share"
android:menuCategory="secondary"
android:title="Share App"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_follow"
android:menuCategory="secondary"
android:title="Follow"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_review"
android:menuCategory="secondary"
android:title="Review"
android:showAsAction="ifRoom" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
actions = menu;
// Inflate the menu items for use in the action bar
getMenuInflater().inflate(R.layout.menu, menu);
return super.onCreateOptionsMenu(menu);
}
/*
* Set whether share buttons should be available.
*/
public void setShareable (boolean shareable)
{
if (!shareable)
return;
// Build share button
MenuItem item = actions.add (Menu.NONE, Menu.NONE, Menu.CATEGORY_CONTAINER, "Share");
item.setShowAsAction (MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
The correct usage of menu categories is to use FIRST instead of CONTAINER for the menu item that needs to get highest priority, and the rest as SECONDARY as used in the question.
An alternate way to order the menu items is through the android:orderInCategory attribute. Set the first preexisting menu item to, say, order 10 (even 2 would work in your case), and then you can add up to 9 other menu items before it, dynamically, by setting this attribute in the code (menu.add(Menu.NONE,Menu.NONE, order,R.string.my_menu_item). The order variable here is a little tricky, since it's an int that represents both the group association and the order within that group: see here.
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...