Skip to content
Related Articles

Related Articles

How to Add Share Button in Toolbar in Android?

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 09 Apr, 2021

In this article, we are going to create a simple Share Button in the Toolbar in Android. The Share Button is used to share information on mail, Bluetooth, Facebook, Twitter, WhatsApp, etc to an individual person or a group on any social media. We can share any type of message like text, images, videos, links, etc. Note that we are using Java as the programming language. A sample video is given below to get an idea about what we are going to do in this project. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Create a New Android Resource Directory

Go to the res folder, right-click on it and follow the steps shown in the below image to create a new Android Resource Directory. 

Now Right-click on Android Resource Directory, a new tab is opened. Do as shown below:

Give a name to your directory and click ok. A new Android Resource Directory is created. 

Step 3: Create a Menu Resource File

Go to the menu directory, right-click on it and follow the images given below:

Now right-click on Menu Resource Directory and do as shown in the image below:

Give a name to your file and then click ok. A new Menu Resource File has been created.

Step 4: Create an icon

Navigate to res > drawable. Now, right-click on the drawable folder and follow the images given below:

Now right-click on Vector Asset and do as shown below:

i) choose the icon by clicking on clip-art and then search for icon share. 

if you want to give some to your icon then write it in Name, otherwise, the default name is generated.

ii) choose a color for your icon by clicking on the color option 

Click on choose then next and finish, your icon has been created. Here ic_baseline_share_24 is given by default.

Step 5: Working with the main_menu.xml

Navigate to res > menu > main_menu.xml and add the below code to that file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--we are using  app:showAsAction="ifRoom" so that share
        button is added in toolbar. -->
        app:showAsAction="ifRoom" />

Step 6: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
        android:text="Hello GFG !!"
        app:layout_constraintTop_toTopOf="parent" />

Step 7: Working with the file

Go to the file and add the code given below. We have implemented two methods public boolean onCreateOptionsMenu() and public boolean onOptionsItemSelected() here. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        // first parameter is the file for icon and second one is menu   
        return super.onCreateOptionsMenu(menu);
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // We are using switch case because multiple icons can be kept
        switch (item.getItemId()) {
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                // type of the content to be shared
                // Body of the content
                String shareBody = "Your Body Here";
                // subject of the content. you can share anything
                String shareSubject = "Your Subject Here";
                // passing body of the content 
                sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
                // passing subject of the content
                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, shareSubject);
                startActivity(Intent.createChooser(sharingIntent, "Share using"));
        return super.onOptionsItemSelected(item);


You can use any medium like Facebook, WhatsApp, email, messaging, Bluetooth, etc. to share your message.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!