0

I am using Xamarin and I have a TextView loaded via an XML layout file with the following code:

SetContentView (Resource.Layout.TextView);

Once the TextView is loaded onscreen, how can I change the Text of the TextView? How can I refer to the TextView via code as I have not declared it as a variable?

I have tried this with no result:

TextView PhoneNumber = (TextView)FindViewById(Resource.Layout.TextView);
PhoneNumber.Text = "This is a phone number: 0800 64 64 64";

The TextView is not displayed onscreen at all.

Can I please have some help?

Thanks in advance.

user22707
  • 505
  • 1
  • 13
  • 23
  • 1
    I think you must access to `TextView` with Id not `Layout`, so you must change `(TextView)FindViewById(Resource.Layout.TextView);` to `(TextView)FindViewById(Resource.id.TextView);` and create `Textview` with `TextView id` in `TextView layout` – Shayan Pourvatan Jan 18 '14 at 06:33
  • possible duplicate of [Uisng XML layout for TextView](http://stackoverflow.com/questions/21200507/uisng-xml-layout-for-textview) – Padma Kumar Jan 18 '14 at 06:34

3 Answers3

2

Replace this:

SetContentView (Resource.Layout.TextView);

and

TextView PhoneNumber = (TextView)FindViewById(Resource.Layout.TextView);
PhoneNumber.Text = "This is a phone number: 0800 64 64 64";

with this:

SetContentView (Resource.Layout.Your_Layout_Name);

and

TextView PhoneNumber = (TextView)FindViewById(Resource.id.TextView);
PhoneNumber.setText("This is a phone number: 0800 64 64 64");
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
1

Declare a TextView in your xml. Retrive it using from id and setText in java

PhoneNumber.setText("This is a phone number: 0800 64 64 64");
Kunu
  • 5,078
  • 6
  • 33
  • 61
0

The same problem does not appear the assigned value.

public class MainActivity : Activity
{
    public string mess;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        StartListening ();


        Button bt = FindViewById<Button>(Resource.Id.button1);
        bt.Click += delegate { start();};
        // Get our button from the layout resource,
        // and attach an event to it
        }
    public void start()
    {
        TextView text = FindViewById<TextView> (Resource.Id.textView1);


        StartListening();
        text.Text = mess;
        //text.SetText (mess);
    }
    private readonly UdpClient udp = new UdpClient(45000);

    public void StartListening()
    {
        this.udp.BeginReceive(Receive, new object());




    }
    public void Receive(IAsyncResult ar)
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Any, 45000);
        byte[] bytes = udp.EndReceive(ar, ref ip);


        mess = Encoding.ASCII.GetString(bytes);
        StartListening();

    }
}
    }