Andromer
Andromer

Reputation: 123

xamarin for android :How to setTextColor Set Xml Selector

I want XML color selector to set a TextView in Java code.

mText.setTextColor(getResources().getColorStateList(R.color.xml_color_selector))

How does this code work in Xamarin?


I found the API from here 1 and here 2. I tried both of them, but:

mText.SetTextColor(Android.Content.Res.Resources. "not found GetColorStateList"<br>
mText.SetTextColor(Resources. "not found GetColorStateList"
mText.SetTextColor(Java.Lang.ClassLoader. "Not Found GetResource"
mText.SetTextColor(Java.Lang.Class. "Not Found GetResource"

Thanks.

P.S. I want to convert Java code to C# code and set the XML selector to Textcolor in code.

This is Resources\Drawable\xml_color_selector.xml
I hope Set this TextColor drawable in Activity

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/menu_item_title_color_pressed" android:state_pressed="true" />
  <item android:color="@color/menu_item_title_color" android:state_pressed="false" />
</selector>


This action SetBackground works fine.

ListItemView.SetBackgroundResource(Resource.Drawable.menu_item_background_color_pressed);

Upvotes: 1

Views: 4238

Answers (3)

RodXander
RodXander

Reputation: 833

This was a headache for me too. Since the more intuitive GetColor(int) was deprecated by Google, you can't do this anymore.

view.SetTextColor(context.Resources.GetColor(Resource.Color.color_name)); 

Instead you should use GetColor(int, Theme) in any of these ways

view.SetTextColor(context.Resources.GetColor(Resource.Color.color_name, null)); 
view.SetTextColor(context.Resources.GetColor(Resource.Color.color_name, theme)); 

However, this is only available from API 23 going forward, therefore if you want to support older devices, many Java developers will propose to use

view.SetTextColor(ContextCompat.GetColor(context, Resource.Color.color_name));

Now for us, Xamarin developers, this won't work either since it'll show an error saying that it can't convert int to ColorStateList. So, instead you should use

SOLUTION:

view.SetTextColor(ContextCompat.GetColorStateList(context, Resource.Color.color_name));

Hope the explanation helps.

Upvotes: 2

Droid Chris
Droid Chris

Reputation: 3783

mText.SetBackgroundColor (Color.Transparent);

Make sure that Color is an xml file in your resource directory (Resources -> Values -> Color.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="solid_red">#fff000</color>
    <color name="transparent">#00000000</color>
    <color name="black">#000000</color>
    <color name="lightgrey">#bbbbbb</color>
    <color name="grey">#333333</color>
    <color name="white">#ffffff</color>
    <color name="listseparator">#2A3748</color>
    <color name="yellow">#FECF35</color>
    <color name="blue">#00f</color>
</resources>

Upvotes: 0

mominapk
mominapk

Reputation: 81

you can set it in xml if you want to select it as background color then write

  android:background="@color/xml_color_selector"

and your xml_color_selector.xml file should be in color folder in res.

if you want to add border only then you should keep xml_color_selector.xmlin drawable folder and then write

android:background="@drawable/xml_color_selector"

Hope it will help you.

Upvotes: 0

Related Questions