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
    • 525
    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-

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3. package="com.tiwari.rajshekhar.shareprovider"
    4. android:versionCode="1"
    5. android:versionName="1.0">
    6. <uses-sdk
    7. android:minSdkVersion="14"
    8. android:targetSdkVersion="19"/>
    9.  
    10. <application
    11. android:allowBackup="true"
    12. android:icon="@mipmap/ic_launcher"
    13. android:label="@string/app_name"
    14. android:supportsRtl="true"
    15. android:theme="@style/AppTheme">
    16. <activity
    17. android:name=".MainActivity"
    18. android:label="@string/app_name"
    19. android:theme="@style/AppTheme.NoActionBar">
    20. <intent-filter>
    21. <action android:name="android.intent.action.MAIN" />
    22.  
    23. <category android:name="android.intent.category.LAUNCHER" />
    24. </intent-filter>
    25. </activity>
    26. </application>
    27. </manifest>

    Step(2)-xml.layout-

    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:paddingBottom="@dimen/activity_vertical_margin"
    6. android:paddingLeft="@dimen/activity_horizontal_margin"
    7. android:paddingRight="@dimen/activity_horizontal_margin"
    8. android:paddingTop="@dimen/activity_vertical_margin"
    9. tools:context=".MainActivity" >
    10.  
    11. <TextView
    12. android:id="@+id/textView1"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:layout_alignParentLeft="true"
    16. android:layout_alignParentRight="true"
    17. android:layout_alignParentTop="true"
    18. android:layout_marginTop="178dp"
    19. android:text="Welcome to share provider screen"
    20. android:textAppearance="?android:attr/textAppearanceLarge" />
    21.  
    22. </RelativeLayout>

    Step(3)-MainActivity-

    1. public class MainActivity extends Activity {
    2. private ShareActionProvider mShareActionProvider;
    3. @Override
    4. protected void onCreate(Bundle savedInstanceState) {
    5. super.onCreate(savedInstanceState);
    6. setContentView(R.layout.activity_main);
    7. }
    8. @Override
    9. public boolean onCreateOptionsMenu(Menu menu) {
    10. getMenuInflater().inflate(R.menu.main, menu);
    11. MenuItem item = menu.findItem(R.id.menu_item_share);
    12. // Fetch and store ShareActionProvider
    13. mShareActionProvider = (ShareActionProvider) item.getActionProvider();
    14. setShareIntent(createShareIntent());
    15. return true;
    16. }
    17. private void setShareIntent(Intent shareIntent) {
    18. if (mShareActionProvider != null) {
    19. mShareActionProvider.setShareIntent(shareIntent);
    20. }
    21. }
    22. private Intent createShareIntent() {
    23. Intent shareIntent = new Intent(Intent.ACTION_SEND);
    24. shareIntent.setType("text/plain");
    25. shareIntent.putExtra(Intent.EXTRA_TEXT,
    26. "http://findnerd.com");
    27. return shareIntent;
    28. }
    29. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: