Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change actionbar title color in Xamarin android

How do I change my actionbar title color in Xamarin android programmatically? I tried a lot of things on the internet, but I just can't find a way to do it.

I don't want to switch to toolbar, because I don't know the code for it, I also dont know if it's possible to make a toolbar look like exactly the same as the actionbar in the code below.

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V7.App;
using Android.Graphics.Drawables;
using Android.Graphics;

namespace actionbarTitleColor
{
    [Activity(Label = "myActivity")]
    public class myActivity : Activity
    {
        protected override void OnCreate(Bundle bundle) {

            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Rapsolie);
            ActionBar.SetHomeButtonEnabled(true);
            ActionBar.SetDisplayHomeAsUpEnabled(true);
            ColorDrawable colorDrawable = new ColorDrawable(Color.ParseColor("#00b8ff"));
            ActionBar.SetBackgroundDrawable(colorDrawable);

        }

        public override bool OnOptionsItemSelected(IMenuItem item) {

            switch (item.ItemId) {
                case Android.Resource.Id.Home:
                    Finish();
                    return true;

                default:
                    return base.OnOptionsItemSelected(item);
            }
        }
    }
}
like image 731
AndreasWT Avatar asked Jan 09 '17 14:01

AndreasWT


1 Answers

The most straight-forward method is that customizing a style for your ActionBar, for example like this:

<resources>
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  </style>

  <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#f08080</item>
  </style>
</resources>

Then apply this style in your page like this:

[Activity(Label = "YOURPACKAGENAME", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/MyTheme")]

There are many discussions talking about customizing the style of ActionBar, you may search for them.

I don't want to switch to toolbar, because I don't know the code for it, I also dont know if it's possible to make a toolbar look like exactly the same as the actionbar in the code below.

I personally think use Toolbar is easier for custom layout, but it is available since android 5.0, for the part how to code for Toolbar, you may refer to Toolbar.

like image 112
Grace Feng Avatar answered Sep 20 '22 21:09

Grace Feng