Changing Properties Based On State

Nathan Sokalski 4,126 Reputation points
2021-03-19T02:49:36.62+00:00

I am new to many of the ways Xamarin.Android does things when designing the UI, so this is probably a very simple question that I just couldn't quite find the answer to. I have several properties of a Button that I want to change based on states such as being enabled. Based on what I could find, this requires an XML file with a selector (which I have never used before). Several of the things in these tutorials & examples that confused me were:

  1. I was not sure what folder to put the selector file in, some said the color folder, but I only have a colors.xml file (not a colors folder, unless I am supposed to create one), and some made me think it was supposed to be the drawable folder
  2. Visual Studio 2019 only listed (aside from the android:state_*'s) android:drawable, so I'm not sure what I should be putting there for properties that use other types such as strings, sizes, boolean, etc. Intellisense also did not want to let me use things such as the colors in colors.xml for this
  3. Do I need to make a separate selector for every property I want to change, or is there a more compact or efficient way of changing multiple properties?

This is my first time using selectors & states in Xamarin.Android, so it would be nice to see an example (with description) for several different property types. Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-03-19T05:41:53.69+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    For this function, try to create a xml file in the Resources/drawable directory to define the button style. Then consume the style for the button in layout.xml.

    Check the code:

       // Resources/drawable/button_style.xml  
       <?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>  
         
       //custom the style for the button in page_layout.xaml  
       <?xml version="1.0" encoding="utf-8"?>  
       <LinearLayout ...>  
           ...  
           <Button  
               android:id="@+id/btn1"  
               android:text="button1"  
               android:background="@drawable/android_button"  
               android:layout_width="wrap_content"  
               android:layout_height="wrap_content" />  
       </LinearLayout>  
    

    Related tutorial: https://learn.microsoft.com/en-us/xamarin/android/user-interface/controls/buttons/custom-button

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful