How to GetIdentifier by style Id in Xamarin Android ??

VictorNguyenVictorNguyen USMember
edited September 2016 in Xamarin.Forms

In a custom renderer, I was trying to get a style id created under Resources/values/styles.xml, by using

var id = this.Context.Resources.GetIdentifier("customCheckBoxStyle", "values", Context.PackageName);

But for some reason this doesnt work. When I set the breakpoint and hover over GetIdentifier it says unknown member GetIdentifier, which I dont understand because I was able to build and deploy the project successfully

Could someone take a look and let me know?

Thanks

Custom Renderer:

    protected override void OnElementChanged(ElementChangedEventArgs<MYNS.Mobile.Controls.CheckBox> e)
    {
        base.OnElementChanged(e);

        if (this.Control == null) {
            int[][] states = new int[][] {
                new int[] { Android.Resource.Attribute.StateEnabled},
                new int[] { Android.Resource.Attribute.StateEnabled},
                new int[] { Android.Resource.Attribute.StateChecked },
                new int[] { Android.Resource.Attribute.StatePressed },
            };
            string hexcolor = Element.CheckBoxColor;
            int[] colors = new int[] {
                Android.Graphics.Color.ParseColor(hexcolor),
                Android.Graphics.Color.ParseColor(hexcolor),
                Android.Graphics.Color.ParseColor(hexcolor),
                Android.Graphics.Color.ParseColor(hexcolor)
            };    


            //var checkBox = new Android.Widget.CheckBox(this.Context,null,Context.Resources.GetIdentifier("customCheckBoxStyle", "values",Context.PackageName));
            var checkBox = new Android.Widget.CheckBox(this.Context);
            int id = 0;
            try
            {
                id = this.Context.Resources.GetIdentifier("customCheckBoxStyle", "values", Context.PackageName);
            }
            catch (Exception exception)
            {
                System.Diagnostics.Debug.WriteLine(exception.ToString());
            }
            checkBox.CheckedChange += CheckBoxCheckedChange;
            if (int.Parse(Build.VERSION.Sdk) >=21)                
                checkBox.ButtonTintList = new ColorStateList(states, colors); 
            this.SetNativeControl(checkBox);
        }
        Control.Checked = e.NewElement.Checked;
    }

Resources/values/styles.xml

<style name="customCheckBoxStyle" parent="Theme.AppCompat">
    <item name="colorAccent">#828591</item>
    <item name="android:textColorSecondary">#828591</item>
</style>

Ultimately, what I was trying to do was changing the check box tint color by applying a custom style for APi <21, since buttonttintlist is not available pre 21.
Stackoverflow reference: http://stackoverflow.com/questions/26532045/tinting-checkbox-on-pre-v21

Device/Emulator tested: Genymotion Samsung Galaxy Note 3 - API 19

Answers

Sign In or Register to comment.