Changing theme from default in the manifest - android

I have an app in the play store: Just Notes
I recently added themes to the app.
The problem is, the status bar color and the default background color always default to the theme that is mentioned in the manifest.
All the other themes have the colorPrimaryDark mentioned in them but Android just does not use it for setting the Status bar color at runtime.
How to properly override the theme mentioned in the manifest
eg.
Following are my Styles
<style name="AppBaseThemeDark" parent="Theme.AppCompat">
<item name="colorPrimary">#color/primary_material_dark</item>
<item name="colorPrimaryDark">#color/primary_dark_material_dark</item>
<item name="colorAccent">#color/accent_material_dark</item>
<item name="android:textColor">#color/abc_primary_text_material_dark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="AppBaseThemeLight" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary_material_light</item>
<item name="colorPrimaryDark">#color/primary_dark_material_light</item>
<item name="colorAccent">#color/accent_material_light</item>
<item name="android:textColor">#color/abc_primary_text_material_light</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
Following is my Manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppThemeDark" >
<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>
The manifest has the app theme set to dark.
In my activity, this how I am setting the theme based on what is saved in the shared pref.
switch (mPrefs.getInt("theme", -1)) {
case 0:
setTheme(R.style.AppThemeDark);
break;
case 1:
setTheme(R.style.AppThemeLight);
break;
default:
//keep the theme from the manifest.
}
If the user has selected the light theme, the status bar still remains black as well as the background.
If I change the theme in manifest to AppBaseThemeLight, then the status bar remains grey even for the dark theme.

Since this was gonna be only for Lollipop, I fixed it using a custom attribute to set the background based on the theme.
And for the status bar color, I used
getWindow().setStatusBarColor(getResources().getColor());

Related

Custom PopupMenu styles are being ignored

I've been struggling with PopupMenu styling, and after a lot of research, the process of creating a custom PopupMenu style for my theme seemed to be fairly straightfoward. For example the answer on this question seems like an easy way to change the background color of a PopupMenu. However, none of these solutions have worked for my project. So I started digging into the style sources to find out what I should actually be inheriting to style the PopupMenu in my own project.
I'm running my tests on a handset running API 19, and my base theme looks like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="android:popupMenuStyle">#style/CustomPopupMenu</item>
</style>
<style name="CustomPopupMenu" parent="#style/Widget.AppCompat.Light.PopupMenu">
<item name="android:popupBackground">#color/retina_burning_hot_pink</item>
</style>
This is the solution that most of the accepted answers here show. But this does not style my PopupMenu. So I dug into the sources to find the geneology of my theme and see what default settings where being used. This is what I found:
<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar"/>
<style name="Base.Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light">
<style name="Base.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light"/>
And then in Base.V7.Theme.AppCompat.Light I found these default settings:
<!-- Popup Menu styles -->
<item name="popupMenuStyle">#style/Widget.AppCompat.Light.PopupMenu</item>
<item name="textAppearanceLargePopupMenu">#style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large</item>
<item name="textAppearanceSmallPopupMenu">#style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small</item>
<item name="listPopupWindowStyle">#style/Widget.AppCompat.ListPopupWindow</item>
<item name="dropDownListViewStyle">?android:attr/dropDownListViewStyle</item>
So as far as I can tell, my theme inherits from Theme.AppCompat.Light.DarkActionBar which uses a default PopupMenu style of #style/Widget.AppCompat.Light.PopupMenu. I then inherit that style to set my own PopupMenu settings. I made sure my Activity and my Application are all using the correct Theme. I'm not sure why else this just won't work. I've also tried these settings:
<item name="android:textAppearanceLargePopupMenu">#style/CustomPopupMenuLarge</item>
<item name="android:textAppearanceSmallPopupMenu">#style/CustomPopupMenuSmall</item>
<item name="android:popupWindowStyle">#style/CustomPopupMenuListWindow</item>
I provided custom styles for each one, and none of them worked. What could be overriding all my custom styles? Any help would be appreciated, because I'm stumped.
EDITS
Here's my manifest...
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blah.blah">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Activities.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is AppTheme.NoActionBar
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Do not specify android: in
<item name="android:popupMenuStyle">#style/CustomPopupMenu</item>.
Instend:
<item name="popupMenuStyle">#style/CustomPopupMenu</item>
I think you need to define a parent for AppCompat.NoActionBar. Try parent="Theme.AppCompat.Light.NoActionBar" and also add in <item name="android:popupMenuStyle">#style/CustomPopupMenu</item> to it.

How to make a transparent status bar in Android 4.4?

I want to make a transparent status bar in my application in Android 4.4.
How do I do that?
I tired:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/skipPrimary</item>
<item name="colorPrimaryDark">#color/skipPrimary</item>
<item name="colorAccent">#color/skipPrimary</item>
</style>
and in the manifest:
<application
android:name=".App"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme" >
and in the activity:
<activity
android:name=".activities.StartActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
android:screenOrientation="portrait"
android:theme="#style/AppTheme"
android:windowSoftInputMode="adjustResize|stateHidden" />
This one will throw exception on Lollipop devices. primColor must be opaque.
<item name="primColor">#android:color/transparent</item>
Style your actionbar using style.xml
<style name="ThemeActionBar"
parent="Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:background">#null</item>
<!-- Support library compatibility -->
<item name="background">#null</item>
</style>
Include you theme..
<item name="android:actionBarStyle">#style/ThemeActionBar</item>
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/ThemeActionBar</item>
<item name="windowActionBarOverlay">true</item>
What you can also do is set the status bar a darker shade of your background or even the same color by setting it in your activity.
You can set the status bar color in your activity with the following code getWindow().setStatusBarColor(getResources().getColor(R.color.your_color));.
If you want to use a darker shade of your color you can do it by changing your color's HSB. For a more detailed way of doing that you can read this post: Android change status bar color by converting existing color's HSB

Android: Change actionbar style

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.

Remove app title bar android

I know this question has been asked million times but whichever method I use is not good for me.
If I use
android:theme="#android:style/Theme.NoTitleBar"
in the manifest file, the whole theme of the app changes. If I put
requestWindowFeature(Window.FEATURE_NO_TITLE);
in the onCreate activities, it shows up shortly when starting an app.
Btw I don't have any theme selected, just using standard android.
It is unclear what you mean by "whole theme of app changes". For API Level 11+ devices, you probably should be using #android:style/Theme.Holo.NoActionBar.
If you add the theme to the application, it applies to the whole app.
android:theme="#android:style/Theme.NoTitleBar"
You have to add it to the activity declaration.
<activity
android:name=".YourActivity"
android:theme="#android:style/Theme.NoTitleBar"/>
To remove the title bar from your app in android studio
Open res > values > styles.xml file
Replace your code with:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Open your style.xml file, create a new style just like below.
You choose your own name and set the parent as below
<style name="your_own_name" parent="Theme.AppCompat.Light.NoActionBar">
</style>
In your AndroidManifest.xml file, set the theme name for the specific activity you dont want the bar to show and you are done
<activity android:theme ="#style/your_own_name" >
</activity>
This is what worked for me
android:theme="#style/Theme.AppCompat.NoActionBar">
when you create a new project in android then you will get default theme applied on your project, that has title bar. but if you want to remove or apply some other theme for your whole App then you can define like below at application level:
<application
android:name="com.mycomp.myproj.MainApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock" >
but if you want to apply some theme only at some specific screens then you can define like below at activity level:
<activity
android:name="com.mycomp.myproj.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
For android 8 and above support. Use this in your app theme file. I've modified my default in res/styles.xml :
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#color/white</item>
</style>
Inside AndroidManifest
<activity
android:name=".activity.HomeMenu"
android:screenOrientation="nosensor"
android:theme="#android:style/Theme.Light.NoTitleBar">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and make sure your Class extends to Activity
If you have made a custom theme in styles.xml you can do this:
<style name="AppTheme" parent="android:Theme.Material.Light">
<!-- Override the android colour scheme etc. -->
<item name="android:colorPrimary">#color/color_primary</item>
</style>
To remove the Action bar and title add to styles.xml:
<style name="AppTheme.NoActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
Then in your AndroidManifest.xml you can use your custom theme with or without the action bar
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"> <---- With action bar
<activity
android:theme="#style/AppTheme.NoActionBar"> <---- No action bar
<intent-filter>
etc...

How to set label for app, but empty for all activities?

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.

Categories

Resources