lundi 27 juin 2016

Share button in Android Navigation Drawer

I am trying to implement a Share Button in Navigation Drawer. I am not sure how we implement this. any help is appreciated. This is how far I have gone.

Menu Item XML

       <?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
      <group android:checkableBehavior="single"
             android:id="@+id/app">
        <item
          android:id="@+id/nav_fibonacci"
          android:icon="@drawable/ic_cards"
          android:title="@string/FibonacciSeriesTabTitle" />
        <item
          android:id="@+id/nav_tshirt"
          android:icon="@drawable/ic_tshirt"
          android:title="@string/TShirtSizesTabTitle" />
        <item
          android:id="@+id/nav_standard"
          android:icon="@drawable/ic_cards"
          android:title="@string/StandardSeriesTabTitle" />
      <item
          android:id="@+id/nav_settings"
          android:icon="@drawable/ic_settings"
          android:title="@string/SettingsTitle" />
      </group>
      <group android:checkableBehavior="single"
             android:id="@+id/contact">

        <item
          android:id="@+id/nav_share"
          android:icon="@drawable/ic_share"
          android:title="@string/ShareTitle"
          android:actionProviderClass="android.widget.ShareActionProvider"/>

      </group>
    </menu>

Navigation Drawer

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:ads="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer_layout"
        android:fitsSystemWindows="true">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
          <FrameLayout
                android:id="@+id/content_frame"
                android:layout_below="@id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
           <com.google.android.gms.ads.AdView
                    android:id="@+id/adView"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    ads:adSize="SMART_BANNER"
                    ads:adUnitId="ca-app-pub-8875162019282514/8222601087"
                    android:gravity="bottom" />

        </RelativeLayout>
        <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/nav_view"
            app:menu="@menu/navmenu"
            app:headerLayout="@layout/header" />

    </android.support.v4.widget.DrawerLayout>

Navigation Activity 

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.Support.V7.App;
using Android.Support.Design.Widget;
using Android.Support.V4.Widget;
using Android.Support.V7.Widget;
using TestNavigationAndroid.Fragments;
using TestNavigationAndroid.Ads;
using Android.Gms.Ads;
using Android.Support.V4.View;

namespace TestNavigationAndroid
{
    [Activity(MainLauncher = true, Theme = "@style/MyTheme", Icon = "@drawable/icon")]
    public class NavigationDrawerActivity : AppCompatActivity
    {
        DrawerLayout drawerLayout;


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

            // Create UI
            SetContentView(Resource.Layout.navigation);
            drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

            // Init toolbar
            var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);

            // Attach item selected handler to navigation view
            var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;

            // Create ActionBarDrawerToggle button and add it to the toolbar
            var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
            drawerLayout.SetDrawerListener(drawerToggle);
            drawerToggle.SyncState();

            //if first time you will want to go ahead and click first item.
            if (bundle == null)
            {
                SwitchFragments(Resource.Id.nav_fibonacci);
            }
            AdView _bannerad = FindViewById<AdView>(Resource.Id.adView);
            //_bannerad = AdMobWrapper.ConstructStandardBanner(this, AdSize.SmartBanner, "ca-app-pub-8875162019282514/8222601087");
            //dMobWrapper.CustomBuild(_bannerad);
            _bannerad.LoadAd(new AdRequest.Builder().Build());
            //   drawerLayout.AddView(_bannerad);
           // ShowShareActivity();
        }

        void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
        {
            SwitchFragments(e.MenuItem.ItemId);
            //if (e.MenuItem.ItemId.Equals(Resource.Id.nav_share))
            //{
            //    ShowShareActivity(e.MenuItem);
            //}
            //else
            //{

            //}
        }

        private void SwitchFragments(int ItemId)
        {
            Android.Support.V4.App.Fragment fragment = null;
            switch (ItemId)
            {
                case (Resource.Id.nav_fibonacci):
                    fragment = FibonacciSeries.NewInstance();
                    // React on 'Home' selection
                    break;
                case (Resource.Id.nav_tshirt):
                    // React on 'Messages' selection
                    fragment = TshirtSize.NewInstance();
                    break;
                case (Resource.Id.nav_standard):
                    // React on 'Friends' selection
                    fragment = StandardFragment.NewInstance();
                    break;
                case (Resource.Id.nav_share):
                    // React on 'Friends' selection
                    ShowShareActivity();
                    break;

            }
            SupportFragmentManager.BeginTransaction()
                .Replace(Resource.Id.content_frame, fragment)
                .Commit();
            // Close drawer
            drawerLayout.CloseDrawers();
        }

        private void ShowShareActivity()
        {
            Intent sharingIntent = new Intent(Intent.ActionSend);
            sharingIntent.SetType("text/plain");
            String shareBody = "Here is the share content body";
            sharingIntent.PutExtra(Intent.ExtraSubject, "Subject Here");
            sharingIntent.PutExtra(Intent.ExtraText, shareBody);
            var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            var menu = navigationView.Menu;
            IMenuItem item = menu.FindItem(Resource.Id.nav_share);

            // Fetch and store ShareActionProvider
            ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.GetActionProvider(item);

            mShareActionProvider.SetShareIntent(sharingIntent);
        }
    }
}

I am using Xamarin.Android. what I'm trying to achieve is when someone clicks on share button I world like to show a fragment with all available apps to share the information.I tried to set the ShareActionProvider based on Google Tutorial. Any Help here is appreciated.

Aucun commentaire:

Enregistrer un commentaire