I am using ActionBarSharlock in my Application for Action Bar. In actionBar I take a button and If I click the button I see a menu with three items. Here is the code-
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu1 = menu.addSubMenu("Action Item");
subMenu1.add("Sample");
subMenu1.add("Menu");
subMenu1.add("Items");
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.sub_menu_icon);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
But I don't know how to make this three submenu1 item clickable and move one activity to another. What kind of function I need to write. Let me know It will be great help
First: why don't you add your menu items in the XML menu file in res/menu/your_file.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
then inflate the menu in your activity:
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);
}
and then override the onOptionsItemSelected method to handle the menu item clicks.
Also Google developed their own library for ActionBar on API level < 11 so you should try using that one as well instead of Sherlock.
Hope this helps. Best of luck.
Related
By default android have an option to enable BACK ARROW on the left of the toolbar using
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
same like i need TICK MARK on the right of the toolbar, is there any method to enable TICK MARK in android by default.
First download the "done" icon as png from the materials icon library
Copy the icons to your projects drawable folder.
Then create a file in res/menu called menu_example.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_settings"
android:icon="#drawable/ic_done_white_24dp"
android:title="#string/action_done"
app:showAsAction="always" />
</menu>
Then add the following code to your Activity.
#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_example, menu);
return true;
}
To have a onClick listener for the tick you can implement this:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
toast.show();
return true;
}
I am working with the navigation drawer and i want to display different icon on different fragment when user click on any item of listview on navigation drawer.
i have did a code in xml of activity which contain navigation drawer.
<item
android:id="#+id/action_settings"
android:icon="#drawable/icon_setting"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
When i click on any item on list the icon should change. i tried to get reference like,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
mItem = menu.getItem(0);
return true;
}
But it gives null pointer exception.thank you in advance.
Try findItem:
mItem = menu.findItem(R.id.action_settings);
Assume we have a activity which is a child class of an activity defined in a library. In this child class, we want to add additional menu item into the menu created by the base class. But we want this additional menu item defined in XML. Is that possible? How?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// TODO: how to add an additional menu item here?
// the item is defined in xml
// the id is item_switch_browsing_mode
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item_switch_browsing_mode"
android:icon="#android:drawable/ic_menu_view"
android:showAsAction="always"/>
</menu>
Try menu inflater, it should add menu items based on the specified xml, but call super.onCreateOptionsMenu to populate the parent menu items first. Something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.new_menu, menu);
return true;
}
You may refer to http://developer.android.com/reference/android/view/MenuInflater.html.
I'm developing an app which supports APIS 9 to 17.
It also has a Navigation Drawer and for the action bar I use ActionBarSherlock. The problem comes when I press the menu hardware button (API <= 10). The actions displayed in the actionbar are duplicated. How to fix this issue?
Here's my code to inflate the menu
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_content_action_menu, menu);
return true;
}
And my menu_content_action_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/send_order_action"
android:icon="#drawable/ic_navigation_accept"
android:showAsAction="ifRoom|withText"
android:title="#string/send_order_button_text">
</item>
</menu>
Hope you can help me.
Use your code in the onCreateOptionsMenu() implementation instead.
public boolean onCreateOptionsMenu(final Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu_content_action_menu, menu);
return true;
}
Lets assume, that I have two activities. One called MainActivity, and the other called PopupActivity.
I want to have a context menu in MainActivity, after the user visited PopupActivity.
Main [no menu] --> startActivity() / startActivityForResult()
==> Popup --> back / finish()
==> Main [now has menu].
Environment details:
minSDK: 10 (needed for backward compatibility)
targetSDK: 17
targetDevice: GalaxyTab 2.0
If the MainActivity has at least one element at the begining state, i can add / remove the desired menu items via onPrepareOptionsMenu. But if the MainActiviy has 0 menu elements inside, android doesn't even render the menu button on the top right corner.
My question is:
What do i miss?
How to tell android, to render the menu button, because i want to add menu items to it.
Workaround soultions can't work (like separating the Activity into two, etc.), because the whole problem is a bit more complex, but the core of it is this. I don't need menu items at the begining state, and i need them in a second one.
main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_add" android:title="#string/add"></item>
<item android:id="#+id/menu_remove" android:title="#string/remove" ></item>
</menu>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
if (initState)
{
menu.findItem(R.id.menu_add).setVisible(false);
menu.findItem(R.id.menu_remove).setVisible(false);
} else
{
menu.findItem(R.id.menu_add).setVisible(true);
menu.findItem(R.id.menu_remove).setVisible(true);
}
return super.onPrepareOptionsMenu(menu);
}
First Create two menu. one for initial stage and second for after visited PopupActivity.
Replace Your onCreateOptionsMenu() with
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
if (initState)
inflater.inflate(R.menu.invisible_main_menu, menu);
else
inflater.inflate(R.menu.visible_main_menu, menu);
return true;
}