Application's icon not displayed in ActionBar - android

I'm new in the Android programming world, and I don't understand why the system is not displaying the application icon on the left side of the 'ActionBar' of my first Android application (HelloWorld).
In the 'ActionBar' developer guide it's stated that:
"By default, the system uses your application icon in the action bar, as specified by the icon attribute in the "application" or "activity" element. However, if you also specify the logo attribute, then the action bar uses the logo image instead of the icon."
(Action bar)
My Android-manifest file isn't defining a 'logo' attribute, but it defines just an 'icon' attribute. Therefore, the icon defined by the 'icon' attribute should be displayed on the left side of the 'ActionBar' as the application icon. However, no icon is being displayed.
My Android-manifest file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.holamundo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppBaseTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And my style files are the following:
/res/values/styles.xml:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
/res/values-v11/styles.xml:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
</resources>
/res/values-v14/styles.xml:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
</resources>
Can anyone explain me why my application icon is not being displayed in the ActionBar and what should I modify in order to such an icon be displayed? Thanks.

Your activity must extend ActionBarActivity.
For example:
public class MainActivity extends ActionBarActivity
{
...
}
Or try this in your activity:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.ic_launcher);
You will have to use getSupportActionBar() if using the support library.

I've also been having the problem where a menu item icon is not being displayed. I have a toolbar and I set it up to be the support action bar. The manifest declares a NoActionBar theme. Originally I thought that none of the icons for action items would display, however, I've discovered that some drawable icons would display. For, example "#android:drawable/ic_menu_add" would work, but "drawable/ic_launcher_background" would not work. Here's the menu structure that I inflate during the onCreateOptionsMenu. The item action_logo uses a png file and that one works too. The 'ic_launcher_background" is a drawable that is automagically added when the project was created.
<item
android:id="#+id/action_help"
android:orderInCategory="50"
android:title="#string/action_help"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="always" />
<item
android:id="#+id/action_more"
android:orderInCategory="60"
android:title="#string/action_help"
android:icon="#drawable/ic_launcher_background"
app:showAsAction="always" />
<item
android:id="#+id/action_logo"
android:orderInCategory="70"
android:title="#string/action_help"
android:icon="#drawable/tabellae_logo"
app:showAsAction="always" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
What I've been trying to do was to generate a BitmapDrawable on the fly an add it to the menu during a onPrepareOptionsMenu call, but that kind of drawable won't display either.
I have different test application that has it's manifest set to an ActionBarTheme. In this configuration, you don't need to call setSupportActionBar. (It's error if you try to do so) Now this test application displays all the menu icons correctly. It is an AppCompatActivity application. My guess is that this test application is probably using the old style ActionBar rather then the newly recommended ToolBar approach and that's why it works.
My preference is to use the ToolBar approach, but I don't have an explanation why some icon drawables work and other do not. In my research on this problem, I've not seen anyone mention these observations. Perhaps someone would have more insight into why certain drawables don't work for menu item icons.

Related

How to disable the title on the ActionBar in Main Launcher Activity

I checked this issue, couldn't find an answer.
there are 3 places I checked that show the label on 3 separated places in my android phone or app.
Under the logo of the app
In the app processes which show's the active apps, (from that window you can close the app process)
In the most annoying place, the "splash" action bar title (It happens before the MAIN activity UI start, when it loading the activity onCreate I believe), which most of us want to get rid of usually.
The 3 places are:
<application
android:name=".Application"
android:icon="#mipmap/icon"
android:label="#string/app_name" <!-- 1--- the first place-->
android:theme="#style/AppTheme">
<activity
android:name=".LoadingActivity"
android:label="#string/app_name" <!-- 2--- the second place-->
android:theme="#style/AppTheme.NoActionBar">
<intent-filter android:label="#string/app_name"> <!-- 3--- the third place-->
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I checked on my phone (LG4 - android 5.1).
I saw that place 2 print on the action bar and the process but place 3 print on under the app icon in the phone.
BUT! when I checked in another phone (Hawaii P9 - android 6.0)
I saw place 2 printed under the app icon in the phone, unlike in LG4 which was place 3 who did this.
The main reason I actually checked it, is that I don't want the title to appear in the action bar, while I want it to appear under the app icon and in the processes.
Any help from an expert?
sorry for commenting in the answer section but that's cuz of the low reputation, will you add your onCreate method code in order to tell you how to use
.setTitle(" ");
in which you set the title blank to make the app name doesn't appear if that helps you
1) You could override the action bar , to use your custom action bar layout.
getSupportActionBar.setCustomView("your custom view");
2) If you just want to hide the title , you could set the title using
getSupportActionBar.setTitle("");
getSupportActionBar.setSubtitle("");
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("");
}
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
From the answers you gave me, I figure a way to do what I need in a way.
I changed the main launcher activity to extend from AppCompatActivity instead of Activity, that way I could use your suggerstions you listed.
(Although I still wonder how to do it with the regular Activity)
The style themes was also enough for me, and it worked in a neater way!
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/colorPrimaryDark</item>
<item name="background">#color/colorPrimaryDark</item>
</style>
<style name="MyAppTheme.NoActionBar" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimaryDark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">#color/colorPrimaryDark</item>
<item name="android:actionBarItemBackground">#color/colorPrimaryDark</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
This xml style, fix the selected PrimaryColor in all of the wanted places and won't show the title in the main launch activity loading actionbar, while showing it under the icon and in the processes!

How can I achieve this theme with AppCompat?

I am trying to create this theme for one, and just one of my activities:
Since I am learning how this styles work, and I am now beginning to be aware that we have more different & incompatible ways of styling in android than atoms in the whole universe. So I've tried to learn from this site: http://jgilfelt.github.io/android-actionbarstylegenerator
However when I try to apply its theme in my project that is using AppCompat I get the
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
error type.
I am extending from ActionBarActivity in my tabbed activity and I don't feel safe going back now (someone suggested extending Activity to solve this issue), since in previous posts I was suggested to use AppCompat, and ActionBarActivity seems somehow to be AppCompat related.
I've spent all my week googling for solutions and I've came across these problems:
The official Android styling documentation is deprecated.
The code generated by Android Studio is also deprecated in method such as this.*
http://jgilfelt.github.io/android-actionbarstylegenerator is deprecated.
BUT, to the point now, this is what I have at the moment:
So, the only thing I am not being able to do is
putting the logo in the top left corner of the screen (in the action bar), in a way that only affects the action bar of this particular activity, (Done)
make the orange bar showing which tab is selected.
How can I achieve this in the most simple way?
This is my values/style.xml file:
<resources>
<!-- Base application theme. -->
<style name="SmartCampusTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/actionbar_bg_color</item>
</style>
<style name="GenericProgressIndicator" parent="#android:style/Widget.ProgressBar.Small">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:indeterminate">true</item>
</style>
</resources>
My AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="greensmartcampus.eu.smartcampususerfeedbackapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:logo="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/SmartCampusTheme">
<!-- Splash screen while connecting to the web-server -->
<activity
android:name=".HomeScreen"
android:label="#string/title_activity_home_screen"
android:parentActivityName=".AbstractPortraitActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="greensmartcampus.eu.smartcampususerfeedbackapp.AbstractPortraitActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- The activity where all actions take place -->
<activity
android:name=".ControlActivity"
android:label="#string/title_activity_home_screen"
android:parentActivityName=".AbstractPortraitActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="greensmartcampus.eu.smartcampususerfeedbackapp.AbstractPortraitActivity" />
</activity>
</application>
</manifest>
*Ps. it's funny how the most up-to-date Android Studio generates deprecated code for tabbed activities and then complains about it being deprecated as if it is your fault...
Important Note: I am a huge fan of MVC and a modularity priest (OSGi practitioner). So I would love if all these styling solutions could be all contained in the style and drawing XML files (as they should be if I correctly recall), the layout information is all contained in my layout files and my java files only contain code about behavior. For example I could just show the action bar icon with java through getSupportActionBar().setDisplayShowHomeEnabled(true), but inserting layout related stuff in java disrupts the MVC pattern. Also, the website that I mention above can create the layout with the icon in the actionbar without using a single java line. I just need to know how can that be done with AppCompat, which apparently is what I am using because other solutions are deprecated.

How to remove the Activity title from Action Bar Sherlock without losing it from the 'RecentApps' chooser?

I am having problems trying to remove the Activity title text from my Action Bar Sherlock.
I have spent(wasted) a lot of time trying to find a solution and just cannot make this work.
I have a Splash Activity on which I go full screen. But just before the Splash Activity appears, a screen appears (for a very short period of time) that has nothing but an Action Bar with the App Icon and Activity Title Text. I do not understand why it appears. Is this default Android behavior? I want to avoid this screen, or at least remove the Activity Title from the Action Bar on this screen. If I set it to "", I lose the app from the Recent Apps chooser.
Please refer to these images:
Pre Splash :
Splash Screen :
I have referred to the following examples on SO and also searched elsewhere. But nothing seems to work.
Remove the title text from the action bar in one single activity
Action Bar remove title and reclaim space
How do you remove the title text from the Android ActionBar?
How can I remove title and icon completetly in Actionbar sherlock?
Please check my Android Manifest and the theme file...
<application
android:name="com.zipcash.zipcashbetaversion.MyApplication"
android:allowBackup="true"
android:icon="#drawable/zipcash_icon"
android:label="#string/app_name"
android:logo="#drawable/zipcash_logo_small"
android:theme="#style/Theme.MyTheme" >
<activity
android:name="com.zipcash.zipcashbetaversion.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.zipcash.zipcashbetaversion.SignUpActivity"
android:label="#string/title_activity_sign_up"
android:screenOrientation="portrait" >
</activity>
And so on...
The following is my theme file:
<style name="Theme.MyTheme" parent="Theme.Sherlock.Light">
<!-- Set style for Action Bar (affects tab bar too) -->
<item name="actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
</style>
<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
<!-- define background for action bar (sets default for all parts of action bar - main, stacked, split) -->
<item name="android:background">#drawable/background</item>
<item name="background">#drawable/background</item>
<!-- set background for the tab bar (stacked action bar) - it overrides the background property -->
<item name="backgroundStacked">#color/action_bar_tab_background</item>
<item name="titleTextStyle">#style/NoTitleText</item>
<item name="subtitleTextStyle">#style/NoTitleText</item>
<item name="displayOptions">showHome|useLogo</item>
</style>
<style name="NoTitleText">
<item name="android:textSize">0sp</item>
<item name="android:textColor">#00000000</item>
<item name="android:visibility">invisible</item>
</style>
I have also written this code in my Splash Activity:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_TITLE);
actionBar.hide();
ActivityHelper.initialize(this);
setContentView(R.layout.activity_splash);
Have I made a mistake that I am overlooking. Is there something else I should do. My minimum API level is 8.
Any help will be appreciated.
You can add this to your activity:
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
When you first launch your app and Application onCreate haven't finished loading you see the empty lag screen. You can set the window background to green and remove the ActionBar for Application theme, so it will look splash-like.
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="splash_background">#86bf3a</color>
</resources>
styles.xml
<style android:name="SplashTheme" parent="#style/Theme.Sherlock.Light.NoActionBar">
<item name="android:windowBackground">#color/splash_background</item>
</style>
Make application use splash theme with green background and no ActionBar
<application
...
android:theme="#style/SplashTheme" >
<activity
android:name="com.zipcash.zipcashbetaversion.SplashActivity"
....>
<!-- .... -->
</activity>
<!--...
make all other Activities use MyTheme
...-->
<activity
android:name="com.zipcash.zipcashbetaversion.SignUpActivity"
...
android:theme="#style/Theme.MyTheme" />

Setting a Window.FEATURE_CUSTOM_TITLE creates AndroidRuntimeException

I can't seem to get past this error:
android.util.AndroidRuntimeException: You cannot combine custom titles
with other title features
I am running API > 14.
Manifest is as follows:
<activity
android:name=".activity.ActivityWelcome"
android:label="#string/app_label_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:theme="#style/My.Theme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My activity is doing the following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.welcome);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title_main);
...
Where R.layout.window_title_main is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_title"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dp" >
<TextView
android:id="#+id/textview_title"
android:drawableLeft="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="-1"/>
</LinearLayout>
Why is this not working when it seems to work for others?
This is the kind of error that will drive you to insanity and beyond.
So I happen to be using Theme.Holo as my parent theme. (The theme that my own theme extends)
The documentation says:
...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.
http://developer.android.com/guide/topics/ui/actionbar.html#Adding (first paragraph)
Ok, so when I try to go through the process above (my question) I get the error:
You cannot combine custom titles with other title features
Well, that's because, by default action bar is setup already and it won't allow you to use a custom title bar as well.
Documentation reveals that "each activity includes the action bar":
This way, when the application runs on Android 3.0 or greater, the
system applies the holographic theme to each activity, and thus, each
activity includes the action bar.
So, I will now focus my time on altering the action bar and not the title bar!
FEATURE_CUSTOM_TITLE
and
<item name="android:windowNoTitle">true</item>
Are mutially exclusive. Remove
<item name="android:windowNoTitle">true</item>
and it will work again.
As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>

Programmatically turn off android:windowActionBarOverlay style from action bar

Currently, I am using ActionBarSherlock. I want to launch SecondActivity from MainActivity.
MainActivity is using action bar with windowActionBarOverlay style turned on. SecondActivity is using action bar with windowActionBarOverlay style turned off. Hence, here is how my XML looks like.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:debuggable="false" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/ThemeWithActionBarOverlay"
android:screenOrientation="nosensor" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:theme="#style/ThemeWithoutOverlay">
</activity>
</application>
<resources>
<style name="ThemeWithActionBarOverlay" parent="#style/Theme.Sherlock">
<item name="android:windowActionBarOverlay">true</item>
<item name="abIcon">#drawable/ic_home</item>
<item name="abTitleTextStyle">#style/ActionBarCompatTitle</item>
</style>
<style name="ThemeWithoutOverlay" parent="#style/Theme.Sherlock">
<item name="abIcon">#drawable/ic_home</item>
<item name="abTitleTextStyle">#style/ActionBarCompatTitle</item>
</style>
</resources>
However, by doing so, in SecondActivity, I realize I can never have a up/back button on the top left of action bar. Although there is icon being shown, it is not pressable. Only by using back same theme (ThemeWithActionBarOverlay) as MainActivity, only up/back button will shown. However, if I let SecondActivity to use same theme as MainActivity, I find out no way to turn off windowActionBarOverlay behaviour.
// SecondActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_list_activity);
ActionBar actionBar = this.getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// How to turn android:windowActionBarOverlay attribute to false during runtime?
// actionBar.???
}
My questions are
Why the child activity has to use action bar with same theme as parent's, in order to have proper up/back button shown? Is there any way I can use different themes, yet have up/back button appears on child activity?
Is it possible to turn of windowActionBarOverlay style during runtime?
To answer your first question, you don't need to have the parent and child activity using the same theme for the 'Up' button to work. In fact, I'm working on a similar parent/child activity application, and its working just fine using two different themes( a theme without overlay for the parent, and the fullscreen theme (with overlay) for the child).
There must be another reason why it is not working...
Make sure you have defined MainActivity to be the parent of second activity. You can do that either by code, or the prefered way, in the AndroidManifest.xml:
<activity
android:name=".SecondActivity"
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Make sure that in your child activity, you have activated the 'up' navigation:
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
For your second question, try overriding the 'windowActionBarOverlay' in the 'ThemeWithoutOverlay' theme to false:
<style name="ThemeWithoutOverlay" parent="#style/Theme.Sherlock">
<item name="android:windowActionBarOverlay">false</item>
<item name="windowActionBarOverlay">false</item><
<item name="abIcon">#drawable/ic_home</item>
<item name="abTitleTextStyle">#style/ActionBarCompatTitle</item>
</style>

Categories

Resources