How to add menu button on the navigation bar - android

I have a simple app which only needs a menu button with some options, and it should work in all devices.
Anyway, my app works fine in all cases, except for that I couldn't place menu button on the navigation bar.
Here is my code:
styles.xml in value folder
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
</style>
styles.xml in value-v11 & value-v14 folders
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
<item name="android:actionBarStyle">#android:style/Widget.Holo.ActionBar</item>
</style>
This code appears in all onCreate events of my activities
if(Build.VERSION.SDK_INT <= 10 || (Build.VERSION.SDK_INT >= 14 &&
ViewConfiguration.get(this).hasPermanentMenuKey()))
{
// menu key is present
ActionBar actionBar = getActionBar();
if(actionBar!=null)
actionBar.hide();
}
else
{
//No menu key
ActionBar actionBar = getActionBar();
if(actionBar!=null)
actionBar.show();
}
This code works fine, but in case I there isn't any action bar, I want to put the menu button in the navigation bar.
I've done so much googling for that, but I couldn't find any working solution for this.
Thanks in Advance.

Since nobody had answered my question, I had to answer it my self.
First, it seems it is not recommended to activate the menu button in the navigation bar! (By Google)
Anyway, if you are interested in activating it, all you have to do is:
1. Make a simple menu like before
2. Not to use an action bar
3. Set targetSdkVersion to 13 or below
And, it is highly recommended to read this article Say Goodbye To The Menu Button

Related

Remove App Icon from Navigation Drawer ActionBar Does not work

I am working on an App which has design requirement to be built as it should only have the Title on the ActionBar and not the App Icon.
i tried various solutions found from StackOverflow like
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setIcon(null);
however none of these worked so far, even i tried to make a hack/fix by making a transparent ic_launcher icon and put it into manifest.xml but it causes the App installation icon make transparent.
please help
getActionBar().setDisplayShowHomeEnabled(false);
works perfectly in 4.4 kitkat , but showing a back arrow in downgraded version like this
and i need it like this to work in every device
please try adding this..
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
and in your theme add
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item
</style>
<!-- ActionBar styles -->
<style name="ActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#android:color/transparent</item>
</style>
Its quite tricky anyhow, but i solved it,and it works like a charm in all devices.
I made a 2x2 px transparant icon and added it as
ActionBar actionBar = getActionBar();
actionBar.setIcon(R.drawable.actionbar_null_icon);
and removed the code
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
thanks to nDroidDev for suport

Navigation drawer forces app name and icon to appear shortly on startup

Each time I start the app, the application name and icon appears very shortly on the left side over the drawer icon. This ruins the look of the custom ActionBar style.
How can I remove it? I already tried the top solution from StackOverflow, but they all refer to completely hiding the ActionBar. I don't want to hide it as I need it. I just don't know where to turn off this irritating display.
Note: I've let the Android Studio create the Navigation Drawer project for me so all the code came from the default creation.
EDIT
This is the code which appears in 3 locations: restoreActionBar, setUp and showGlobalContextActionBar. I am talking about the default Android Studio drawer project.
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
// actionBar.setTitle(mTitle);
}
Best method is to set the display options to useLogo in your theme. E.g.
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<style name="AppTheme.ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions">useLogo</item>
</style>
This won't actually show the logo (if set) because showHome is not included.
Try to use these
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);

LeftNavBar creates black bar at the top of the activity

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.

Using support action bar home enabled

I've just modified our code to use the new SupportActionBar provided in the v7-appcompat library but when running the code on a Jellybean phone (presumably the same problem exists for Honeycomb and Ice Cream Sandwich) the home button doesn't ever seem to be activated.
Calling getSupportActionBar().setHomeButtonEnabled(true); doesn't seem to do what it says but works for Gingerbread phones.
If I replace it with getActionBar().setHomeButtonEnabled(true) it does work.
The theme that I use for v11+ is as follows:
<style name="MyTheme" parent="#style/Theme.AppCompat">
<item name="android:windowActionBar">true</item>
<item name="android:windowNoTitle">false</item>
<item name="android:listViewStyle">#style/MyListView</item>
<item name="android:actionBarStyle">#style/MyActionBarStyle</item>
<item name="android:windowSoftInputMode">stateAlwaysHidden</item>
<item name="android:buttonStyle">#style/MyButton</item>
<item name="android:radioButtonStyle">#style/MyRadioButtonStyle</item>
<item name="android:windowContentOverlay">#drawable/ab_solid_dove_grey</item>
<item name="android:windowTitleSize">#dimen/action_bar_height</item>
<item name="android:selectableItemBackground">#drawable/sel_standard_item</item>
<item name="android:windowBackground">#drawable/default_bg</item>
<item name="android:actionMenuTextAppearance">#style/MyActionBarText</item>
<item name="android:actionMenuTextColor">#color/gallery</item>
<item name="android:tabWidgetStyle">#style/MyTabWidget</item>
</style>
And the action bar style v11+ is defined:
<style name="MyActionBarStyle" parent="android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">useLogo|showHome|showCustom</item>
<item name="displayOptions">useLogo|showHome|showCustom</item>
<item name="android:actionBarSize">#dimen/action_bar_height</item>
<item name="android:icon">#drawable/ic_launcher</item>
<item name="android:background">#android:color/transparent</item> <!-- Remove blue line from bottom of action bar -->
</style>
Anyone know why the home button is not getting enabled when on an Android version that supports action bar correctly.
=== UPDATE ===
I've just looked at the source code for the appcompat library and I've noticed the following in ActionBarImplBase which looks wrong to me:
setHomeButtonEnabled(abp.enableHomeButtonByDefault() || homeAsUp);
This means that the home button will only be enabled if the Android version is less than ICS or if I've enabled the up indicator? - which I don't want.
This one worked for me:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ... other stuff
}
#Override
public boolean onSupportNavigateUp(){
finish();
// or call onBackPressed()
return true;
}
The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.
Have you tried using all three of these (also try swapping for getSupportActionbar())?
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
Have you tried handling the button manually?
#Override
public boolean onOptionsItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
if(itemId == android.R.id.home){
// Do stuff
}
return true;
}
Try use Sherlock library for android devices such as Gingerbread cos android action bars is only supported from 3.0 upwards so the sherlock lock library gives you backward compatibility.
http://actionbarsherlock.com/ --- download library here.
Then add this lines in your code.
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(android.R.color.transparent);
actionBar.setDisplayUseLogoEnabled(false);
This would help you to add a back home key in your action bar. It would also make your icon invisible if you dont want it to show.
But if you want your app icon show on all activity simply comment this line below
actionBar.setIcon(android.R.color.transparent);
Now Please try this. Cause I was able to solve my own problem like this though it was on Sherlock. From your styles above I can see you did some customization to your themes. In my case I did some customization to my Sherlock theme and this was what gave me problem cos on android 4.0 and above my theme failed. so I simple added a piece of code that tells android to use the default Sherlock theme when it is running on android 4.0 and greater. So I suppose this would work for you. you tell android to use the default theme of v7-appcompat library on the version of android that is not working for you.
Code is below:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
this.setTheme(R.style.Theme_Sherlock);
} else {
this.setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
}
In your case edit the theme to v7-appcompat library.
Please Mark as answer if it work for you. I believe it might be possible to customize the theme from the code for places were you are using this.
You can add an ActionBar to your activity when running on API level 7 or higher by extending ActionBarActivity class for your activity and setting the activity theme to Theme.AppCompat or a similar theme.

Activity with action bar but without title?

How to achieve this? It seems very simple, but actually it's not easy to do it in acceptable way. I tried this:
1) in AndroidManifest I set activity theme to Theme.NoTitleBar. However, in my Activity then getActionBar() method returns null => UNUSABLE
2) I hide title programatically in my activity by calling:
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayShowHomeEnabled(false);
and it helped, but there's a little delay, so I can see title bar maybe quarter of second after activity launch, then it disappears. It looks really lame.
Are there other options?
Apply a custom theme to your Activity, something like:
<style name="TestTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/TestActionBar</item>
</style>
<style name="TestActionBar" parent="android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="android:displayOptions"></item>
</style>
This will show the action bar with the logo, but no title.
Have u try bellow code :-
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.ur activityxml);
or also try below one
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activityxml);

Categories

Resources