No App Icon on ActionBar - android

There are a lot of queries here about adding icons to ActionBar but none solved my problem. If you know a duplicate of this question, feel free to comment or close this question.
I migrated my project to IntelliJ and I didn't encounter this problem with my previous IDE (Eclipse).
PROBLEM: The app icon is not displayed in the ActionBar.
I think it's supposed to be added by default that's why I can't add it through its XML
Here's its XML
<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=".MainActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>
Thanks!

As of AppCompat version 21, the Action Bar follows the material design guidelines and uses a Toolbar:
A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
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.
However, if you want an application icon, setLogo() is the correct method.

Update your onCreate() method with the code below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.activity_main);
}
NOTE: ic_launcher is the icon you want to display in your actionbar. To display it, add the icon in the drawable folder of your app project.

In your Style.xml file:
<style name="MyTheme_ActionBar" parent="#style/Theme.AppCompat.Light">
<item name="icon">#drawable/actionbar_logo</item>
</style>
In activity add this code:
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

If you don't care about the Material theme and are fine having an Activity that looks more JellyBean/Kitkat style and includes the icon in the Action Bar, you can do the following:
First, change themes setting in styles.xml from this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
To this:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
Now change all your Activities to inherit from android.app.Activity instead of android.support.v7.app.ActionBarActivity. That is:
Change this:
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity{
To this:
import android.app.Activity;
public class MainActivity extends Activity {
The end of result of doing both of the above steps is that the icon as specified by the android:icon attribute in AndroidManifest.xml will appear in the Action Bar.

This worked for me. Your onCreate method should have these lines:
ActionBar menu = getSupportActionBar();
menu.setDisplayShowHomeEnabled(true);
menu.setIcon(R.mipmap.imageFile);
Make sure the imageFile is a .png in the mipmap folder. To get the icon exactly at the start of your action bar, the mipmap folder should have multiple versions of the image file in all screen sizes: hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi, etc.

Setting Icon On the Action Bar
Showing Icon On the Action Bar Can Be Tricky
If you are extending Activity this should be enough:
getActionBar().setDisplayShowHomeEnabled(true);
If you are extending AppCompatActivity then additional code is needed:
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);

Related

Android API 15. I have toolbars in my activites but they are not shown in XML

Very Novice Android Programmer here. I have this small program where i have my main activity and whenever the user clicks an option, a new activity will open. I am trying to change the color of the top toolbar/actionbar for each different activity. I have tried changing the color through Java code within the activity class in the onCreate() method,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.gradient));
but the program would always crash whenever I would switch to my activity.
I have looked in the XML file of my activity and the toolbar/action code does not show up anywhere, but it does in my main activity, app_bar_main.xml. I'm wondering why the actionbar shows up in activies if it does not show up in the XML file for the activity. How to change the color of the actionbar for newely added non main activities?
I'm guessing you're using app theme with action bar. In res/styles.xml change your theme to NoActionBar version:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Now ActionBar shouldn't be visible. Add your Toolbar XML code to other activity layouts.
Try this, it will work:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

How can I use an icon instead of a title in the Android Toolbar?

I'm using the Toolbar (instead of ActionBar) via AppCompat. I'd like to replace the Toolbar's title (the app/actity name) with an icon, but I don't see how.
My icon is just text using a non-standard font, so I guess it might be allowed in Material Design.
Use the following steps in your activity:
Declare the Toolbar object:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool1);
Set the support actionbar:
setSupportActionBar(toolbar);
Remove title:
getSupportActionBar().setDisplayShowTitleEnabled(false);
Add your logo in drawable folder in:
res/drawable.
Add logo using this code:
toolbar.setLogo(R.drawable.ic_launcher);
At styles.xml first make a style for action bar with no title and with the logo like that
<style name="ActionBar.NoTitle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="displayOptions">useLogo|showHome</item>
<item name="logo">#drawable/ic_logo</item>
</style>
Then create custome theme for your activity with new actionbar style
<!-- Create a style for MainActivity called AppTheme.Main that uses our custom ActionBar style -->
<style name="AppTheme.Main" parent="#style/AppTheme">
<item name="actionBarStyle">#style/ActionBar.NoTitle</item>
</style>
Then go to manifest file under the specified activity set theme to AppTheme.Main
android:theme="#style/AppTheme.Main"
I wish that help everyone
Here is the answer to a very similar question:
Android Lollipop, add popup menu from title in toolbar
It comes down to using the Toolbar as a ViewGroup (LinearLayout is a ViewGroup too, for instance). The layout designer doesn't currently support that, but you can add a child node in the XML.
Then you need to call this to remove the standard title text:
getSupportActionBar().setDisplayShowTitleEnabled(false);

Action bar app icon missing with Android 5

Just rebuilt my app using the Android 5 SDK and associated appcompat.
Seems to work fine but my app icon is no longer showing in top left hand corner. The icon to open nav drawer is there but no icon.
Any way to fix this?
Use the code below in onCreate:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
I too noticed that the default projects created by Android Studio were missing the icon in the action bar. Here's how I fixed it.
Disclaimer: This solution will result in in the Material theme being dropped in favor of the older JellyBean/Kitkat styles.
First, change themes setting in styles.xml from this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
To this:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
Now change all your Activities to inherit from android.app.Activity instead of android.support.v7.app.ActionBarActivity. That is:
Change this:
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActivityBarActivity {
To this:
import android.app.Activity;
public class MainActivity extends Activity {
The end of result of doing this is an app that has the Holo theme more commonly seen on Jelly Bean and Kitkat.
Make sure to extend ActionBarActivity rather than Activity. And add the following code:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
You shouldn't have an icon in an API OS 5+ app. By default it is not displayed (and this is the preferred behavior). The Toolbar docs specifically state this:
"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."
http://developer.android.com/reference/android/widget/Toolbar.html

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 remove default action bar in Android project?

I have created an Android application in Eclipse and when I have setting up I have unchecked every navigation bar. I still have the default navigation bar, how I can remove it?
You can do this in your activity:
ActionBar actionBar = getSupportActionBar(); \\ or getActionBar()
You can then hide it like this:
actionBar.hide();
Please note the warning about this in the "Removing the action bar" section of http://developer.android.com/guide/topics/ui/actionbar.html
In an app I made, I removed the title/ActionBar using this code in the onCreate method:
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main); //insert your own layout here
Make sure that the requestWindowFeature method appears before the setContentView method, because otherwise your Activity will crash.
First of all create a new theme/style within your styles.xml in the resources folder. Insert the following code within your resource tags:
<style name="noActionBar" parent="Theme.Base"></style>
By using parent "Theme.Base" it creates a blank screen to work on. Then just set your android theme to your new style in the android manifest, within the 'application' tags.
android:theme="#style/noActionBar"
Please note that if your class file for your activity contains any reference to the ActionBar it may crash when compiling.
Go in styles.xml and change to:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Categories

Resources