Naly
Naly

Reputation: 33

Xamarin: Setting text for a TextView programmatically

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.

Upvotes: 3

Views: 17849

Answers (2)

Vahid
Vahid

Reputation: 101

Test this code:

TextView currentCharacterName = FindViewById<TextView>(Resource.Id.characterName);
currentCharacterName.Text = "Your Text";

Upvotes: 7

SushiHangover
SushiHangover

Reputation: 74144

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; }

Upvotes: 7

Related Questions