How to change the color of Actionbar in the eclipse?

Asked

Viewed 438 times

0

I’m studying how to change the color of mine ActionBar, but I can’t. I’ve tried everything but without success.

This is my MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

And my file style.xml:

<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!-- Este "colorPrimary" não funcionou -->
        <item name="colorPrimary">@color/material_blue_grey_900</item>
    </style>
    <style name="AppTheme" parent="AppBaseTheme">
    </style>
</resources>

1 answer

2

Traditional Actionbar evolves to the use of Toolbar, which is much more flexible. I recommend that you use the principles of Material Design and make use of Toolbar, so you can change your style in several ways, after all it is only a View.

<android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar_sort"
        android:title="Toolbar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:background="@color/theme_primary_dark" />

To set the Toolbar as Actionbar

 setSupportActionBar(toolbar);

Don’t forget to add in graddle dependencies

compile "com.android.support:appcompat-v7:22.2.1"

EDIT: No eclipse you can follow these footsteps to include the libs

EDIT 2: I didn’t mention it here, but you must change your Theme by removing Actionbar to be able to use Toolbar as Actionbar correctly.

<style name="Theme.MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="actionBarStyle">@style/MyActionBarStyle</item>
    <item name="colorPrimary">@color/theme_primary</item>
    <item name="colorPrimaryDark">@color/theme_primary_dark</item>
    <item name="colorAccent">@color/theme_accent</item>
</style>

<style name="MyActionBarStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">@color/theme_primary_dark</item>
</style>
  • Thanks tim ! Well, I forgot to mention that you are in the eclipse that does not have Gradle script. I think this version "Mars", already comes with functions of Gradle integrated. I still do not know how to assign new " Theme"to my project.

  • Hello Ralften. I don’t use Eclipse and I don’t know how to help you very well in this. I put a link in the answer that should help you. However the content is in English. Good luck.

  • Blza! I am using in my project the appcompat v7.. which is already integrated in the project. I don’t know if there is, but I don’t have all the Android versions installed only API 21, API 10.. I thought it would be so simple but it’s been a nightmare to change the background color of actionbar.. #upset

  • Using Toolbar is easier. Do this. I would also recommend you to download Android Studio, which is very good.

  • I added the edition you need to make in your Theme to make your life easier.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.