2

I have a problem to update a TextView in Xamarin/Android.

I have created a tabbed application that is the default tabbed application in Xamarin.

When I click on the "Resource.Id.nav_camera" I am trying to change the textview that by default in the App says: "Hello World!" to "Hello World2!" but the textview is not updating the text. However the "Toast.MakeText" is showing "Hello World2!" in the alertbox which means that the code is coming through and finds the textview.

What can be the problem?

public bool OnNavigationItemSelected(IMenuItem item)
{
    int id = item.ItemId;
    if (id == Resource.Id.nav_camera)
    {
        // Handle the camera action
        Android.Views.View mainview = LayoutInflater.Inflate(Resource.Layout.content_main, null);
        TextView tview = mainview.FindViewById<TextView>(Resource.Id.hw);
        if (tview != null)
        {
            tview.Text = "Hello World2!"; //Is not updating??
            Toast.MakeText(ApplicationContext, "Hello World2!", ToastLength.Long).Show();
        }
    }
}

content_main.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!"
        android:id="@+id/hw" />

</RelativeLayout>
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • 1
    Try `RunOnUIThread`? – FreakyAli Feb 13 '20 at 11:36
  • I tried this but it didn't work: `if (tview != null) { RunOnUiThread(() => { tview.Text = "Hello World2!"; }); }` – Andreas Feb 13 '20 at 11:49
  • 2
    You are inflating a new layout which you set the text property on a TextView. This is fine. However, you are not displaying that inflated layout anywhere. Hence, you are not seeing anything changing. The context `OnNavigationItemSelected` is running in. Is it showing the same view you are inflating or another? What are you trying to achieve? – Cheesebaron Feb 13 '20 at 12:55
  • I see, perheps I miss something here. So I inflate the view but I am not displaying it. How is it possible to display it?. The only thing I try to do is to change the text on the existing TextView in the content_main.axml page. Nothing else. (OnNavigationItemSelected is showing a "slide in from the left panel" which is another view) – Andreas Feb 13 '20 at 13:00
  • But are you already showing `content_main`? – Cheesebaron Feb 13 '20 at 13:17
  • content_main is visible directly when I debug the app on the mobile. It the view that you see. Then I tap on the slider(OnNavigationItemSelected) and click on: "Resource.Id.nav_camera" where I try to change the TextViews text which is located in "content_main.axml" – Andreas Feb 13 '20 at 13:19
  • 1
    @Cheesebaron I changed to this and now it did work actually: `TextView tview = FindViewById(Resource.Id.hw);` I removed the inflate code as you mentioned something about there. Thank you for your help! – Andreas Feb 13 '20 at 14:13
  • 2
    Please post your solution to answer and mark it. It will help others who have similar issue. – Leon Feb 14 '20 at 05:35

1 Answers1

0

You may simplify the FindViewById() call:

TextView tview = FindViewById<TextView>(Resource.Id.hw);
tview.Text = "Hello World2!";