Using support action bar home enabled - android

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.

Related

How to get lollipop status bar on kitkat

I'm trying to achieve that Status bar with PrimaryDarkColor And PrimaryColor like Lollipop on Kitkat and here is my try:
<style name="PopularTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">#color/colorPrimaryDarkPopular</item>
<item name="colorPrimary">#color/ColorPrimaryPopular</item>
<!--here is what I've added to try to get transparency -->
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
Also, I test this method with https://github.com/jgilfelt/SystemBarTint but it won't be my choice because I'm using another color for each activity.
How to achieve that status bar with PrimaryDarkColor and PrimaryColor in Kitkat?
Result:
I've just checked here on the app I work at how we do it.
First remove everything from under <!--here is what I've added to try to get transparency -->, we have nothing of those on our style. However it works the AppCompat theme or activity already does for you.
and then you add the extra rule for Kitkat:
#TargetApi(Build.VERSION_CODES.KITKAT)
private static void setupWindowKiKat(Window window) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
and call it from your activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
setupWindowKiKat(getWindow());
it is possible that you can add this to a values-v19\style.xml but above it's how it's on our app. Hope it helps.
You Should first root Your device, then go to Google play store and install (Wanam Xposed) http://repo.xposed.info/module/ma.wanam.xposed
open it and follow some steps right there then install (Xposed installer) http://repo.xposed.info/module/de.robv.android.xposed.installer
Then open this last Go to "download" and look for "Flat style colored bars" download and install it then go back to Modules activate it and reboot your phone and You will see the changes.
I hope i helped. i works great for me on ace 4 Sm-g316hu
This is not possible because they both run on a different API (lollipop on 20, 21 or 22).

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

Android get Support Action Bar always returns NULL

I'm using the support library v7 on a certain project. I'm trying to instantiate the support action bar like this:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.top_action_bar);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setHomeButtonEnabled(true);
Everything works fine on Android devices with API lvl below 11. However, while trying to run the same code on newer devices, like a Samsung Galaxy S4 with Android 4+, getSupportActionBar() always returns null, causing the expected crash.
I'm using this style file:
<style name="my_style" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/my_style_ab</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="my_style_ab" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:height">70dp</item>
</style>
Any hints on what may be wrong? I think it has something to do with the style, or maybe I just can't use the support ActionBar on API lvl above 11. Thanks in advance.
Using android:windowNoTitle with true is hiding your ActionBar
I got the solution. And I didn't provide enough information in the question so it may be hard for others to help. However, I don't know why the problem happens, I just found out how to solve it. The error was due to the app title. I removed this item from the style:
<item name="android:windowNoTitle">true</item>
And also all the references in the code regarding that title bar, like this one:
requestWindowFeature(Window.FEATURE_NO_TITLE);
Why does this solve it? still don't know. It may be useful for me if someone can explain it.
If you are using Toolbar and AppCompat then you probably have android:windowNoTitle set to true, hiding your action bar. Create a Toolbar in your xml and add this code in the onCreate() of your activity.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Inside Manifest file, check the activity for which are you having the issue.
I have the theme with "NoActionBar" Property.
Removing it resolved this code worked for me.
android:theme="#style/AppTheme.NoActionBar"

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.

How to add menu button on the navigation bar

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

Categories

Resources