1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Drain Droids

Android Studio For Beginners.Mobile Apps.Android Games.

Wednesday, December 26, 2018

How to Set up Share Button Action Bar on Android Studio



Every one now realize how power using social media for marketing. Many company rely fully on social media marketing, as a result, sharing on social platform is now important. The only method that makes sharing convenient is implement a sharing button on whatever platform or project you are working on. 
Clicking on a sharing button give you a multiple choice platform to share the information, you could share it to Instagram, Twitter, Facebook and Google Plus etc.

However, many people don’t really know how to implement this useful sharing button on their app.In this tutorial, I will be showing you how you can implement a share button on Android Action Bar Menu.
You could be going through tough time implementing this feature, most of the time you could do it correctly but it’s not active. Why, definitely something is not done the way it suppose.
So let get started with the process

Step 1











The first step to take is to create menu.xml
How to do this, click on Res just to highlight
-Click on File
-Click on New
-Click on Android Resource File
-On File Name- Type a name e.g. action_menu
-Under Resource type, select menu

Step 2
Open menu.xml file under res and past the bellow code
<item
        android:id="@+id/share"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:icon="@android:drawable/ic_menu_share"
        android:title="Share"
        app:showAsAction="always" />


Step 3
Go to mainactivity.java- import the bellow
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

Step 4
Paste this code on the onCreate


}

    public void shareText(View view) {
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/plain");
        String shareBodyText = "Your shearing message goes here";
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject/Title");
        intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
        startActivity(Intent.createChooser(intent, "Choose sharing method"));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.share:

                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String shareBodyText = "Check it out. Your message goes here";
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject here");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
                startActivity(Intent.createChooser(sharingIntent, "Shearing Option"));
                return true;

            default:
                return super.onOptionsItemSelected(item);


 
  }