Reputation: 1040
I created a custom style for my action bar on my android project:
<!-- Style for the Login Menu Toolbar -->
<style name="LoginMenu">
<item name="android:colorBackground">@color/menu</item>
<item name="android:textColor">@color/menuFont</item>
</style>
And I have a Action Bar.xml which I want to add this style:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
</menu>
As you can see, the xml file with the action bar is empty right now, and I can't figure how to add the style to it...
My objective is to create a custom Action Bar with different background colors, how should I do this?
Upvotes: 0
Views: 848
Reputation: 2448
This will work for you. You can change the colors of toolbar in colors.xml. colorPrimary is for ToolBar and colorPrimaryDark is for status bar
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.newapp.toolbarwork.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!-- <include layout="@layout/content_main" />-->
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
package com.newapp.toolbarwork;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Upvotes: 2
Reputation: 8231
I would recommend the following. You will need to ensure you have the AppCompat
library dependency in your gradle.build
file (i.e. compile 'com.android.support:appcompat-v7:23.1.1'
, an example gradle file is here)
First, ensure your app's theme has a Theme.AppCompat.*
parent. Then, assign colorPrimary
item with the preferred colour for your action bar. For example, in your styles.xml
:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/my_action_bar_color</item>
</style>
</resources>
Second, ensure that AppTheme
is your application's theme in your manifest
.
Finally, your Activity
classes will need to extend AppCompatActivity
rather than Activity
.
Upvotes: 1