Harshil.Chokshi Harshil.Chokshi - 2 months ago 13
C# Question

How to get value of string created in resources Xamarin.android

I am developing an android application in Xamarin studios and want to make a text of a button underlined. I created a string in strings.xml like the following

<string name="Advanced_Search"><u>Advanced Search</u></string>


I was succesfully able to reference this string in the xml file for the desginer, however I want to access it in code to set it as a button's text. I tried the following but it doesn't work:

advancedSearchButton.Text = Resource.String.Advanced_Search.ToString();


When i try to reference the string like that i get an integer value for the string, but I actually want the text that the string holds. How can I access the string like that in Xamarin using c#?

Answer

You need to use Resources.GetString:

advancedSearchButton.Text = Resources.GetString(Resource.String.Advanced_Search);

This is assuming the code is contained within an Activity, otherwise you need to have a reference to an Android Context.

Comments