How do I programmatically set the background color gradient on a Custom Title Bar?
Asked Answered
S

4

97

There are many tutorials out there and questions on SO that implement custom title bars. However, in my custom title bar I have a custom gradient for the background and I would like to know how to set it dynamically in my code.

Here is where my custom title bar gets called:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.foo_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar); 

And this is my custom_title_bar:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@layout/custom_title_bar_background_colors">
<ImageView   
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:src="@drawable/title_bar_logo"
              android:gravity="center_horizontal"
              android:paddingTop="0dip"/>

</LinearLayout>

As you can see, the background on the linear layout is defined by this guy:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient 
    android:startColor="#616261" 
    android:endColor="#131313"
    android:angle="270"
 />
<corners android:radius="0dp" />
</shape>

What I would like to do is set those gradient colors dynamically in my code. I do not want to hard code them in my XML file like they currently are.

I am open to all ideas if you have a better way of setting a background gradient.

Thank you in advance!!

Susa answered 24/5, 2011 at 19:21 Comment(0)
S
248

To do this in code, you create a GradientDrawable.
The only chance to set the angle and color is in the constructor. If you want to change the color or angle, just create a new GradientDrawable and set it as the background

    View layout = findViewById(R.id.mainlayout);

    GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] {0xFF616261,0xFF131313});
    gd.setCornerRadius(0f);

    layout.setBackgroundDrawable(gd);

For this to work, I added an id to your main LinearLayout as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<ImageView   
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:src="@drawable/title_bar_logo"
              android:gravity="center_horizontal"
              android:paddingTop="0dip"/>

</LinearLayout>

And to use this as for a custom title bar

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);
    View title = getWindow().findViewById(R.id.mainlayout);
    title.setBackgroundDrawable(gd);
Sumy answered 24/5, 2011 at 20:12 Comment(6)
Thanks for the tip. Can this be done for a widget? I have a widget that has a gradient shape and I want to change the color of this gradient. Is this possible in the case of widgets?Mister
@Sumy is there any way to originate the gradient from the center? instead of using 'GradientDrawable.Orientation.TOP_BOTTOM?'Perlis
That's ok, but what to do if user's API < 16?Crayfish
@Crayfish this is the answer for your question https://mcmap.net/q/109248/-setbackground-vs-setbackgrounddrawable-androidTurrell
setBackgroundDrawable() is deprecated since, I think, API 16. You should use setBackground() as well/instead. See #27141779Longlegged
How can we set it for RemoteViews?Schaller
C
4

if you are using Kotlin then write

val drawable = GradientDrawable().apply {
        colors = intArrayOf(
            R.color.register_gradient_top,
            R.color.register_gradient_middle,
            R.color.register_gradient_bottom_middle,
            R.color.register_gradient_bottom,
        )
        orientation = GradientDrawable.Orientation.TOP_BOTTOM
        gradientType = GradientDrawable.LINEAR_GRADIENT,
        shape = GradientDrawable.RECTANGLE
    }

Source

https://www.android--code.com/2020/06/android-kotlin-create-gradientdrawable.html

Conan answered 28/5, 2022 at 5:7 Comment(0)
B
1

If accepted answer is not working, you can try to set foreground.

color.setForeground(gradient);
Burweed answered 7/11, 2019 at 8:58 Comment(0)
E
0

Answer with gradient generation is correct, but it will not work if you just set background to MaterialButton. For MaterialButton you have to nullable default backgroundTint:

app:backgroundTint="@null"

After that solution will work. (link to source: https://github.com/material-components/material-components-android/issues/889#issuecomment-621194246)

Englishry answered 9/2, 2022 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.