I'm trying to implement a share intent to share images. I have a full screen activity which extends ActionBarActivity and a fragment that implements the immersivemode as the android developer's guide explains, so the user can see the image in fullscreen. The problem is that even if I can see the share icon on ActionBar, it seems that it can't be clicked, apparently because the method onOptionsItemSelected is never called.
Here's the onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_share, menu);
return true;
}
onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.menu_share:
Intent i = getIntent();
uriString = i.getStringExtra("uri");
if(uriString != null) {
Uri uri = Uri.parse(uriString);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
mShareActionProvider.setShareIntent(createShareIntent(uri));
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here's the method I created to prepare and start the intent:
private Intent createShareIntent(Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_image)));
return shareIntent;
}
menu_share.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_share"
android:title="Share"
app:showAsAction="ifRoom"
android:icon="#drawable/abc_ic_menu_share_mtrl_alpha"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>
</menu>
I've tried different ways, for instance using setOnMenuItemClickListener or similar things which consider listeners, but I see that these listeners are ignored when I debug. I've also searched for a solution here and on other websites but I can't get how to solve this problem, so any idea will be appreciated.
Thanks in advance!
You are inflating the wrong menu. Since your menu file is called menu.xml you should do a
getMenuInflater().inflate(R.menu.menu, menu);
You dont directly inflate an entry, but the menu.
Also below your
switch (item.getItemId()) {
case R.id.menu_share:
write: Log.d("mytag", "share menu clicked"); Then you can verify in your android logcat, that there is nothing wrong with the menu clicking, but with the Intent you are receiving from.
Eventually, I changed my approach to solve the problem and now I'm able to call the onOptionsItemSelected(MenuItem item) method and share images from my app to the others. I put the code below.
onCreateOptionsMenu(Menu menu):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menuItem = menu.add(Menu.NONE, R.id.action_share, Menu.NONE, R.string.action_share);
menuItem.setIcon(R.drawable.abc_ic_menu_share_mtrl_alpha);
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
mShareActionProvider = new ShareActionProvider(this);
return true;
}
onOptionsItemSelected(MenuItem item):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
Intent i = getIntent();
uriString = i.getStringExtra("uri");
if (uriString != null) {
Uri uri = Uri.parse(uriString);
MenuItemCompat.setActionProvider(item, mShareActionProvider);
createShareIntent(uri);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
My method to create the share intent when I click on the right menu item:
private void createShareIntent(Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
The id of the item is into ids.xml in values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="action_share"/>
</resources>
strings.xml:
<!-- Actions -->
<string name="action_share">Share image</string>
As you can see, I don't use getMenuInflater().inflate(int menuRes, Menu menu) anymore, but I use the add() method to generate my menu and it works for me.
Related
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.
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());
I have created an android project, with a couple of activities.
I have also added the back button feature into the activities, however when I click it
nothing happens.
Have I done something wrong?
I've put this code into each of the activities, and none of them work
Any opinion/input is greatly appreciated.
Code for trying to go back from my Gallery activity to the main:
//GalleryActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
// return true;
MenuInflater mif = getMenuInflater();
mif.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
and.. in res/menu/
<item
android:id="#+id/back_icon"
android:icon="#drawable/ic_action_back"
android:title="#string/back_title"
android:showAsAction="always"
/>
I downloaded the proper android design icons, and have added them to the drawable folders too.
EDIT:
Main Activity
button4= (Button) findViewById(R.id.button4);//find the button
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), GalleryActivity.class);
startActivity(i);
finish();//close main activity after start info activity
}
});// links to gallery page
you have to override onOptionsItemSelected and check for the id. For instance:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back_icon:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You have to specify the action of the options, included the home/back action as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.back_icon:
onBackPressed(); //Or whatever you want to do when back_icon is pressed!
return true;
default:
return super.onOptionsItemSelected(item);
}
}
My code:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
return true;
}
This works to open the settings menu (Preferences.class) but I would like it to close the menu if it's already open when this system button is pressed. What can I add or change to make that happen?
Here is how you should be implementing what you want:
First thing first, you want to supply a "Settings" item that can selected by the user to get to your Preferences class. You do that by adding an item to your menu/main.xml and inflating it in onCreateOptionsMenu().
menu/main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Settings"/>
...
</menu>
Inside your Main Activity, override this to inflate your main.xml file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
...
return super.onCreateOptionsMenu(menu);
}
And then override this to handle what happens when a Menu item is selected, in this case the Settings item:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_settings:
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
return true;
...
default:
return super.onOptionsItemSelected(item);
}
}
I was able to get the menu to close by adding this to my customized preferences menu class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
onBackPressed();
return false;
}
which calls this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
which takes the user back to the screen to play another round. I don't know if this is an acceptable solution for every case but it worked for me.
I am using Sherlock Library Action Bar, which is similar to the ICS action bars, the place where I am stuck is this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Save")
.setIcon(R.drawable.ic_compose).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Search")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
SubMenu sub = menu.addSubMenu("Options");
sub.add(0, SubMenu.NONE, 0, "First");
sub.add(0,SubMenu.NONE, 1, "Second");
sub.add(0, SubMenu.NONE, 2, "Three");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// HERE IS WHere I AM FACING PROBLEM IN
getSupportMenuInflater().inflate(R.menu.share_action_provider, menu);
// Set file with share history to the provider and set the share intent.
actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); //this is BRILLIANT WAY TO AVOID REPEATation
actionProvider.setShareIntent(createShareIntent());
return super.onCreateOptionsMenu(menu);
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
//THIS TEXT IS WHAT I WANT TO OBTAIN DYNAMICALLY
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Something goes here");
return shareIntent;
}
Whenever I try putting the actionProvider.setShareIntent(createShareIntent()); inside a listener or anywhere other than the actionProvider.setShareIntent(createShareIntent()); I get D/View(11753): onTouchEvent: viewFlags is DISABLED
I wish to include this sharing Action provider and want to input my own text that gets generated on user inputs.
Any inputs are welcome.
Adding this doesn't work
actionItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.e("THIS IS NEVER CALLED", "??");
return true;
}
});
How do I get an update for the Share button being clicked
I hit a similar issue with a android.support.v7.app.ActionBarActivity derivative (though I think this approach should work for any activity) and I overrode onPrepareOptionsMenu and got the effect you were after:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
if (actionProvider != null) {
actionProvider.setShareIntent(createShareIntent());
}
return super.onPrepareOptionsMenu(menu);
}
i use menu like this:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.your_share_button_name:
share();
return true;
}
}
private void share() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"your text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"your subject");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
that's all. I suggest you to put all the layout of the menu in a xml in your res/menu folder.