44

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

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
rnoway
  • 8,571
  • 4
  • 19
  • 9

5 Answers5

44
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.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Raj Trivedi
  • 557
  • 7
  • 18
43

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);
Vinay W
  • 9,912
  • 8
  • 41
  • 47
rnoway
  • 8,571
  • 4
  • 19
  • 9
  • 1
    May i ask how you did it from code? How to set to be tileMode repeat? I always got null when try to get the action bar – Alex May 12 '11 at 13:41
  • 8
    I've done it like this: 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); – rnoway May 13 '11 at 06:17
  • 1
    @rnoway , it would be better to add your comment as an edit in your answer. You should mark the answer as resolution, because it fixes the problem. – Mario Kutlev Jun 21 '13 at 21:27
  • For me it works if I call `invalidateMenuOptions` after `setBackgroundDrawable` without making any assumptions about the type of `Drawable` – gunar Aug 14 '13 at 06:11
  • @rnoway: Why did you define actionBar property final? How defining it final affects it? Sorry i'm new to Java and Android. Thanks – Mustafa Chelik Dec 03 '14 at 22:14
5

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.

Satyaki Mukherjee
  • 2,857
  • 1
  • 22
  • 26
3
mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.navbar));
Naskov
  • 4,121
  • 5
  • 38
  • 62
2

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

Ron
  • 91
  • 1
  • 5