Android Sherlock Actionbar onOptionsItemSelected is not called - android

I m using Sherlock Actionbar to show share option in Action Bar of my application.
The Share option works fine.
I want to get the option which user selects from Share option, so that I can share
different text for facebook, messages, mail etc.
For this I am using onOptionsItemSelected function but this function
is never called.
Please help me to fix this or is there any workaround
to achieve this. Thanks..
public class MainActivity extends SherlockActivity {
private ShareActionProvider mShareActionProvider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getSupportMenuInflater().inflate(R.menu.items, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
Intent intent = getDefaultShareIntent();
if(intent!=null)
mShareActionProvider.setShareIntent(intent);
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,"Sample Content !!!");
return intent;
}
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
System.out.println("Testing...................");
return false;
}
}
Items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/share"
android:title="#string/share"
android:showAsAction="always"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
/>
</menu>

For this I am using onOptionsItemSelected function but this function is never called.
It is not supposed to be called. It is not called for any action provider.
I want to get the option which user selects from Share option, so that I can share different text for facebook, messages, mail etc.
That is not directly supported by ShareActionProvider.

Related

Android: SharingActionView on swipe from bottom

I would like to share data with other Apps, but I don't want the standard popup which appears bottom tool bar; I want this kind of view:
I followed the official document: https://developer.android.com/training/sharing/shareaction.html
So is there a native component to do that in Android SDK? Or is there any library to do that?
thank for your help guys!
You can do it like this :
First create a menu item :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.rashish.myapplication.MainActivity">
<item
android:id="#+id/action_share"
android:orderInCategory="100"
android:title="#string/action_settings"
android:icon="#android:drawable/ic_menu_share"
app:showAsAction="always" />
</menu>
Then create a method to show the Intent chooser dialog, like this :
private void share() {
String textToShare = "hello";
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// set the type here i.e, text/image/ etc.
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(Intent.EXTRA_TEXT, textToShare);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
Then in the onOptionsItemSelected method, call this function when the menu item is clicked :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
int id = item.getItemId();
if (id == R.id.action_share) {
share();
return true;
}
return super.onOptionsItemSelected(item);
}
Also make sure you've overriden onCreateOptionsMenu in your Activity like this :
#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);
return true;
}
Here's the output :
screenshot from an android device
In Android OS Lollipop google has change their share intent design. So to get this design you should have lollipop version and above.
Update:
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
File photoFile = new File(getFilesDir(), "foo.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
mShareActionProvider.setShareIntent(shareIntent);
}
}
try like this.

Unable to click on ShareAction Provider

I am trying to click on the ShareActionProvider button on my mobile, but it is completely unresponsive. None of the sharing apps such as texting or emailing pop up. I've labeled the ShareActionProvider as menu_share in my xml file, and am using the method onOptionsItemSelected to respond to any clicks.
Java
import android.support.v7.widget.ShareActionProvider;
public class MainActivityFragment extends Fragment{
ArrayAdapter<String> mForecastAdapter;
//String[] parsedWeatherData;
ShareActionProvider provider;
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
MenuItem item = menu.findItem(R.id.menu_share);
provider = (ShareActionProvider)
MenuItemCompat.getActionProvider(item);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_share:
Log.d("Weather", "Is menu share clicked");
doShare();
break;
default:
break;
}
return true;
}
public void doShare() {
String message = oneDayWeather;
// populate the share intent with data
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, message);
provider.setShareIntent(intent);
}
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bwq="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/menu_share"
android:title="#string/menu_share"
bwq:actionProviderClass="android.support.v7.widget.ShareActionProvider"
bwq:showAsAction="always"/>
</menu>
Any ideas?
Never mind, I solved it. This version of ShareActionProvider only works if you set the setShareIntent method right after getting the provider. For example:
MenuItem item = menu.findItem(R.id.menu_share);
provider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
provider.setShareIntent(getDefaultShareIntent());

Share button with ShareActionProvider added twice on Action Bar [duplicate]

This question already has an answer here:
ActionBar share item produces "Android System" thingy
(1 answer)
Closed 7 years ago.
I have an issue with a Share Button in the Action Bar.
The screenshot:
As you can see, there are two icons on the right. But I only added one icon (the first) which is inactive. The second one is active, and do what I want (share the content).
As a result, I'd just want the first icon with the behavior of the second one.
Where does the second icon come from ??!!
The menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/animation_detail_share"
android:title="#string/share"
android:showAsAction="ifRoom"
android:actionProviderClass=
"android.widget.ShareActionProvider"
/>
</menu>
The fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_inplace_animation_details, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.animation_detail_menu, menu);
MenuItem item = menu.findItem(R.id.animation_detail_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
if(mShareActionProvider != null) {
mShareActionProvider.setShareIntent(Intent.createChooser(sendIntent, getResources().getText(R.string.share)));
}
}
I inflate the menu only inside the fragment, not in the activity.
I had two issues here:
createChooser() creates the new icon
Had to add setShareHistoryFileName(null); to prevent showing another icon.
So the final code looks like:
...
mShareActionProvider.setShareHistoryFileName(null);
mShareActionProvider.setShareIntent(sendIntent);
...

Adding a "share" button to share the app on social networks

I have an app, and I'd like to add a share button to it. Once the button is clicked, I'd like it to open the following window:
Then the user will choose where to share it and it will display the following default message:
"Just found this great app! Find it here: https://play.google.com/store/apps/details?id=com.ideashower.readitlater.pro"
Solution 1: Launch ACTION_SEND Intent
When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
# change the type of data you need to share,
# for image use "image/*"
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, URL_TO_SHARE);
startActivity(Intent.createChooser(intent, "Share"));
Solution 2: Use ShareActionProvider
If you are just looking to add a Share button in Overflow Menu, also have a look at ShareActionProvider.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share, menu);
MenuItem item = menu.findItem(R.id.share_item);
actionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
String shareText = URL_TO_SHARE;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(shareText).getIntent();
actionProvider.setShareIntent(shareIntent);
return true;
}
Hope this helps. :)
As explained on Android Developers on this link: http://developer.android.com/training/sharing/shareaction.html
you have to add this menu item:
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
Then add the following code in Activity:
private ShareActionProvider mShareActionProvider;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.share_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}

Android - share button not responding

I have this intent
Button share = (Button)findViewById(R.id.share);
share.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//createShareIntent( );
}
});
and these two methods in the class:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.layout.menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
myShareActionProvider = (ShareActionProvider)item.getActionProvider();
myShareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
myShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"testing");
return shareIntent;
}
public void doShare(Intent shareIntent)
{
// When you want to share set the share intent.
myShareActionProvider.setShareIntent(shareIntent);
}
and this sdk version configuration:
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15"/>
But my problm is that I do not know how to call this. How do I actually get the menu to show up after the share button is clicked?
Thanks!
You can open an option menu from your code by calling openOptionsMenu() method. From the doc here it does-
Programmatically opens the options menu. If the options menu is
already open, this method does nothing.
But the question is why are you doing so. You have hardware menu button to open option menu. So it is better to leave that so. Even If you want to show user some option by button click then use Dialog. Why option menu?

Categories

Resources