Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create ShareActionProvider function in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 522
    Comment on it

    If you want to create Share Action Provider function check my below example. Here I have created frist main.xml. After creating main.xml I have added attribute to the menu item. In MainActivity created onCreateOptionsMenu method I sets the share action provider using setShareIntent. See the below example code it will clearly described you how to create ShareActionProvider function.

    Step(1)-Manifest file-

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.tiwari.rajshekhar.shareprovider"
        android:versionCode="1"
        android:versionName="1.0">
        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="19"/>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
        </manifest>
    

    Step(2)-xml.layout-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="178dp"
            android:text="Welcome to share provider screen"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
    </RelativeLayout>
    

    Step(3)-MainActivity-

    public class MainActivity extends Activity {
         private ShareActionProvider mShareActionProvider;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
         @Override
         public boolean onCreateOptionsMenu(Menu menu) {
              getMenuInflater().inflate(R.menu.main, menu);
              MenuItem item = menu.findItem(R.id.menu_item_share);
              // Fetch and store ShareActionProvider
              mShareActionProvider = (ShareActionProvider) item.getActionProvider();
              setShareIntent(createShareIntent());
              return true;
         }
         private void setShareIntent(Intent shareIntent) {
              if (mShareActionProvider != null) {
                   mShareActionProvider.setShareIntent(shareIntent);
              }
         }
         private Intent createShareIntent() {
              Intent shareIntent = new Intent(Intent.ACTION_SEND);
              shareIntent.setType("text/plain");
              shareIntent.putExtra(Intent.EXTRA_TEXT,
                        "http://findnerd.com");
              return shareIntent;
         }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: