I have developed an android app that mainly targets smartphones. However in tablet emulator I see that it works on android 3.x, too.
However there is one little problem. The user cannot open OptionsMenu when he clicks on the menu button. As you know on the smartphone a menu appears from the bottom. But on tablet nothing happens.
I have read this http://developer.android.com/guide/topics/ui/menus.html#options-menu but still cannot figure out how to manage this. My app has a custom action bar.
My code is quite straight forward. In main activity:
#Override
public boolean onCreateOptionsMenu(Menu men) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, men);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//...
}
}
And the prefs activity:
public class MdPrefsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(
MdSharedPrefs.PREFS_NAME);
addPreferencesFromResource(R.xml.prefs);
}
}
MdSharedPrefs class just contains some getters and setters to retrieve/write the pref values.
Any ideas how I can show the OptionsMenu on tablet?
As the documentation said:
Items in the Options Menu are accessible in two distinct ways: the
MENU button or in the Action Bar (on devices running Android 3.0 or
higher).
[...]
On Android 3.0 and higher, items from the Options Menu is placed in
the Action Bar, which appears at the top of the activity in place of
the traditional title bar. By default all items from the Options Menu
are placed in the overflow menu, which the user can open by touching
the menu icon on the right side of the Action Bar. However, you can
place select menu items directly in the Action Bar as "action items,"
for instant access [...]
So for Android 3.0 or higher you can see only the menu items in the ActionBar.
It is also important to notice that:
Beginning with Android 3.0 (API level 11), the action bar is included
in all activities that use the Theme.Holo theme (or one of its
descendants), which is the default theme when either the
targetSdkVersion or minSdkVersion attribute is set to "11" or greater.
But be aware that the ActionBar is visible only if you don't have an application or activity theme that explicitly hides it like
android:theme="#android:style/Theme.Holo.NoActionBar"
On a tablet, no hardware button that can be used to load the menus, you need to create two folders in your res: the first call it values-11 and the second call it values-14. Inside these folders, put these styles (styles.xml) that will replace your default basetheme in the values folder whenever devices of higher version are used:
Res/values-11
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
Res/values-14
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
Related
I had my app looking pretty nice using the new Lollipop tools. I decided backwards compatibility is important, so I switched all my Fragments,actionBar imports to the support library. Now (understandably) I can't use my lollipop theme.
Is there a way to use different action bars for different themes? I tried to cast the support ActionBar to a new one but it doesn't seem this is allowed.
My problem lies with the following (from v21 docs)
All of your Activities must extend from ActionBarActivity, which
extends from FragmentActivity from the v4 support library, so you can
continue to use fragments. All of your themes (that want an Action
Bar/Toolbar) must inherit from Theme.AppCompat. There are variants
available, including Light and NoActionBar. When inflating anything to
be displayed on the action bar (such as a SpinnerAdapter for list
navigation in the toolbar), make sure you use the action bar’s themed
context, retrieved via getSupportActionBar().getThemedContext(). You
must use the static methods in MenuItemCompat for any action-related
calls on a MenuItem.
so by calling getsupportActionBar I can't use my Holo theme:
<resources>
<!-- Base application theme. -->
<style name="appTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
<style name="MyActionBar"
parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:background">#color/blue_semi_transparent</item>
</style>
</resources>
Also for some reason the action bar loses the button that was on it and it goes into the dropdown menu and the app icon no longer appears in the action bar.
I really am no expert on this stuff having only started developing on lollipop so would really appreciate advice.
AppCompat (i.e., ActionBarActivity) uses the Material color palette which defines default coloring throughout your app. In your case, you need to use colorPrimary for your action bar color:
<style name="appTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/blue_semi_transparent</item>
</style>
Note that you should also provide a colorPrimaryDark (a darker version of the same color) for coloring the status bar.
Per the partially outdated Action Bar training, AppCompat also uses app namespaced attributes (as things like showAsAction didn't exist before API 11) for your menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
...
</menu>
Per the Toolbar documentation (which is default behavior on Material themes and in AppCompat):
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.
Therefore as you noted the app icon not appearing on the Action Bar is expected behavior.
I'm trying to incorporate Google's LeftNavBarLibrary into my application. When I load the nav bar I end up with a black bar across the top of the activity. The bar appears to be taking up the space a traditional actionbar would occupy.
Does anyone know where the bar is coming from or how to remove it.
Thanks.
My application theme is slightly customized. Based on the AppCompat theme due to requirements of the MediaRouteActionProvider
styles.xml
<resources>
<style name="AppTheme" parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/ab_gradient</item>
</style>
</resources>
The activity pictured above has a custom theme defined in the manifest.
AndroidManifest.xml
<activity
android:name="my.app.namespace.CoreActivity"
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen" >
</activity>
The applications minimum sdk version is 14. So it's not exclusively a Google TV app. I've only been able to test this bug on my Android 4.1 and 4.4 devices.
I deal with the action bar this way:
getActionBar().hide();
Try to put this in your main activity or the activity that is the parent and always present.
Don't bother about the theme in manifest, just set your theme with title bar and hide it through the code.
Hope this helps.
Take a look at: Hide the Status Bar on Android 4.0 and Lower
Notice that this is in the <application> tag. That might help you.
<application
...
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>
or programmatically:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
}
You can set android:windowActionBar style to false by setting custom theme.
Note: In this case getActionBar() will return null. If you remove the action bar using a theme, then the window will not allow the action bar at all.
Thanks for the answers guys. The real issue was actually that the LeftNavBar.java class was creating an artificial margin at the top of the screen. I'd have thought that a google published library would be better than that, but apparently not.
I am having an application which contains Menu options, but in some of Jelly Bean devices we don`t have the Menu soft key button so in that case how can I show those menu options?
Do I need to check sdk version, based on which I have to implement functionality?
and I am unable to use hasPermanentMenuKey() function in my app.
My app targets
android:minSdkVersion="9"
android:targetSdkVersion="17"
Can anyone give me any suggestions?
I think, you must have given android-9 specific theme in Android Manifest.
Example
android:theme="#android:style/Theme.Light"
1) Since Android 4.0 we have Action Bar, so many of the device running Android 4.0 and above will not have Hardware Menu.
2) All your Menu will be in action bar.
3) So, you need to specify different theme to Android v-14 and above, for you app to display Action bar with your menu.otherwise you will not get Action bar so as you Menu.
How To Do It
1) In values/styles.xml
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<style name="AppTheme" parent="AppBaseTheme"> </style>
</resources>
2) In values-v14/styles.xml
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"></style>
</resources>
3) In AndroidManifest.xml
android:theme="#style/AppTheme"
use a lower minSDKVersion. Then in those set there will be a soft key for option menu too. for example
android:minSdkVersion="5"
Since the ActionBar was introduced in HoneyComb the menu you set using the onCreateOptionsMenu is displayed on the ActionBar of the activity.If you specify the android:showAsAction="never|withText" the item goes into the overflow menu...something which looks like three dots and is displayed with it's full text.
You can use the same API you use in pre HoneyComb:
boolean onCreateOptionsMenu(Menu menu)
boolean onOptionsItemSelected(MenuItem item)
My Android App has min sdk and target sdk set to 10, when I run my app on Nexus 4 -Android Version 4.2.2, it shows a menu option button beside the system navigation.
I am not using menu options in my app and I dont want to show the user this useless menu option. How can I remove it ?
I have read about this on http://android-developers.blogspot.com.au/2012/01/say-goodbye-to-menu-button.html but I do not get a concrete solution.
Attached is the image :
I dont want to change the sdk version for my app. Is it possible to remove menu option without changing sdk version?
You simply have to modify your targetSdk to something above 10. For instance, if you use last SDK you should use targetSdk=16
The concrete solution is in the article:
Add theme to your application:
<application android:theme="#style/NoActionBar">
Then define this theme for pre-Honeycomb in res/values/themes.xml:
<resources>
<style name="NoActionBar" parent="#android:style/Theme">
<!-- Inherits the default theme for pre-HC (no action bar) -->
</style>
</resources>
and for Honeycomb and later:
<resources>
<style name="NoActionBar" parent="#android:style/Theme.Holo.NoActionBar">
<!-- Inherits the Holo theme with no action bar; no other styles needed. -->
</style>
</resources>
If you set your target SDK version to 11 and below, the android legacy menu button will appear. But just in case you want to keep that target SDK version, you can just delete the method onCreateOptionsMenu, this way you won't inflate any options.
I'm creating an Android app using the theme "noTitlebar" and all the design changes are done and approved. Now I need to add a menu, so I have to add title bar. But, if I change the theme, the whole design for pop up, border, etc. will be affected.
So I created a custom theme by extending the theme
"android:style/Theme.NoTitleBar".
I enabled the title by
<item name="android:windowNoTitle">false</item>
Now I got the title bar, but menu is showing up. The control is not coming to
public boolean onCreateOptionsMenu(Menu menu) {
How can we enable menu in this theme? Help will be greatly appreciated.
Since your are targeting Honeycomb (right?), when you say titlebar you probably mean Action Bar? Follow the link to read the docs.
In a nutshell:
You need to set targetSdkVersion="11" and your build target (in eclipse) to v11.
Just use Theme.Holo. Action bar is by default enabled. Theme.Holo is a default theme for v11 so you even don't need to define it.