Friday, December 20, 2013

ActionBarCompat with ShareActionProvider

This exercise implement MenuItem of Share action in ActionBarCompat, and ShareActionProvider to share text if user click on the Share on the Share item. Base on last exercise "Handle onOptionsItemSelected() for ActionBarCompat".

ActionBarCompat with Share ShareActionProvider
ActionBarCompat with Share ShareActionProvider

Modify /res/menu/main.xml to add new item of action_share.
<menu 
    xmlns:myapp="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android" >

 <item
        android:id="@+id/action_share"
        android:title="Share"
        myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        myapp:showAsAction="always|withText" />
    <item
        android:id="@+id/action_settings"
        myapp:showAsAction="always|withText"
        android:title="@string/action_settings"/>

</menu>

MainActivity.java.
package com.example.testactionbarcompat;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
 
 private ShareActionProvider myShareActionProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        
        //add MenuItem(s) to ActionBar using Java code
        MenuItem menuItem_Info = menu.add(0, R.id.menuid_info, 0, "Info");
        menuItem_Info.setIcon(android.R.drawable.ic_menu_info_details);
        MenuItemCompat.setShowAsAction(menuItem_Info, 
          MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        
        //Handle share menu item
        MenuItem shareItem = menu.findItem(R.id.action_share);
        myShareActionProvider = (ShareActionProvider)
          MenuItemCompat.getActionProvider(shareItem);
        setShareIntent();
        
        return true;
    }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
  //return super.onOptionsItemSelected(item);
  switch (item.getItemId()) {
        case R.id.action_settings: 
         //match with /res/menu/main.xml
            Toast.makeText(this, "Setting", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.menuid_info:  
         //match with defined in onCreateOptionsMenu()
         Toast.makeText(this, "Info", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);    
  }
 }
    
 //Set the share intent
 private void setShareIntent(){
  
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.setType("plain/text");
  intent.putExtra(Intent.EXTRA_TEXT, "Hello from android-er.blogspot.com"); 

  myShareActionProvider.setShareIntent(intent); 
 }

}


download filesDownload the files.

Next:
ActionBarCompat with ShareActionProvider to update dynamic content


Visit: ActionBarCompat Step-by-step

2 comments:

faizal said...

How did you resolve the "getActionProvider: item does not implement SupportMenuItem" issue as in http://stackoverflow.com/questions/24219842/getactionprovider-item-does-not-implement-supportmenuitem

Unknown said...

Thank you for your guidance, I saw in the android's official website, found that the project whitch contain v7 package does not apply to the project,but is completely can solve in your way!