Forum Xamarin.Android

Customs states not working since update to Visual Studio 2019

ivnsfivnsf ESMember ✭✭
edited February 13 in Xamarin.Android

Hello,

I am trying to update my Xamarin.Android project to target Android 10 and Visual Studio 2019 but there is a bug that i can not solve.

I have custom states for some components, but their condition is being ignored and always shows "true" except when other default state in android is active and upper in the hierarchy. This is the code example:

Attrs.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <declare-styleable name="CustomStates">
    <attr name="state_error" format="boolean"/>
  </declare-styleable>
</resources>

CustomButton.xml

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto/XXXX.XXXX.XXXX">

  <item android:state_enabled="false"
        android:drawable="@drawable/DisabledButton"/>    

  <item app:state_error="true"
        android:drawable="@drawable/ErrorButton"/>  

  <item android:state_pressed="true"
        android:drawable="@drawable/PressedButton"/>

  <item android:drawable="@drawable/DefaultButton"/>

</selector>

CustomButton.cs

public class CustomButton : ImageButton
{
    private static int[] errorState = { Resource.Attribute.state_error };
    private bool error;

    ...

    public bool Error
        {
            get
            {
                return this.error;
            }

            set
            {
                this.error = value;
                this.RefreshDrawableState();
            }
        }

    public override int[] OnCreateDrawableState(int extraSpace)
        {
            int[] state = base.OnCreateDrawableState(extraSpace + 1);

            if (this.error)
            {                
                LoginButton.MergeDrawableStates(state, errorState);
            }

            return state;
        }
}

My app is always showing @drawable/ErrorButton even when the property Error is setted to false. The only case when @drawable/ErrorButton is not being showed, is when the button is disabled (correctly shows @drawable/DisabledButton).

This is not happening in Visual Studio 2017 targeting Android 9. I am doing the test in the same devices (Android 5.1 and Android 7.1).

Tagged:

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited February 14

    update my Xamarin.Android project to target Android 10 and Visual Studio 2019 but there is a bug that i can not solve.

    The packages in VS 2019 is a little different from 2017. Try to update the packages to the newest stable version. Or create a new sample and copy the code to the new project.

    I create a basic demo on Visual Studio 2019 and it works fine.

    <?xml version="1.0" encoding="utf-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@drawable/android_pressed"
            android:state_pressed="true" />
      <item android:drawable="@drawable/android_focused"
            android:state_focused="true" />
      <item android:drawable="@drawable/android_normal" />
    </selector>
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/buttons/custom-button

  • ivnsfivnsf ESMember ✭✭

    @Jarvan said:

    update my Xamarin.Android project to target Android 10 and Visual Studio 2019 but there is a bug that i can not solve.

    The packages in VS 2019 is a little different from 2017. Try to update the packages to the newest stable version. Or create a new sample and copy the code to the new project.

    I create a basic demo on Visual Studio 2019 and it works fine.

    <?xml version="1.0" encoding="utf-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@drawable/android_pressed"
            android:state_pressed="true" />
      <item android:drawable="@drawable/android_focused"
            android:state_focused="true" />
      <item android:drawable="@drawable/android_normal" />
    </selector>
    

    Hello,

    Thanks for the answer, but the default states are working correctly. The problem is when i use a custom state declared in Attrs.xml like in my code.

  • ivnsfivnsf ESMember ✭✭

    @Jarvan said:

    update my Xamarin.Android project to target Android 10 and Visual Studio 2019 but there is a bug that i can not solve.

    The packages in VS 2019 is a little different from 2017. Try to update the packages to the newest stable version. Or create a new sample and copy the code to the new project.

    I create a basic demo on Visual Studio 2019 and it works fine.

    <?xml version="1.0" encoding="utf-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@drawable/android_pressed"
            android:state_pressed="true" />
      <item android:drawable="@drawable/android_focused"
            android:state_focused="true" />
      <item android:drawable="@drawable/android_normal" />
    </selector>
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/buttons/custom-button

    I updated the packages to the latest version and the problem persists.

  • JarvanJarvan Member, Xamarin Team Xamurai

    This is not happening in Visual Studio 2017 targeting Android 9.

    If the target framework is set to Android 9 in Visual Studio 2019, will it work well?

Sign In or Register to comment.