Xamarin: Setting text for a TextView programmatically
Solution 1
TextView.SetText(X)
allows you to set the text from a Resource id:
currentCharacterName.SetText(Resources.Id.MyString);
You are looking for the Text
property:
currentCharacterName.Text = "test";
Xamarin: TextView class
Android.Widget.TextView.Text Property
Syntax:
public String Text { get; set; }
Solution 2
Test this code:
TextView currentCharacterName = FindViewById<TextView>(Resource.Id.characterName);
currentCharacterName.Text = "Your Text";

Naly
Updated on June 04, 2022Comments
-
Naly 12 months
This is probably a mistake or lack of comprehension on my part, but I am quite confused right now. I'm trying to set a TextView in my Xamarin Android application programmatically. Here's my code:
TextView currentCharacterName = FindViewById(Resource.Id.characterName); currentCharacterName.SetText("test");
Unfortunately, this does not work, as I get the error "Argument 1: cannot convert from 'string' to 'int'". After reading in the available methods for SetText, I noticed the method I'm trying to call demands a ResId. I don't really understand why I would need a ResId to modify the text of a TextView.
I tried searching on Google for answers, and I came across this answer from 2014 that had the exact same problem as I do. The solution was to use the Text() method instead to set the TextView. Unfortunately, when I try this solution, I get the error "Non-invocable member 'TextView.Text' cannot be used like a method". When I try to check the Text method description, I see "string TextView {get/set} To be added."
Does this mean there's no implementation yet to set the text of a TextView? I am really reluctant to believe this, as it baffles me that such a big framework like Xamarin wouldn't even have get/set functions for something as simple as setting the text of TextView. I feel like there's a very simple solution for my problem, but I can't seem to find it.
-
Jason about 7 yearsgenerally java style get* and set* methods are replaced with C# style Properties. So getText() and setText() would be replaced by a single Property named Text.
-
-
Naly about 7 yearsAh I saw my mistake! I wrote Text("test") instead of Text = "test"! Thank you so much, that was really silly of me.