1
votes

I'm changing toolbar color like this:

MainActivity.xml

    <Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></Toolbar>

styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:colorPrimary">#0000ff</item>
    <item name="android:colorPrimaryDark">#ff0000</item>
    <item name="android:textColorPrimary">#fff</item>
    </style>

and MainActivity.java

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setActionBar(toolbar);

I can change color colorPrimaryDark and textColorPrimary but can not change colorPrimary, why?

I can change toolbar color like this:

toolbar.setBackgroundColor(Color.parseColor("#0000ff"));

but cant change using style also why after removing setActionBar(toolbar); the text and title overflow menu gone why?

4
post ur java code,of oncreateAk9637
posted oncreateblackHawk

4 Answers

2
votes

From XML

android:background:"your color"

From code

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
ActionBar actionBar = getSupportActionBar();

actionBar.setBackgroundDrawable(Color.parse("your color"));
0
votes

You may remove NoActionBar from parent theme name

0
votes

put these line

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 ActionBar actionBar = getSupportActionBar();

            actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
0
votes

Use this theme Theme.AppCompat.Light.NoActionBar instead