Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5967195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:54:32+00:00 2026-05-22T19:54:32+00:00

There are many tutorials out there and questions on SO that implement custom title

  • 0

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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:54:33+00:00Added an answer on May 22, 2026 at 7:54 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are many tutorials out there for getting started in C. However
There are many skills a programmer could have (understanding the problem, asking good questions,
There are many usability evaluation techniques that have been developed over the history of
I'm looking to implement my first Android database, but I have so many questions
I am using vbulletin 4.1.7 I've read many tutorials out there but none explais
are there any tutorials out there how to scaffolding a simple model that uses
I have to implement a client-server architecture where there are many android client located
Are there any tutorials out there that explain how I can draw a sphere
There are many techniques to enforce strong passwords on website: Requesting that passwords pass
There are many tools out there for writing and managing requirements, but are there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.