1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.supportv7.app;
18
19import android.content.Intent;
20import android.view.Menu;
21import android.view.View;
22
23import androidx.appcompat.app.AppCompatActivity;
24import androidx.appcompat.widget.ShareActionProvider;
25import androidx.core.view.MenuItemCompat;
26
27import com.example.android.supportv7.R;
28
29/**
30 * This activity demonstrates how to use {@link ShareActionProvider} with the Action Bar.
31 */
32public class ActionBarShareActionProvider extends AppCompatActivity {
33
34    @Override
35    public boolean onCreateOptionsMenu(Menu menu) {
36        getMenuInflater().inflate(R.menu.action_bar_share_action_provider, menu);
37        return true;
38    }
39
40    @Override
41    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
42        final ShareActionProvider sap = (ShareActionProvider) MenuItemCompat.getActionProvider(
43                menu.findItem(R.id.menu_item_share_provider_action_bar));
44
45        final Intent shareIntent = new Intent(Intent.ACTION_SEND);
46        shareIntent.setType("text/plain");
47        shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello!");
48
49        sap.setShareIntent(shareIntent);
50
51        return true;
52    }
53}
54