Set sharing intent using icon in action bar

天涯浪子 提交于 2019-12-06 16:21:39

I have found the answer. It was rather simple in the end.

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

@Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.menu_item_share:
        shareURL();
}
if(item.getItemId() == R.id.menu_item_refresh){
    mWebView.reload();
    return true;
}
return super.onOptionsItemSelected(item);
}

private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This Website!"));
}


}

This also allowed me to have a refresh button too.

I don't see any override for onCreateOptionsMenu(..), are you sure you have added your ActionItems? Take a look at http://developer.android.com/guide/topics/ui/actionbar.html.

Ali.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!