Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

ActionBar Tabs withh View Pager using Compatibility Library

$
0
0

I'm attempting to recreate the following Android example using Xamarin: http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

The error that I'm getting is when I try and get the instance of the action bar. The code for my initial Activity is below. The error is on line 27: actionBar = this.ActionBar; The error is: Error CS0030: Cannot convert type 'Android.App.ActionBar' to 'Android.Support.V7.App.ActionBar'

using Android.OS;
using Android.Views;
using System;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Support.V7.App;

namespace MyApplication
{
    public class MainActivity : FragmentActivity
    {
        private ViewPager viewPager;
        private TabsPagerAdapter mAdapter;
        private ActionBar actionBar;    
        // Tab titles
        private String[] tabs = { "Top Rated", "Games", "Movies" };

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

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

            // Initilization
            viewPager = FindViewById<ViewPager>(Resource.Id.pager);
            actionBar = this.ActionBar;
            mAdapter = new TabsPagerAdapter(this.SupportFragmentManager);

            viewPager.Adapter = mAdapter;
            actionBar.SetHomeButtonEnabled(false);
            actionBar.NavigationMode = Convert.ToInt32(Android.App.ActionBarNavigationMode.Tabs);       

            // Adding Tabs
            foreach (String tab_name in tabs) {
                ActionBar.Tab tab = actionBar.NewTab ();
                tab.SetText (tab_name);
                tab.SetTabListener (new TabListener<ParentFragment> (this, tab_name));
                actionBar.AddTab (tab);
            }
        }

        public override bool OnCreateOptionsMenu (Android.Views.IMenu menu)
        {
            // Menu items default to never show in the action bar. On most devices this means
            // they will show in the standard options menu panel when the menu button is pressed.
            // On xlarge-screen devices a "More" button will appear in the far right of the
            // Action Bar that will display remaining items in a cascading menu.

            menu.Add (new Java.Lang.String ("Normal item"));

            var actionItem = menu.Add (new Java.Lang.String ("Action Button"));

            // Items that show as actions should favor the "if room" setting, which will
            // prevent too many buttons from crowding the bar. Extra items will show in the
            // overflow area.
            MenuItemCompat.SetShowAsAction (actionItem, MenuItemCompat.ShowAsActionIfRoom);

            // Items that show as actions are strongly encouraged to use an icon.
            // These icons are shown without a text description, and therefore should
            // be sufficiently descriptive on their own.
            actionItem.SetIcon (Android.Resource.Drawable.IcMenuShare);

            return true;
        }

        public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
        {
            Android.Widget.Toast.MakeText (this, 
                "Selected Item: " + 
                item.TitleFormatted, 
                Android.Widget.ToastLength.Short).Show();

            return true;
        }
    }
}

Any help you can give would be really helpful. Thanks!


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>