Reputation: 117
I am working on a mvvm light xamarin forms application where i allow the user to create there own theme with in the application itself. As of right now, i have everything working as intended with the exception of the send button(image icon), add button (image icon), and hamburger menu icon.
The user selects a image/name, then the hex value from that color is applied to the control/font. But i have run into the issue when trying to programmatically set the hamburger menu color.I started with the android side of things.I can set the color once using styles.xml.
But this not sufficient for what i am trying to do. I haven't figure out how to get a reference to the toggledrawer to change the color without creating a new reference and supplying drawables.
I will also need to have the same action happen on iOS(changeable color hamburger menu). I don't want to supply images because the user will set the color based on a hex value...what is my best option? Is what i am trying to do even possible at this point?
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected ActionBarDrawerToggle drawerToggle;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Rg.Plugins.Popup.Popup.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
drawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
}
}
i found this code here:
drawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
It would be ideal, but i don't have a reference to drawerToggle...(the object is null)
i also tried to setting it using styles.xml as should below:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/green</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
This code does work to set it once, but i need it reset within the application by a button click. So this is not sufficient for the what i am trying to do.
Upvotes: 0
Views: 1006
Reputation: 117
var toolbar = FindViewById<AndroidToolbar>(Resource.Id.toolbar);
var view = (AppCompatImageButton)toolbar.GetChildAt(2);
// 1. actionmenuview (primary/secondary menu icons),
// 2. appcompattextview (title),
// 3. appcompatimagebutton (hamburger icon)
view.SetImageDrawable(d);
// view.SetImageDrawable(d);
In my case, I have menu Icons so I select GetChildAt(2)
[the third item]
Upvotes: 0