简体   繁体   中英

How to make a share button

I would like to add a share button that opens something like this.

分享

I am using this source from the official Android site

I want to send standard text so I thought I should use this part:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");

Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);

But I have no idea where to place the code. How would I do it if I wanted to place it in 1 of these 3 spots?

分享位置

I would prefer to have it in the Menu or up top and just remove the button below but I am open to any solution.

To add a click event listener to the FloatingActionButton :

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        share();
    }
});

private void share(){
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    sendIntent.setType("text/plain");
    
    Intent shareIntent = Intent.createChooser(sendIntent, null);
    startActivity(shareIntent);
}

To handle a menu item click:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.yourItemId:
        share();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From your comment: This worked for me. Is there anyway to do this on top of the screen next to the settings button? This worked for me. Is there anyway to do this on top of the screen next to the settings button?

You have to create menu directory in resource/res . Then, create a menu. Add following source code to menu.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item  android:id="@+id/share"
        android:title="Shar"/>
</menu>

Add following source code to MainActivity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id){
        case R.id.share:
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");

            Intent shareIntent = Intent.createChooser(sendIntent, null);
            startActivity(shareIntent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

If you want to trigger this code from your navigation drawer, then you need to override the onNavigationItemSelected method in your calling activity like this

 @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    switch (item.getItemId()) {

       case R.id.nav_share: {
      Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");

            Intent shareIntent = Intent.createChooser(sendIntent, null);
            startActivity(shareIntent);
            break;
        }  
    }
    //close navigation drawer
    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

Identify the ID of the menu item, then place the code accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM