How to create share button in android using intent? - android

My app has a share button that shares a link to the app on the play-store. I'm new to the intent method and keep getting this error:
And this error
Any input on how to correctly create a share button is greatly apreciated! Below is the code in ActivityMain and below that; the XML to the share button.
private ShareActionProvider shareActionProvider;
#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;
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.nav_share);
// Fetch and store ShareActionProvider
shareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (shareActionProvider != null) {
shareActionProvider.setShareIntent(shareIntent);
}
SHARE BUTTON XML
<item android:title="Communication">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
</item>

On your menu item / button click call the following method.
private void shareAppLink(){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_VIEW);
shareIntent.setData(Uri.parse("Link to app store"));
startActivity(shareIntent);
}

Here is sample code:
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
</menu>
#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;
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.nav_share);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.nav_share){
ShareApp();
}
return false;
}
});
// Fetch and store ShareActionProvider
shareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
private void ShareApp() {
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
String shareMessage = "\nLet me recommend you application\n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch (Exception e) {
DebugLog.e(e.getMessage());
e.printStackTrace();
}
}

Related

In android coding of share button got null pointer exception on this line mShareActionProvider.setShareIntent(getDefaultShareIntent());

Logcat Image In android coding of share button got NullPointerException on this line mShareActionProvider.setShareIntent(getDefaultShareIntent());
Can anyone solve this error?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
// Now get the ShareActionProvider from the item
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
/** Getting the actionprovider associated with the menu item whose id is share */
// mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
return intent;
}
UpDate :
import this
import android.widget.ShareActionProvider;
declare it global
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider)shareItem..getActionProvider();
setShareIntent(getDefaultShareIntent());
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
shareIntent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
return shareIntent;
}

How to set the menu bar icon initially according to the server response

I am having list of Colleges to display and click the perticular college it goes to the detail page. In there there is a menu bar with notification and Favourite_icon.Here i am having the Favourite_icon and Favourite_icon1. If users clcik the favourite_icon it stored as favourited in server and the icon changed as Favourite_icon1. After do some process i have visted the Favourited college. That time it should show the Favourite_icon1 in menu bar. I have tried the following method but nothing happend . I have added the the code which i have tried
menu_clg.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UserDashBoardFragment">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/icon_selector"
appmunu:showAsAction="always"
android:title="Favourite" />
</menu>
this Activity code
private boolean canAddItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_clg, menu);
mMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
navigatetoNotification();
return true;
case R.id.action_favourite:
if (item.getItemId() == R.id.action_favourite) {
invalidateOptionsMenu();
favouriteClg();
}
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (canAddItem) {
menu.getItem(1).setIcon(R.drawable.vijay);
canAddItem = false;
favouriteClg();
} else {
menu.getItem(1).setIcon(R.drawable.favourite_icon);
canAddItem = true;
favouriteClg();
}
return super.onPrepareOptionsMenu(menu);
}
this is the code for check wheather the college is already favourited or not in onCreate() method
public void chechFavourite() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... arg0) {
//method name changed here
//getAllEventFromUser method used for getting all previously send events of current user
return favouriteDelegates.getAllCollegeDetails(userMO, context);
}
#Override
protected void onPostExecute(String collegelists) {
if (collegelists != "null") {
initCollegeMO = gson.fromJson(collegelists, new TypeToken<InitCollegeMO>() {
}.getType());
collegeMOs = initCollegeMO.getCollegeMOs();
for (CollegeMO collegeMO1 : collegeMOs) {
//here the list of college has eceived from server so i checked all the college id with current college id
collegeId = collegeMO1.getCollegeId(); //here collegeMO isthe object which is accessed by parcelable from another activity
if (collegeMO.getCollegeId() == collegeId) {
canAddItem = true;
} else {
canAddItem = false;
}
}
} else {
canAddItem = false;
}
}
}.execute(null, null, null);
}
}
Hope below code would be helpful for you.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.actionbar, menu );
userItem = menu.findItem(R.id.userItem);
return menu;
}
Edit 1:
Method which will update menu programatically.
private void updateMenuTitles() {
if(isFavorite){
userItem.setIcon(new BitmapDrawable(getResources(), favoriteBitmap));
}else{
userItem.setIcon(new BitmapDrawable(getResources(), unFavoriteBitmap));
}
invalidateOptionsMenu();
}
Hope this would help you.
You can change the title of your MenuItem runtime and manage the click event according to their title.
See this code, hope it will help you.
You can call updateMenuTitles() after getting response from API and change the title.
private Menu menu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Create your menu...
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favourite:
if (item.getTitle().equals("favourite")) {
//Alerady Favourite, make it unfavourite
MenuItem.setTitle("un-favourite");
// do what you want
}
else if(item.getTitle().equals("un-favourite")){
// un-favourite, make it favourite
MenuItem.setTitle("favourite");
// do what you want
}
return true;
}
return super.onOptionsItemSelected(item);
}
/*
Update title of your menuItem whenever you want
*/
private void updateMenuTitles() {
MenuItem MenuItem = menu.findItem(R.id.action_favourite);
if (MenuItem.getTitle().equals("favourite")) {
//Alerady Favourite, make it unfavourite
MenuItem.setTitle("un-favourite");
}
else if(MenuItem.getTitle().equals("un-favourite")){
// un-favourite, make it favourite
MenuItem.setTitle("favourite");
}
}

ShareActionProvider not clickable?

I have a ShareActionProvider in my ActionBar and I've followed google's example for it to the tee. I'm curious why it's not clickable?
EDIT: I know that if I remove the shareHistory function it becomes clickable but I don't want to keep shared history.
xml:
<item
android:id="#+id/menu_item_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:icon="#android:drawable/ic_menu_share"
android:showAsAction="ifRoom"
android:title="Share" />
Java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_legislation, menu);
MenuItem item = (MenuItem)menu.findItem(R.id.menu_item_share);
sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.ACTION_ALL_APPS, "http://www.anilinkz.com/");
mSharedActionProvider = (ShareActionProvider)item.getActionProvider();
setShareIntent(sharingIntent);
return true;
}
public void setShareIntent(Intent shareIntent) {
if (mSharedActionProvider != null) {
mSharedActionProvider.setShareIntent(shareIntent);
mSharedActionProvider.setShareHistoryFileName(null);
}
}
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_legislation, menu);
MenuItem item = (MenuItem)menu.findItem(R.id.menu_item_share);
sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "http://www.anilinkz.com/");
mSharedActionProvider = (ShareActionProvider) item.getActionProvider();
setShareIntent(sharingIntent);
return true;
}
public void setShareIntent(Intent shareIntent) {
if (mSharedActionProvider != null) {
mSharedActionProvider.setShareIntent(shareIntent);
}
}

Adding an Easy Share Action in ActionBar

I have problem. I was follow a lot of guides, but still can't get my Share button to work. I got the icon showing in ActionBar, but when I press nothing is happening when android:showAsAction="always". But when android:showAsAction="never" it works. I just want share icon to be always shown in ActionBar. What I am doing wrong? Please help
Here is my code:
public class Main extends Activity {
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.shareButton);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.shareButton:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Check it out";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
return true;
case R.id.aboutButton:
Intent intent = new Intent(this, About.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/shareButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="#string/share"
android:showAsAction="always"/> **---> when I put here "NEVER" then it works! But I want Share to be always as icon**
<item
android:id="#+id/aboutButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="never"
android:title="#string/about_dev"/>
</menu>
If you didnĀ“t find a solution try this to get the ShareActionProvider
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share)
.getActionProvider();
mShareActionProvider.setShareIntent(doShare());
return true;
}
And doShare() would be:
public Intent doShare() {
// populate the share intent with data
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Put whatever you want");
return intent;
}
Try "Intent share = new Intent (Intent.ACTION_SEND)" instead.
You seem to be doing everything right!
In
case R.id.shareButton:
instead of using
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
use:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
mShareActionProvider.setShareIntent(sharingIntent);
Refer the following links for more information:
https://developer.android.com/reference/android/widget/ShareActionProvider.html
https://developer.android.com/training/sharing/shareaction.html#set-share-intent

Android share app dialog box display error

im having problem implementing the menu to share my app. When ever i open my activity that has the icon share_button, the dialog box "share via" is displayed immediately. I reckon i have problem with this line of code "startActivity(Intent.createChooser(shareIntent(), "Share..."));"
here is my code
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareHistoryFileName(null);
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" +
getPackageName();
String yourShareText = "Install this app " + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
startActivity(Intent.createChooser(shareIntent(), "Share via"));
return true;
}
menu item
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:icon="#drawable/ic_share"
android:actionProviderClass="android.widget.ShareActionProvider" />
Change you code like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareHistoryFileName(null);
return true;
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_item_share:
String playStoreLink = "https://play.google.com/store/apps/details?id=" +
getPackageName();
String yourShareText = "Install this app " + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
startActivity(Intent.createChooser(shareIntent(), "Share via"));
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
onCreateOptionsMenu will call when your activity will call.
You need to start the share intent when Share button pressed`
So call share Intent in onOptionsItemSelected
You have to move this line of code startActivity(Intent.createChooser(shareIntent(), "Share via")); out into a button click handler or menu select handler.
The function onCreateOptionsMenu() will be called to setup the menu and that will happen when your Activity starts up.

Categories

Resources