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>