• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help with gradient in toolbar

FSchmidt

Lurker
Hello androidForums,
I want to create a toolbar with a gradient in the background. The problem is that also title and subtitle have a gradient background and I didn't find any possible way to set the textbackground transparent.

This is my xml so far:
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:title="Title"
    app:subtitle="Subtitle"
    app:subtitleTextColor="@color/White"
    android:fitsSystemWindows="true"
    android:theme="@style/ToolbarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>

What do I have to change?

Kind regards
FSchmidt
 
I don't have a fix for your issue since I use a different method to create my toolbars. But I'll show you how I set title and subtitle colors and font size.

Here's my toolbar layout called toolbar.xml. As you can see I set an id to reference from java...

XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mytoolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" // sets whatever the primary color of my app is
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>

Here's how I add the toolbar in my java classes in onCreate()...

Java:
Toolbar toolbar = findViewById(R.id.mytoolbar); // id set in toolbar.xml layout
toolbar.setTitleTextAppearance(this, R.style.toolbarTitle); // title theme set in styles.xml
toolbar.setSubtitleTextAppearance(this, R.style.toolbarsubTitle); // subtitle theme set in styles.xml
setSupportActionBar(toolbar);
if(getSupportActionBar() != null){
    getSupportActionBar().setElevation(8); // sets a shadow under the toolbar
}

Here's the font theme for toolbar title/subtitle in my styles.xml...

XML:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> // App theme example just for ideas
    <item name="colorPrimary">#3F51B5</item>
    <item name="colorPrimaryDark">#3949AB</item>
    <item name="colorAccent">#FFC107</item>
    <item name="colorButtonNormal">#3F51B5</item>
    <item name="colorControlHighlight">#40000000</item>
</style>

<style name="toolbarTitle"> // title
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">22sp</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="toolbarsubTitle"> // I don't use subs in toolbar, but this is a functional example
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>
</style>

My titles are set dynamically from my manifest by setting activity labels in manifest. Each activity will have it's own label(title)...

XML:
<activity
    android:name=".CurrentSettings"
    android:screenOrientation="portrait"
    android:label="@string/current_settings" />

Then to use my toolbar I add the following to all my activity layout xmls at the top of the layout like this...

XML:
<include layout="@layout/toolbar" />

I know this is probably not exactly what you were looking for, but I hope it's of some help to you or someone else who reads this.

Cheers

edited:
Added syntax highlighting to code tags.
 
Last edited:
Hello,

now it's a long time ago ;)
The solution is really easy:

I've used android:background:<path to gradient drawable>

To prevent this ugly mistake, I just had to change it to android:drawable<path to gradient drawable>

I also removed the parent of my toolbar-style.

These two changes fixed the problem.


Kind regards and thank's a lot for your response!
F.S.

//Closed
 
Back
Top Bottom