androidandroid-fragmentsandroid-webviewshareshareactionprovider

Add share action to android WebView fragment


I know this question has been asked many times before, and i have tried many of those answers, but nothings works for me, So here is my question. I have android app which uses WebView, earlier I was using WebView in activity in which adding share action which shares current URL link of the page was easy, but in recent update i have added WebView in fragment, and now I am not able to add share action in that fragment. As I have added button which shows on action bar but it noes not respond to click. following are my codes.

main.xml

<item android:id="@+id/menu_item_share"
        android:icon="@drawable/ic_menu_share"
        android:title="@string/menu_item_share"
        app:showAsAction="always"/>

WebviewFragment.java

public class WebviewFragment extends Fragment {
    ProgressBar bar;
    WebView myWebView;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:{
                    webViewGoBack();
                }break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_webview, container, false);
        ;
        myWebView = (WebView) v.findViewById(R.id.WebView);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("http://www.google.com/");
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //improve webView performance
        myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        myWebView.getSettings().setAppCacheEnabled(true);
        myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);
        myWebView.setWebViewClient(new WebViewClient());
}

share action in MainActivity.java

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

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            //share app content
            case R.id.menu_item_share: {
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT, myWebView.getUrl());
                startActivity(Intent.createChooser(shareIntent, "Share This Website!"));
                shareIntent.setPackage("com.whatsapp");
                break; //or, return true;
            }

Please help with the working solution.


Solution

  • Use these methods in your Fragment class and add your sharing funcionality in these methods:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Do something that differs the Activity's menu here
    super.onCreateOptionsMenu(menu, inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    
        case R.id.activity_menu_item:
    
            // Not implemented here
            return false;
        case R.id.fragment_menu_item:
    
            // Do Fragment menu item stuff here
            return true;
    
        default:
            break;
    }
    
    return false;
    }
    

    And in your "OnCreateView()" method of Fragment add this line:

    setHasOptionsMenu(true);
    

    Hope it will work!