Xamarin Tabs not changing data Android

69 Views Asked by At

Someone please help this is killing me!!! I have a textview which I am trying to et working for some reason it does not update? the text does not change the tabs are just blank.

NOTE: there are no errors or anything tabs are working fine, bur the text never gets updated I cannot seem to find out why please help!!

Thanks in advance

Main.cs:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace App1
{
    [Activity(Label = "ActionBar Tabs Example")]
    public class Main : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

            //enable navigation mode to support tab layout
            this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

            //adding audio tab
            AddTab("Audio", Resource.Drawable.Icon, new AudioFragment());

            //adding video tab 
            AddTab("Video", Resource.Drawable.Icon, new VideoFragment());
        }

        /*
         * This method is used to create and add dynamic tab view
         * @Param,
         *  tabText: title to be displayed in tab
         *  iconResourceId: image/resource id
         *  fragment: fragment reference
         * 
        */
        void AddTab(string tabText, int iconResourceId, Fragment fragment)
        {
            var tab = this.ActionBar.NewTab();
            tab.SetText(tabText);
            tab.SetIcon(iconResourceId);

            // must set event handler for replacing tabs tab
            tab.TabSelected += delegate (object sender, ActionBar.TabEventArgs e) {
                e.FragmentTransaction.Replace(Resource.Id.fragmentContainer, fragment);
            };

            this.ActionBar.AddTab(tab);
        }


        class AudioFragment : Fragment
        {
            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                base.OnCreateView(inflater, container, savedInstanceState);

                var view = inflater.Inflate(Resource.Layout.Tab, container, false);
                var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView);
                sampleTextView.Text = "This is Audio Fragment Sample";
                return view;
            }
        }

        class VideoFragment : Fragment
        {
            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                base.OnCreateView(inflater, container, savedInstanceState);

                var view = inflater.Inflate(Resource.Layout.Tab, container, false);
                var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView);
                sampleTextView.Text = "This is Video Fragment Sample";

                return view;
            }
        }






    }
}

Tabaxml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textStyle="bold"
    android:textSize="16dp"
    android:background="#ffe3e1e1"
    android:textColor="#ff393939" />

TabbedMenu.axml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            />
    </LinearLayout>
1

There are 1 best solutions below

1
On BEST ANSWER

Your issue is here:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            />
    </LinearLayout>

Your FrameLayout has a height of 0! That means all the content inside it is hidden. Set the layout_height to something else such as match_parent.

Also, side note, fill_parent is deprecated now. You should use match_parent.