Cannot find Toolbar

FotalFotal Member ✭✭✭

Hello everybody!

In MainActivity i trying to get a Android.Support.V7.Widget.Toolbar, but he does not find. After the line of code below
ToolBar = FindViewById(Resource.Id.toolbar); ToolBar is null.
MainActivity

using Acr.UserDialogs;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Support.V7.Widget;
using Android.Views;
using MDOSchedule.Droid.Extensions;
using Xamarin.Forms;


namespace MDOSchedule.Droid.Activities
{
    [Activity(Label = "MDOSchedule", 
              Icon = "@drawable/icon", 
              Theme = "@style/MainTheme",
              MainLauncher = true,
              ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public static Toolbar ToolBar { get; private set; }

        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            ToolBar = FindViewById<Toolbar>(Resource.Id.toolbar);

            base.OnCreate(bundle);

            UserDialogs.Init(() => (Activity)Forms.Context);

            Forms.Init(this, bundle);            

            RegisterServices();

            LoadApplication(new App());
        }


        #region Private Methods

        private void RegisterServices()
        {
            DependencyService.Register<OpenAppAndroid>();
        }

        #endregion
    }
}

ToolBar Resource

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Best Answer

  • FotalFotal ✭✭✭
    edited April 2018 Accepted Answer

    @tedlin01 Thanks, I did receive this ToolBar in my service, when I need it. In OnElementChanded event:

    public class SearchPageRenderer : PageRenderer
        {
            private readonly Context _localContext;
            private SearchView _searchView;
    
            private Toolbar _toolBar;
    
            public SearchPageRenderer(Context context) : base(context)
            {
                _localContext = context;
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
            {
                base.OnElementChanged(e);
    
                if (e?.NewElement == null || e.OldElement != null)
                {
                    return;
                }
                var context = (Android.App.Activity)_localContext;
                _toolBar = (context).FindViewById<Toolbar>(Resource.Id.toolbar);
    
                AddSearchToToolBar();
            }
    
            protected override void Dispose(bool disposing)
            {
                if (_searchView != null)
                {
                    _searchView.QueryTextChange += searchView_QueryTextChange;
                    _searchView.QueryTextSubmit += searchView_QueryTextSubmit;
                }
                _toolBar?.Menu?.RemoveItem(Resource.Menu.mainmenu);
                base.Dispose(disposing);
            }
    
            private void AddSearchToToolBar()
            {
                _toolBar.Title = Element.Title;
    
                _toolBar.InflateMenu(Resource.Menu.mainmenu);
    
                _searchView = _toolBar.Menu?.FindItem(Resource.Id.action_search)
                    ?.ActionView?.JavaCast<SearchView>();
    
                _searchView.SetIconifiedByDefault(false);
    
                _searchView.Iconified = false;
                _searchView.RequestFocusFromTouch();
    
                _searchView.FocusChange += ShowKeyBoard;
                _searchView.QueryTextChange += searchView_QueryTextChange;
                _searchView.QueryTextSubmit += searchView_QueryTextSubmit;
                _searchView.QueryHint = (Element as SearchPage)?.SearchPlaceHolderText;
                _searchView.ImeOptions = (int)ImeAction.Search;
                _searchView.InputType = (int)InputTypes.TextVariationNormal;
                _searchView.MaxWidth = int.MaxValue;
    
                _searchView.RequestFocus();
            }
    
            private void ShowKeyBoard(object sender, FocusChangeEventArgs e)
            {
                _searchView.Iconified = false;
                _searchView.RequestFocusFromTouch();
            }
    
            private void searchView_QueryTextSubmit(object sender, SearchView.QueryTextSubmitEventArgs e)
            {
                var searchPage = Element as SearchPage;
                searchPage.SearchText = e.Query;
                searchPage.SearchCommand?.Execute(e.Query);
                e.Handled = true;
            }
    
            private void searchView_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e)
            {
                var searchPage = Element as SearchPage;
                searchPage.SearchText = e?.NewText;
            }
        }
    

Answers

  • tedlin01tedlin01 SEMember ✭✭

    Hello, before you can use FindViewById the view need to actually exist. It doesn't exist until it is inflated and rendered in the screen. If you run it "after" a NavigationPage is created you will find it.

  • FotalFotal Member ✭✭✭
    edited April 2018 Accepted Answer

    @tedlin01 Thanks, I did receive this ToolBar in my service, when I need it. In OnElementChanded event:

    public class SearchPageRenderer : PageRenderer
        {
            private readonly Context _localContext;
            private SearchView _searchView;
    
            private Toolbar _toolBar;
    
            public SearchPageRenderer(Context context) : base(context)
            {
                _localContext = context;
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
            {
                base.OnElementChanged(e);
    
                if (e?.NewElement == null || e.OldElement != null)
                {
                    return;
                }
                var context = (Android.App.Activity)_localContext;
                _toolBar = (context).FindViewById<Toolbar>(Resource.Id.toolbar);
    
                AddSearchToToolBar();
            }
    
            protected override void Dispose(bool disposing)
            {
                if (_searchView != null)
                {
                    _searchView.QueryTextChange += searchView_QueryTextChange;
                    _searchView.QueryTextSubmit += searchView_QueryTextSubmit;
                }
                _toolBar?.Menu?.RemoveItem(Resource.Menu.mainmenu);
                base.Dispose(disposing);
            }
    
            private void AddSearchToToolBar()
            {
                _toolBar.Title = Element.Title;
    
                _toolBar.InflateMenu(Resource.Menu.mainmenu);
    
                _searchView = _toolBar.Menu?.FindItem(Resource.Id.action_search)
                    ?.ActionView?.JavaCast<SearchView>();
    
                _searchView.SetIconifiedByDefault(false);
    
                _searchView.Iconified = false;
                _searchView.RequestFocusFromTouch();
    
                _searchView.FocusChange += ShowKeyBoard;
                _searchView.QueryTextChange += searchView_QueryTextChange;
                _searchView.QueryTextSubmit += searchView_QueryTextSubmit;
                _searchView.QueryHint = (Element as SearchPage)?.SearchPlaceHolderText;
                _searchView.ImeOptions = (int)ImeAction.Search;
                _searchView.InputType = (int)InputTypes.TextVariationNormal;
                _searchView.MaxWidth = int.MaxValue;
    
                _searchView.RequestFocus();
            }
    
            private void ShowKeyBoard(object sender, FocusChangeEventArgs e)
            {
                _searchView.Iconified = false;
                _searchView.RequestFocusFromTouch();
            }
    
            private void searchView_QueryTextSubmit(object sender, SearchView.QueryTextSubmitEventArgs e)
            {
                var searchPage = Element as SearchPage;
                searchPage.SearchText = e.Query;
                searchPage.SearchCommand?.Execute(e.Query);
                e.Handled = true;
            }
    
            private void searchView_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e)
            {
                var searchPage = Element as SearchPage;
                searchPage.SearchText = e?.NewText;
            }
        }
    
Sign In or Register to comment.