ActionBar background image - android

I've inherited the Holo Light Theme and customized the background of the ActionBar with the following:
Content of styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
</style>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
</resources>
Content of actionbar_background.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#raw/actionbar_background"
android:tileMode="repeat" />
Instead of being repeated, the image is stretched, any idea of why android:tileMode="repeat" is not applied?
Thanks in advance

Drawable d=getResources().getDrawable(R.drawable.background_image_name);
getActionBar().setBackgroundDrawable(d);
The above code sets the background image for the action bar.
Hope it helps.

Ok, thanks to Romain Guy on #android-dev IRC channel, it's a known bug on honeycomb / Android 3.0 which will be fixed on the next release. Since then, the only solution is do it from code, and it works :-)
final ActionBar actionBar = getActionBar();
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background));
background.setTileModeX(android.graphics.Shader.TileMode.REPEAT);
actionBar.setBackgroundDrawable(background);

You can easily do this thing. If you would like to change Action Bar background image then you place this code to your res/styles.xml file.
<style name="Theme.MyAppTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/Theme.MyAppTheme.ActionBar</item>
</style>
<style name="Theme.MyAppTheme.ActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/top_black_bg</item>
</style>
For this you have to select an image from "drawable" folder . Here I select an image "tp_black_bg.png"
After that don't forget to declare this theme to your AndroidManifest.xml file
<application
.
.
.
android:theme="#style/Theme.MyAppTheme" >.............</application>
Now you can reopen any XML layout file , you can easily see the effect. In the same way you can also able to change the background color of ActionBar.
Thanks.

mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.navbar));

Use getSupportActionBar() from android.support.v7 for backward compatability.

Related

Android translucent status bar but non-translucent navigation bar (if present)

I'm trying to create splash screen for android application with this article: Splash Screens the Right Way. As article says, I created LayerDrawable with two layers: background bitmap and logo (also bitmap). Logo need to be located at the bottom of screen with indent 32dp, for example. Here my drawable:
<item
android:drawable="#drawable/splash_image" />
<item
android:id="#+id/logo"
android:bottom="#dimen/margin_splash">
<bitmap
android:gravity="bottom|center"
android:src="#drawable/logo_green" />
</item>
I pointed this drawable as android:windowBackground param in my splash activity theme. I also want to have transparent status bar when splash screen is shown on devices where this feature is supported (API >=19), so I created two resource files for different android versions and in values-v19\styles.xml I pointed flag android:windowTranslucentStatus as true. Here my values/styles.xml and values-v19/styles.xml:
values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/bg_splash</item>
</style>
</resources>
andvalues-v19/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/bg_splash</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
But on some Android devices with Soft NavigationBar my logo is overlapped by it.
I tried to point android:windowTranslucentNavigation flag as false but without success.
Is any way to make Android Soft NavigationBar transparent together with
transparent status bar, or I need detect Soft NavigationBar availability in onCreate method of my splash activity and update my LayerDrawable by adding bottom indent for logo to the height of NavigationBar?
Thanks.
Did you try this?
<item name="android:navigationBarColor">#android:color/transparent</item> <item name="android:windowTranslucentStatus">false</item> <item name="android:windowTranslucentNavigation">false</item>
Also you can a little bottom padding to your bitmap item in the layer drawable.
I encountered the same problem and solved this problem by setting the following attributes.
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
This attributes is set to values-v21 directory.

Android Style Split does not work, explanation needed

i am trying to change the style of the action bar in android.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="GoetheTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/GoetheActionBar</item>
</style>
<style name="GoetheActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/base_color_dark</item>
</style>
So yeah, thats what I got. Now I have put the application theme in the android manifest to the created one (GoetheTheme). But the ActionBar does not change. If I set it to #GoetheActionBar, it does. Why does GoetheTheme style don't take the GoetheActionBar style?
Can please someone explain me the style system? I really dont get it.
Thanks in advance
Sorry, my very bad. I found out, I have to use a different parameter as parent ^^
I took a Light theme based one not android specific.
Correct:
<style name="GoetheActionBar"parent="#android:style/Theme.WithActionBar">
<item name="colorPrimary">#color/base_color_dark</item>
</style>
<style name="GoetheTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/GoetheActionBar</item>
</style>

ActionBar padding between elements:

I am trying to put padding between items in the action bar - specially my logo and the Navigation drawer icon, following is the style I am using:
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:icon">#drawable/logo_header</item>
<item name="android:actionButtonStyle">#style/MyActionButtonStyle</item>
</style>
<style name="MyActionButtonStyle" parent="#android:style/Widget.ActionButton">
<item name="android:minWidth">32dip</item>
<item name="android:padding">12dp</item>
</style>
</resources>
However these two dont seem to work:
<item name="android:minWidth">32dip</item>
<item name="android:padding">12dp</item>
Any idea what I am missing here?
I'd make the logo into a drawable layer list instead and that way i can set its padding something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/my_main_icon"
android:right="20dp"/>
</layer-list>
And if you use this please update your manifest to use this drawable as the main icon instead of the current.
update: it might be better to use this icon as a logo as per reading on stackOverFlow.
so in your manifest in application tag set android:logo="my_main_icon" , researching this still.
update: if you use logo i think you have to turn it on in the actionbar...
setDisplayUseLogoEnabled() Enables the use of an alternative image (a
"logo") in the Action Bar, instead of the default application icon. A
logo is often a wider, more detailed image that represents the
application
i think the correct syntax here would be:
<item
...
android:layout_padding="12dp"
...
/>

Setting background color of android actionbar

I am operating with the compatibility library v7.
I am just trying to set the color of my actionbar (for Android 2.1 and above - though I run Android 4.4.2) to a solid color.
However the color does not change. It remains the same.
I have also tried creating a solid drawable with the color but that also does not change.
Finally I tested if I could change the backgroudn of my layout and I could - it must be something about the actionbar background which I'm not getting.
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#0000ff</item>
</style>
</resources>
This seems to work for me. Try to use a resource instead of a raw value.
<style name="app_theme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/app_action_bar</item>
</style>
<style name="app_action_bar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#color/google_lightest_gray</item>
<item name="android:icon">#android:color/transparent</item>
<item name="android:windowActionBarOverlay">false</item>
</style>
use getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#808080"))); in Activity extends SherlockActivity or the color as your wish :)
You can use:
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.colorname));
use the following code getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg_color.xml));
actionbar_bg_color.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/actionbar_bg"/> <!-- set desired #0000ff color in color.xml
you can use here solid color as well as gradient -->
</shape>
hope this help you
I solved it by:
Properly installing the support-v7-appcompat library.My mistake.
Shifting the styling to the res/values/styles.xml files for each version, i.e. one for backwards compatibility and one for values-v11.
I don't know why I couldnt get the result I wanted within themes.xml.
If anyone has an answer I'd be grateful

Change ActionBarSherlock background color

I'm trying to implement ActionBarSherlock because I was told it is relatively easy to implement and customize. I've found it was pretty easy to implement, but I'm trying to change the background color of the ActionBar and it's proving difficult.
According to the the site (link), it seems you can inherit one of the ActionBarSherlock's themes and then override the properties you need.
This is what I have so far:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="Theme.ActionBar" parent="Theme.Sherlock.ForceOverflow">
<item name="android:background">#000000</item>
<item name="background">#000000</item>
</style>
</resources>
I'm noticing the built-in theme are using images for the background, but I'm praying I don't have to create images to change the background color.
Thanks.
The action bar background color is defined in a style for the action bar, not in the theme itself. You'll need to do something like this:
<style name="Theme.MyTheme" parent="Theme.Sherlock.ForceOverflow">
<item name="actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.MyTheme.ActionBar</item>
</style>
<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
<item name="android:background">#ff000000</item>
<item name="background">#ff000000</item>
</style>
Be careful using colors defined in XML. ColorDrawable did not respect it's view bounds on pre-Honeycomb so if you use tab navigation with a separate background for the stacked tab view you will have problems.
I just used
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00853c")));
It changed the background color. Hope it helps.
The code mentioned by Jake Wharton is true. But when applying the code to the styles.xml may not work if you have minSDK<11 because android:actionBarStyle is supported in API-11+
To solve that error:
Make a folder of values-v11 in your res folder and create the XML file as mentioned above.

Categories

Resources