question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Changing Properties Based On State

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

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://docs.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.



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How would I modify that for android:textColor? Intellisense only seems to want me to put things from @drawable in android:drawable (which based on the name I guess makes sense). I want my values to be colors from my colors.xml file. I may also (in the future, I am in the early stages of my app) need other types like strings, ints, sizes, bools, etc. for other properties. How do I return types other than drawables?

0 Votes 0 ·

It supports other types such as android:color, however the VS doesn't remind some properties. We can define different styles for the properties of a control.

<Button
    android:id="@+id/btn1"
    android:text="button1"
    android:textColor="@drawable/button_color"
    android:background="@drawable/android_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

//resource\drawable\button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="#ff00"
        android:state_pressed="true" />
  <item android:color="#ff0"
        android:state_focused="true" />
  <item android:color="#000" />
</selector>

You could refer to the drawable resources doc to get the properties: https://developer.android.com/guide/topics/resources/drawable-resource

0 Votes 0 ·