How to activate “Share” button in android app?

i want to add “Share” button to my android app.

Like that

  • MediaPlayer no longer working on the emulator since r10?
  • How to install Google Play Services on 6.0 Genymotion device?
  • Unity3D build error
  • “Content view not yet Created” on Android Fragments
  • How to update Android version 2.3.6
  • Eclipse : Where to put NDK reference? Native Development is missing?
  • :

    I added “Share” button, but button is not active. I click, but nothing happens.

    My code in MainActivity.java:

    private ShareActionProvider mShareActionProvider;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.share_menu, menu);
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem item = menu.findItem(R.id.share_menu);
        mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share_menu).getActionProvider();
        mShareActionProvider.setShareIntent(getDefaultShareIntent());
    
        return true;
    }
    
    {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(sharingIntent, "Share using"));
    }
    

    I want to share text in my first tab (first_tab.xml) or second tab (second_tab.xml).

    Code in tab (xml) (If need):

    <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:background="@color/background_color"
    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$DummySectionFragment" >
    
    <TextView
        android:id="@+id/section_label1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/text"
        android:textColor="@color/text_color" />
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/sprite" />
    

    Sorry my english

    Related posts:

    What is the shortcut to Auto import all in Android Studio?
    How to implement Expandable android navigation drawer with subitems?
    android lolipop java.lang.SecurityException: Neither user nor current process has android.permission...
    Android, setSelected() and state_selected
    Jelly bean not calling onPrepareOptionsMenu() when opening the menu for the first time
    how to align parent bottom in android linear layout?
  • SimpleCursorAdapter vs CursorAdapter?
  • getting the response body of HttpResponse
  • One C# game to rule them all(iOS, Android, WP7, W7, Mac OS X and XBOX)
  • How to pass Bundle from Fragment to Fragment
  • Flurry Analytics vs Google Analytics on the mobile platform
  • Espresso: How to do custom swipe e.g. swipeTop or swipeBottom
  • 2 Solutions collect form web for “How to activate “Share” button in android app?”

    Add a Button and on click of the Button add this code:

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharingIntent.setType("text/plain");
    String shareBody = "Here is the share content body";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
    

    Useful links:

    For basic sharing

    For customization

    Create a button with an id share and add the following code snippet.

    share.setOnClickListener(new View.OnClickListener() {             
        @Override
        public void onClick(View v) {
    
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "Your body here";
            String shareSub = "Your subject here";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share using"));
        }
    });
    

    The above code snippet will open the share chooser on share button click action.
    However, note…The share code snippet might not output very good results using emulator. For actual results, run the code snippet on android device to get the real results.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.