Clarifications about ActionBarCompat - android

Until now my Activity extended FragmentActivity and uses the ActionBarCompat. If i try to run my app on a smartphone with API level 10 (2.3.3) (or less i think) the ActionBar doesn't appear. To try to solve this problem i extended ActionBarActivity, BUT now i can't see the
items on it. I think i need to change this method:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
but i don't understand how.
This is the main:
<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.loris.stefano.easyroutes.MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/option_group_2"
android:icon="#drawable/layers"
android:showAsAction="always">
<menu>
<item
android:id="#+id/map_tipe_normal"
android:title="#string/map_type_normal"/>
<item
android:id="#+id/map_type_terrain"
android:title="#string/map_type_terrain"/>
<item
android:id="#+id/map_type_hybrid"
android:title="#string/map_type_hybrid"/>
<item
android:id="#+id/map_type_satellite"
android:title="#string/map_type_satellite"/>
<item
android:id="#+id/map_type_none"
android:title="#string/map_type_none"/>
</menu>
</item>
<item
android:id="#+id/draw_path_menu"
android:icon="#drawable/draw"
android:showAsAction="always">
<menu>
<item
android:id="#+id/merge_markers"
android:title="Merge Markers"/>
<item
android:id="#+id/draw_path"
android:title="Draw Path"/>
</menu>
</item>
<item
android:id="#+id/track_options"
android:icon="#drawable/import_map"
android:showAsAction="always">
<menu>
<item
android:id="#+id/import_track"
android:title="Import Track"/>
<item
android:id="#+id/export_track"
android:title="Export Track"/>
<item
android:id="#+id/download_track"
android:title="Download Track"/>
</menu>
</item>

Related

How do I get two menu options with dropdowns next to each other in an Android layout file?

I'm trying to get a menu layout where I have 2 icons next to each other which both have dropdown options. Currently it's being annoying and putting all the items in the default option menu like this:
image
<?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:title="#string/menu_sort"
android:id="#+id/menu_sort"
app:showAsAction="always"
android:icon="#drawable/ic_baseline_sort_24">
<item android:title="Distance"/>
<item android:title="Salary"/>
<item android:title="Date Posted"/>
</item>
<item
android:title="#string/menu_filter"
android:id="#+id/menu_filter"
app:showAsAction="always"
android:icon="#drawable/ic_baseline_filter_list_24">
<item android:title="Freelance"/>
<item android:title="Zero Hour"/>
<item android:title="Part Term"/>
<item android:title="Internship"/>
</item>
</menu>
Maybe this can help....
<?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:title="#string/menu_sort"
android:id="#+id/menu_sort"
app:showAsAction="always"
android:icon="#drawable/ic_baseline_sort_24">
<menu>
<item android:title="Distance"/>
<item android:title="Salary"/>
<item android:title="Date Posted"/>
</menu>
</item>
<item
android:title="#string/menu_filter"
android:id="#+id/menu_filter"
app:showAsAction="always"
android:icon="#drawable/ic_baseline_filter_list_24">
<menu>
<item android:title="Freelance"/>
<item android:title="Zero Hour"/>
<item android:title="Part Term"/>
<item android:title="Internship"/>
</menu>
</item>
</menu>
You add <menu></menu> inside an item and add submenu items there....

OptionsMenu - How to change options menu background layout

I want to change the background layout of the OptionsMenu. Not from styles.xml but dynamically on java. I want to get the inflate view and change it. I have not found such method that solves my problem. I want something like
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.options_menu, menu);
// Layout root = menu.getView();
}
I'm using this options menu
<?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/options_menu_more"
android:title=""
android:icon="#drawable/ic_more_vert"
android:orderInCategory="1"
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/options_menu_1"
android:title=""
android:orderInCategory="1" />
<item
android:id="#+id/options_menu_2"
android:title=""
android:orderInCategory="2" />
<item
android:id="#+id/options_menu_3"
android:title=""
android:orderInCategory="3" />
<item
android:id="#+id/options_menu_4"
android:title=""
android:orderInCategory="4" />
</menu>
</item>
</menu>

Determining the SubMenu of a MenuItem

I have a NavigationView in my app. Inside the menu for the NavView, I have two sub menus:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_group_one"
android:title="SubMenu A">
<menu>
<item
android:checkable="true"
android:title="One"/>
<item
android:checkable="true"
android:title="Two"/>
</menu>
</item>
<item
android:id="#+id/nav_group_two"
android:title="SubMenu B">
<menu>
<item
android:checkable="true"
android:title="Three"/>
<item
android:checkable="true"
android:title="Four"/>
</menu>
</item>
</menu>
In my activity's onNavigationItemSelected() I would like to be able to figure out which sub menu the selected item is in. How would I do this?
So far I haven't found an existing solution at this time, however I have found a workaround:
To solve this with minimal additional XML overhead, wrap each set of sub-menu items in a group, with an Id:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_group_one"
android:title="SubMenu A">
<menu>
<group id="#+id/nav_group_wrapper_one">
<item
android:checkable="true"
android:title="One"/>
<item
android:checkable="true"
android:title="Two"/>
</group>
</menu>
</item>
<item
android:id="#+id/nav_group_two"
android:title="SubMenu B">
<menu>
<group id="#+id/nav_group_wrapper_two">
<item
android:checkable="true"
android:title="Three"/>
<item
android:checkable="true"
android:title="Four"/>
</group>
</menu>
</item>
</menu>
This way, one can simply call item.getGroupId() to infer the sub-menu it is contained within.

Android actionbar only show overflow

my actionbar is only showing the items in overflow. the androidstudio displays the items correct in the actionbar (not overflow). if i run the app on emulator or on my device the items always appears in overflow
action_bar.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_home"
android:icon="#drawable/ic_home"
app:showAsAction="always|withText"
android:title="home"/>
<item
android:id="#+id/action_share"
android:title="share"
android:icon="#android:drawable/ic_menu_share"
app:showAsAction="always"
/>
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/MyAppTheme.ActionBarStyle</item>
</style>
<style name="MyAppTheme.ActionBarStyle" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/MyAppTheme.ActionBar.TitleTextStyle</item>
<item name="android:displayOptions">showHome</item>
</style>
<style name="MyAppTheme.ActionBar.TitleTextStyle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#000000</item>
</style>
</resources>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, menu);
return super.onCreateOptionsMenu(menu);
}
thanks for any help....
You are using Theme.Holo. This means that you are trying to use the native action bar. In that case, change app:showAsAction to android:showAsAction.
The following code would solve your problem.
<item
android:id="#+id/action_share"
android:icon="#android:drawable/ic_menu_share"
android:showAsAction="always"
android:title="share"/>
Note the namespace used for the attributes.

Adding more options to "Settings" ActionBarSherlock

I have the following items to display on the ActionBar. They are :- Share and Settings.
Under Settings I need to show a drop down menu with two more options.
Here is my code:-
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_share"
android:icon="#drawable/share"
android:title="Share"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="Settings"
android:showAsAction="never" />
</menu>
How can I go ahead wit it?
Try this code for subMenu using XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_share"
android:icon="#drawable/share"
android:title="Share"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:icon="#drawable/setting"
android:title="#string/settings"
android:showAsAction="ifRoom|withText">
<menu>
<item android:id="#+id/option1"
android:icon="#drawable/yourIcon"
android:title="SubMenu1" />
<item android:id="#+id/option2"
android:icon="#drawable/yourIcon"
android:title="SubMenu2" />
</menu>
</item>
</menu>
JAVA code for Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar, menu);
return true;
}

Categories

Resources