Reputation: 3762
Can anyone please, explain me, why my toolbar doesn't change its color even though I created another style like:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
</style>
added it in my manifest file like:
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:theme="@style/AppTheme.NoActionBar"
android:name="android.support.PARENT_ACTIVITY"
android:value="com.com.com.com.MainActivity" />
</activity>
and try to change the toolbar like:
Toolbar toolbar = new Toolbar(this);
toolbar.setBackgroundColor(getResources().getColor(R.color.light_blue));
setSupportActionBar(toolbar);
but still get
This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
I even tried to getSupportActionBar().hide();
but still doesn't work.
XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.com.com.com.SettingsActivity"
android:id="@+id/settings_activity">
</RelativeLayout>
Basically, what I want is to keep my global theme and just change toolbar color in one of the activities. That is why I don't create a toolbar inside xml file, I create it programmatically, add background color and want to set it.
Upvotes: 3
Views: 3728
Reputation: 1835
This might help you.
here's styles.xml
contains custom theme.
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
<style name="AppTheme" parent="AppBaseTheme"></style>
<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>
AndroidManifest.xml
contains custom theme for SettingActivity
<activity
android:name=".SettingActivity"
android:label="@string/title_activity_setting"
android:theme="@style/CustomTheme" >
</activity>
Layout
file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/settingRootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.demo.SettingActivity" >
</RelativeLayout>
finally your Activity file
package com.example.demo;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
@SuppressLint("NewApi")
public class SettingActivity extends AppCompatActivity {
private RelativeLayout rootLayout;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
rootLayout = (RelativeLayout) findViewById(R.id.settingRootLayout);
mToolbar = new Toolbar(this);
mToolbar.setLayoutParams(
new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mToolbar.setBackgroundColor(getResources().getColor(R.color.light_blue));
mToolbar.setFitsSystemWindows(true);
rootLayout.addView(mToolbar);
setSupportActionBar(mToolbar);
}
}
Upvotes: 0
Reputation: 3594
Hope the below code helps you...
Create the theme in your styles.xml as follow:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
Make sure you have this in your gradle dependencies
compile "com.android.support:appcompat-v7:21.0.+"
In your layout xml files add this toolbar tag
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
In your manifest file add the theme you created to the acctivity
<activity
android:name="com.javatechig.sample.MyActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
And finally, here is your activity
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
public class MyActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Set a toolbar to replace the action bar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
Upvotes: 0
Reputation: 1608
I think you're getting your toolbar in the wrong way. You're probably not extending Toolbar
class.
Try to get it directly by the id defined in the xml file.
Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
Then you can set it as support and eventually set your color.
Edit: Didn't see you're creating it programmatically. Try to set a simple AppTheme style like
<style name="NoToolbarStyle" parent="Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Upvotes: 0
Reputation: 17142
You need to take off android:theme="@style/AppTheme.NoActionBar"
outside the meta-data
tag :
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="net.noorr.menote.menote.MainActivity" />
</activity>
And in java code:
Toolbar toolbar = (Toolbar) findViewById(R.id.yourToolbarId);
toolbar.setBackgroundColor(getResources().getColor(R.color.light_blue));
setSupportActionBar(toolbar);
Upvotes: 5