How to set theme at runtime in android - android

I tried to set theme at run time, other than action bar everything works fine.
I am setting theme in oncreate() before super.oncreate(...)
How to set theme in action bar ?
This is my theme in style.xml
<style name="abcThemeTest" parent="Theme.AppCompat.Light">
<item name="android:windowBackground">#color/RED</item>
<item name="actionBarStyle">#style/MyActionBarTest</item>
</style>
<style name="MyActionBarTest" parent="#style/Widget.AppCompat.ActionBar">
<item name="background">#color/RED</item>
<item name="android:background">#color/RED</item>
<item name="titleTextStyle">#style/MyActionBarTitleTextTest</item>
</style>
<style name="MyActionBarTitleTextTest" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/WHITE</item>
<item name="android:textStyle">bold</item>
</style>

Simply Modify your style file like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">#color/actionbar_text</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="actionMenuTextColor">#color/actionbar_text</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
<!-- Support library compatibility -->
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
<!-- ActionBar tabs text -->
<style name="MyActionBarTabText"
parent="#style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">#color/actionbar_text</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
</resources>

Related

How to change ActionBar Colour

I am trying to change my App ActionBar colour to no success. I am using the this library https://github.com/neokree/MaterialNavigationDrawer;
Here is the style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="colorControlActivated">#color/color_c</item>
<item name="toolbarStyle">#style/Widget.Toolbar</item>
</style>
<style name="Widget.Toolbar" parent="#style/Widget.AppCompat.Toolbar">
<item name="contentInsetStart">5dp</item>
</style>
<style name="AppTheme.ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/color_a</item>
<item name="background">#color/color_a</item>
<!-- Support library compatibility -->
</style>
<style name="FullscreenTheme" parent="android:Theme.NoTitleBar">
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#null</item>
</style>
<style name="MyNavigationDrawerTheme" parent="MaterialNavigationDrawerTheme.Light.DarkActionBar">
<item name="colorPrimary">#color/color_a</item>
<item name="colorPrimaryDark">#color/color_a</item>
<item name="colorAccent">#FFFFFF</item>
<item name="rippleBackport">true</item>
<item name="singleAccount">true</item>
<item name="multipaneSupport">true</item>
<!--<item name="learningPattern">false</item>-->
</style>
<style name="MySubheaderTheme">
<item name="subheaderTitleColor">#000000</item>
</style>
<style name="MySectionTheme" >
<item name="sectionColorIcon">#000000</item>
<item name="sectionColorText">#000000</item>
<item name="sectionColorNotification">#000000</item>
<item name="sectionBackgroundColorPressed">#160000FF</item>
<item name="sectionBackgroundColorSelected">#0A0000FF</item>
<item name="sectionBackgroundColor">#000000FF</item>
</style>
</resources>
I use the MyNavigationDrawerTheme theme for my activities.
You should be able to set the action bar color using the android:colorPrimary attribute:
<resources>
<!-- inherit from the material theme -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">#color/accent</item>
</style>
</resources>
Based on information here
You should create your styles also for v21 and above with a small change (because they natively support material theme).
And in values-v21/style.xml add android:colorPrimary and others attributes with android: prefix to your style:
<style name="MyNavigationDrawerTheme" parent="MaterialNavigationDrawerTheme.Light.DarkActionBar">
<item name="android:colorPrimary">#color/color_a</item>
<item name="android:colorPrimaryDark">#color/color_a</item>
<item name="android:colorAccent">#FFFFFF</item>
<!--...-->
</style>

Android action bar's title color not changing

I would like to change the contextual action bar's title text color and back button colour.
Please find the code i am using, still i couldn't able to achieve
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:actionModeBackground">#color/dark_blue_Shade1</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
Please help me to fix
</style>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyTheme.ActionBarStyle</item>
</style>
<style name="MyTheme.ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="android:subtitleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="android:background">#0061C2</item>
<item name="android:icon">#android:color/transparent</item><!--hiding the icon ..in case you want it..-->
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#ffffff</item>
</style>
and in the manifest ...you can apply this theme for your app
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyTheme" ><!--this is that theme-->
or just to one of your activities
<activity
android:name="eddine.charef.mechalikh.swipedemo.MainActivity"
android:label="#string/mainActivity"
android:theme="#style/MyTheme"
>
that's all
Edit: i found this in android developpers website
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">#color/actionbar_text</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="actionMenuTextColor">#color/actionbar_text</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
<!-- Support library compatibility -->
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
<!-- ActionBar tabs text -->
<style name="MyActionBarTabText"
parent="#style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">#color/actionbar_text</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
</resources>
here is the link https://developer.android.com/training/basics/actionbar/styling.html

Android action bar autosrollable title

I'd like to create app with setTitle function at ActionBar
But, some strings are bigger than ActionBar width, and i want use autoscrollable function for my title.
I try to create style for my ActionBar:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar.Solid">
<item name="android:background">#drawable/ab_background</item>
<item name="android:titleTextStyle">#style/ActionBarTitleStyle</item>
<!-- Support library compatibility -->
<item name="background">#drawable/ab_background</item>
<item name="titleTextStyle">#style/ActionBarTitleStyle</item>
</style>
<style name="ActionBarTitleStyle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:ellipsize">marquee</item>
<item name="android:marqueeRepeatLimit">1</item>
<item name="android:singleLine">true</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:textColor">#FF64A3</item>
</style>
But it doesn't work.
Please tell me what i do wrong?

Actionbar Actionmenu item icon not visible

This is my menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.citrusz.MainActivity" >
<item
android:id="#+id/action_offers"
android:showAsAction="ifRoom"
android:title="#string/captain_offers"/>
<item
android:id="#+id/action_events"
android:showAsAction="ifRoom"
android:title="#string/events_list"/>
<item
android:id="#+id/action_user"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:title="#string/action_user"/>
And this is my ActionBar
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="actionMenuTextColor">#android:color/white</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<!-- Support library compatibility -->
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
<item name="background">#drawable/actionbar_background</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
<!-- ActionBar tabs text -->
<style name="MyActionBarTabText" parent="#style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">#android:color/white</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
</resources>
And I getting following output, I need Logout should be visible in ActionBar.
Try changing this
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="actionMenuTextColor">#android:color/white</item>
</style>
To
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="actionMenuTextColor">#android:color/white</item>
</style>

android add background to action bar

I am using support library to add an action bar.
in res/values/styles:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme"></style>
<style name="Theme.Transparent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Translucent</item>
<item name="android:windowBackground">#drawable/transparent_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">#color/actionbar_text</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="actionMenuTextColor">#color/actionbar_text</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
<!-- Support library compatibility -->
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
<!-- ActionBar tabs text -->
<style name="MyActionBarTabText"
parent="#style/Widget.AppCompat.ActionBar.TabText">
<item name="android:textColor">#color/actionbar_text</item>
<!-- The textColor property is backward compatible with the Support Library -->
</style>
</resources>
and then when i add this
application android:theme="#style/CustomActionBarTheme"
to mainfest, the application falls to start. if i removed it the application works perfectly
help pelase
Try using: Action bar stylle generator. It's simple and works perfectly for default, ActionBarSherlock, Holoeverywhere themes.

Categories

Resources