Berit Larsen
Berit Larsen

Reputation: 752

Xamarin Android: Cannot use ActionBarActivity

I'm having a hard time using ActionBarActivity in my app, as I am unable to import it. I tried adding Android Support Library 4,7,13 and 17...

Nr.7, 13 and 17 doesn't work, because "Some required packages are not referenced by the project". I haven't figured out what that even means.

Nr.4 doesn't work, because "The type or namespace name 'ActionBarActivity' could not be found (are you missing a using directive or an assembly reference?) C:\progge\Apper\AkvaApp\AkvaApp\AkvaApp\Browser.cs"... even though ActionBarActivity is supposed to be included in the component.

I have built and rebuilt, again and again.

Here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4;
using Xamarin;

namespace AkvaApp
{
    [Activity(Label = "Browser")]
    public class Browser : ActionBarActivity, ActionBar.ITabListener
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SupportRequestWindowFeature(WindowCompat.FeatureActionBar);

            ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
            SetContentView(Resource.Layout.BrowserLayout);
            ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
            ActionBar.Tab tab = ActionBar.NewTab();
            tab.SetText(Resources.GetString(Resource.String.SearchTabText));
            tab.TabSelected += (sender, args) =>
            {
                Android.Util.Log.Info(this.Class.Name, "this is an info message");
                Android.Util.Log.Warn(this.Class.Name, "this is a warning message");
                Android.Util.Log.Error(this.Class.Name, "this is an error message");
            };

        }

        public void OnTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
        {
            throw new NotImplementedException();
        }

        public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
        {
            throw new NotImplementedException();
        }

        public void OnTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
        {
            throw new NotImplementedException();
        }
    }
}

Whyy?

Upvotes: 2

Views: 5541

Answers (3)

Berit Larsen
Berit Larsen

Reputation: 752

I still haven't managed to get it working in Visual Studio, but I did manage in Xamarin Studio. Adding Support Library v7 in Visual Studio did not help at all, but adding it in Xamarin Studio fixed it.

For Xamarin Studio: Right-click the project (not the solution), click Add -> Add NuGet Packages, search for "support library v7", check off the one called AppCompat, and click Add Packages. Then in the class that requires this library, add "using Android.Support.V7;"

Upvotes: 3

Jon Douglas
Jon Douglas

Reputation: 13176

ActionBarActivity is part of the Support Library v7 AppCompat as mentioned by Martijn

https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html

  1. Create a new Xamarin.Android Blank Project (Ensure the Target Framework is set to API 21)
  2. Install the Android Support Library v7 AppCompat via Nuget - https://www.nuget.org/packages/Xamarin.Android.Support.v7.AppCompat/
  3. Replace Android.App.ActionBar with ActionBar as you are trying to access this from a static context.
  4. Please note that the ActionBar TabListener is deprecated and you should use other patterns instead(http://developer.android.com/design/patterns/navigation.html)

Upvotes: 2

Martijn00
Martijn00

Reputation: 3559

You need to add the nuget package to Support Library v7 AppCompat to use the ActionBarActivity. James Montemagno of Xamarin explains this in: http://blog.xamarin.com/android-support-library-v7-hello-actionbarcompat/

If you want to know why "Some required packages are not referenced by the project" is coming, you should look into the package console to see what the actual error message is about.

Upvotes: 1

Related Questions