I am currently trying to change the style of my action bar to accomplish the following:
Changing the color of the action bar
Changing the logo of the action bar
Removing the activity name from the action bar
In order to do so, this is what I have tried:
Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shoppinglist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppThemeWithoutActionBarTitle">
<activity
android:name="com.example.shoppinglist.MainActivity"
android:label="#string/app_name"
android:logo="#drawable/mylogo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And in styles.xml:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#ffffff</item>
</style>
<style name="ActionBarWithoutTitle" parent="#style/ActionBar">
<item name="android:displayOptions">useLogo</item>
</style>
<style name="AppThemeWithoutActionBarTitle" parent="AppTheme">
<item name="android:actionBarStyle">#style/ActionBarWithoutTitle</item>
</style>
</resources>
However, when I tried this, everything worked except for replacing the app logo: instead of the new logo #drawable/mylogo, no logo appears at all. How can I fix this?
You are close to the right answer. All you need is to modify one element in your styles.xml file to the following:
<style name="ActionBarWithoutTitle" parent="#style/ActionBar">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
The addition of showHome| will allow for the logo you need to be present.
You need to set in your Activity whether you want to use the launcher icon or the logo icon. So add this line to your Activity in onCreate:
setDisplayUseLogoEnabled(true);
Also your approach can be done programatically as well:
getActionBar().setTitle("");
getActionBar().setLogo(R.drawable.my_logo);
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.my_background_drawable));
This would of course be a good solution, if you wanted to add different styles to different Activities, but would be a bit cumbersome, if you have more Activities and wanted the same style all over.
Related
I've a newly created project and the first thing I want to do is add and style an action bar. I made it the way they tell in their official tutorials and even tried some other ways but I just can't make it work.
After many tries, I decided to do something really basic but even like that I can't make it work.
This is what I have:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/blue</item>
</style>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
Another question is what is the difference between the #android:style/Theme.Holo, #style/Theme.AppCompat.Light.DarkActionBar or simply Theme.AppCompat.Light.DarkActionBar?
Thanks.
Per the Using the Material Theme training, the Material theme (and AppCompat theme's which backport / use Material theming) use colorPrimary to color the action bar. Therefore your theme can be:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/blue</item>
</style>
You'll find more details on how to theme using AppCompat / Material in the AppCompat v21 blog post and in this pro-tip
I recommend you use this tools to generate ActionBar styles:
http://jgilfelt.github.io/android-actionbarstylegenerator/
Good Luck!
I am trying to give colour/image as background to the action bar in android studio (), max sdk ver 21, but with no success. What is wrong with the below code and or else how to do it:
<resources>
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar">
<item name="android:background">#drawable/back</item>
</style>
</resources>
If you are facing problem in android lollipop actionbar, then first of all you have to different style.xml file in your res folder named as values-v21.
in this create style.xml as in values folder.
you did not added parent theme correctly. you should create style1 whose parent is style2. In style2 parent add desired theme like parent="#android:style/ThemeOverlay.Material.Light".
In menifest add style1 as your application theme.
Take a look on example.
Here colorPrimary for your action bar,
colorPrimaryDark for your notification bar outside application,
colorAccent for native dialogs like progressbar, tabs etc.
<resources>
<style name="AppBaseTheme" parent="#android:style/ThemeOverlay.Material.Light">
<item name="android:colorPrimary">#color/material_blue_500</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/material_blue_700</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">#color/material_green_A200</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
your xml file should be like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.slidingmenu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="info.androidhive.slidingmenu.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
If you haven't already, remember to reference your theme in AndroidManifest.xml:
android:theme="#style/CustomActionBarTheme"
Also, make sure that in your Java code, you activity extends ActionBarActivity instead of just Activity as you are using the AppCompat Support Library.
If none of this works, go to your build.gradle and check that in the dependencies section, you have:
compile 'com.android.support:appcompat-v7:21.0.3'
I've a newly created project and the first thing I want to do is add and style an action bar. I made it the way they tell in their official tutorials and even tried some other ways but I just can't make it work.
After many tries, I decided to do something really basic but even like that I can't make it work.
This is what I have:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/blue</item>
</style>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
Another question is what is the difference between the #android:style/Theme.Holo, #style/Theme.AppCompat.Light.DarkActionBar or simply Theme.AppCompat.Light.DarkActionBar?
Thanks.
Per the Using the Material Theme training, the Material theme (and AppCompat theme's which backport / use Material theming) use colorPrimary to color the action bar. Therefore your theme can be:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/blue</item>
</style>
You'll find more details on how to theme using AppCompat / Material in the AppCompat v21 blog post and in this pro-tip
I recommend you use this tools to generate ActionBar styles:
http://jgilfelt.github.io/android-actionbarstylegenerator/
Good Luck!
i want to disable the ActionBar in my application, I have been looking for a solution on stackoverflow for quiet a while but nothing helped. My problem is that my custom-theme "NoActionBar" is not used, instead it keeps using "AppBaseTheme" although I changed it in the manifest:
android:theme="#style/NoActionBar"
values-v14 styles:
<resources>
<!--
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="Theme.AppCompat.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
<style name="NoActionBar" parent="AppBaseTheme">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
same with values-v11. In values i added this line but I think its not neccessary:
<style name="NoActionBar" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
</style>
I tried to change the parent of NoActionBar, didn't work. I tried to create an extra .xml for "NoActionBar" but that didn't work either because the manifest didn't find it. I tried other tipps from stackoverflow questions but they're pretty outdated.
I know when i put the two "items" within the "AppBaseTheme"-style it works, but I'm pretty sure that is not the proper way to use it?
In a nutshell:
Can somebody please explain to me why its not using my theme? What theme needs which parent? (How would I define multiple themes?)
I hope I explained it properly, I assume the answer is rather simple, sorry in advance for my language mistakes, I'm not native. Thanks!
Edit Manifest:
<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/NoActionBar" >
<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>
You are using your theme but your parent can't be AppBaseTheme, you have to use as parent for example Theme.Holo.NoActionBar or at least a style without action bar.
short and simple question:
I use the actionBarSherlock library, and wish to have only the app's label set to some string, yet all activities have the empty label unless specified otherwise.
I know you can go over each activity and set its label to "" , but is there a better solution? like using the styles ? I've tried putting :
<item name="android:label"></item>
in there, but it didn't do anything.
EDIT: fact is, for some reason, setting the label of all activities to "" , actually also change the label of the app on some android versions (tested on galaxy s mini, with android 2.3) , so its name is "" too. How come? is it a bug?
This is a bug on either android or any launcher i've tested on.
it seems that setting the label to the activities to "" (or at least the main one) sets the name of the app to "" , so the label of the application tag is just for the default value of all of its activities.
You can remove the whole title bar by putting <item name="android:windowNoTitle">true</item> in your app theme. I don't think you should have a title bar without a title (unless on devices running 3.0 or higher, where the title bar becomes the ActionBar and has more functionality), as it'll just waste valuable space.
If you are developing on 3.0+, as well as 2.3, add the following elements to your theme:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.ActionBar">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
EDIT:
styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Holo">
<!-- Base Theme style elements -->
</style>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Other style elements. -->
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="AppThemeWithTitle" parent="AppTheme">
<item name="android:actionBarStyle">#style/ActionBarWithTitle</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.ActionBar">
<!-- Other ActionBar style elements. -->
<item name="android:displayOptions">showHome|useLogo</item>
</style>
<style name="ActionBarWithTitle" parent="ActionBar">
<item name="android:displayOptions">showHome|useLogo|showTitle</item>
</style>
</resources>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.themes"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.themes.Activity1"
android:theme="#style/AppThemeWithTitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.themes.Activity2" />
<activity
android:name="com.example.themes.Activity3" />
</application>
</manifest>
Activity1 will have the title displayed but Activity2 and Activity3 will not.