ShareActionProvider not clickable? - android

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);
}
}

Related

How to create share button in android using intent?

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();
}
}

Action bar share button not working after upload on playstore

App share button on action bar is not working after i upload the app update on the play store as it works on the testing devices and also works on emulator.. ?
thank you in advance .
Circled part is not working after uploading on play store
Activity:
public class RecommendetableActivity extends AppCompatActivity {
Button button;
Intent ShareIntent;
ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.recommended_table);
ShareIntent = new Intent();
ShareIntent.setAction(Intent.ACTION_SEND);
ShareIntent.setType("text/plain");
ShareIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/............app name ");
button = (Button)findViewById(R.id.close_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
overridePendingTransition(R.anim.backfadein, R.anim.backfadeout);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.popup_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
// Get its ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// Connect the dots: give the ShareActionProvider its Share Intent
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(ShareIntent);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
overridePendingTransition(R.anim.backfadein, R.anim.backfadeout);
return true;
}
return false;
}
#Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.backfadein, R.anim.backfadeout);
super.onBackPressed();
}
}
popup_menu.xml:
<?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/menu_item_share"
android:title=""
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>
</menu>
I had this issue as well once and after some digging inside the Logcat of Play store ready APK, I found the problem; it might apply to you as well.
The Issue was NoClassDefFoundError exception on ShareActionProvider !
Solution is to add this rule to your ProGuard file such as proguard-rules.pro and that's it!
-keep class android.support.v7.widget.ShareActionProvider { *; }

How to add multiple activities under one onCreateOptionsMenu(Menu menu)

I want to add multiple activities under one `onCreateOptionsMenu(Menu menu) in my android app, I have already added two activities and they are working fine but third activity isn't working, following is my code
onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, 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;
}
if(item.getItemId() == R.id.share_this_app)
mShareActionProvider.setShareIntent(getDefaultShareIntent());
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!"));
shareIntent.setPackage("com.whatsapp");
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
return intent;
}
menu_main.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:title="#string/share"
android:id="#+id/menu_item_share"
android:showAsAction="always"
android:icon="#drawable/share"
/>
<item
android:id="#id/menu_item_refresh"
android:title="Refresh"
android:showAsAction="never"
android:icon="#drawable/refresh"
/>
<item
android:id="#+id/share_this_app"
android:title="Share this app"
android:showAsAction="never"
android:actionProviderClass="android.widget.ShareActionProvider"/>
From above, menu_item_share and menu_item_refresh is working, but Share this app isn't working.
It's probably your are missing a return true statement below this line mShareActionProvider.setShareIntent(getDefaultShareIntent());. You can simply follow a clean structure to achieve your task.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share: {
shareURL();
break; //or, return true;
}
case R.id.menu_item_refresh: {
mWebView.reload();
break; //or, return true;
}
case R.id.share_this_app: {
mShareActionProvider.setShareIntent(getDefaultShareIntent());
break; //or, return true;
}
return super.onOptionsItemSelected(item);
}
Hey I think this would help...
You have written
android:showAsAction="never".
So it would generate an error while putting an icon inside the menu icon(three dots at toolbar).
Hence put the value "always" or "ifRoom" for this attribute.
Hope this would work :-)

How to set a default icon in menu bar according to the server response in android

I am having list of colleges. If I click one of the college it has go to detail page which has a menu bar with favourite_icon.If I click the favourite_icon that college is stored as favourite in server and that favourite_icon changed as favourite_icon1.If we chech the college detail again after some times that menu bar favourite_icon should be favourite_icon1 if that college is already favoiurited.I am having the API to check whether the college is favourited or not if it favourited the response is like "status=fav" otherwise "status=not_fav".
Here I have added the code for your reference
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>
icon_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/vijay"
/>
<item
android:state_selected="false"
android:drawable="#drawable/favourite_icon"
/>
</selector>
activity code
#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;
}
// delete the selected event from event list added here
boolean canAddItem = false;
#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);
}
favourtieclg() method
public void favouriteClg() {
final CollegeMO collegeMO = (CollegeMO) getIntent().getSerializableExtra("CollegeMO");
DatabaseHelper db = new DatabaseHelper(context);
userMO = db.getUserData(1);
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... arg0) {
return favouriteDelegates.addFavourite(userMO, collegeMO, context);
}
#Override
protected void onPostExecute(String userData) {
if (!userData.equals("0") && null != userData) {
UserMO userMO = gson.fromJson(userData, new TypeToken<UserMO>() {
}.getType());
if (userMO.getStatus().equals("success")) {
Toast.makeText(getApplicationContext(), userMO.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}.execute(null, null, null);
}
This is how i achieved it, hope this help. You can also use supportInvalidateOptionsMenu() or InvalidateOptionsMenu() to update changes.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear(); //If you want to clear the menu
inflater.inflate(R.menu.activity_menu, menu);
updateMenuIcon(menu);
}
private void updateMenuIcon(Menu menu) {
if (menu == null) return;
if (condition) {//Check for some condition
changeMenuIcon(menu, R.drawable.fav);
} else {
changeMenuIcon(menu, R.drawable.not_fav);
}
}
private void changeMenuIcon(Menu menu, int resource) {
Drawable newIcon = getResources().getDrawable(resource);
menu.findItem(R.id.menu_fav).setIcon(newIcon);
}

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

Categories

Resources