I'm following a course on Udacity on building an Android app (weather, in this case). I've been having trouble implementing a Share action. After getting some advice from another forum, I changed the min SDK version from 10 or 11 to 17, since this is just a learning activity. Currently, I have the "Share" button showing up in the action bar, but tapping on it does nothing. I tried putting it in the overflow menu, but still, nothing. I tried some debugging, but I don't know where the button click is supposed to be handled; the debugger goes through and creates the shareIntent object, but then nothing seems to happen with it. I looked at this doc, but when I try to handle the sharing in the view's onOptionsItemSelected, I get a null pointer exception on the call to createShareIntent. What am I missing?
Here's the nested fragment's onCreateOptionsMenu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail_fragment, menu);
MenuItem item = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = new ShareActionProvider(getActivity());
if(mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareIntent());
} else {
Log.d(LOG_TAG, "Share action provider is null");
}
}
Here's the containing view's onOptionsItemSelected, with the problematic code commented out:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
} else if (id == R.id.action_share) {
//DetailFragment details = (DetailFragment) getFragmentManager().findFragmentByTag("detailFragment");
//startActivity(details.createShareIntent());
}
return super.onOptionsItemSelected(item);
}
And here's the createShareIntent method:
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mForecastStr + FORECAST_SHARE_HASHTAG);
return shareIntent;
}
Currently, I have the "Share" button showing up in the action bar, but tapping on it does nothing.
Make sure you are going through your code branch that calls setShareIntent(), and make sure that the device or emulator you are testing on has an activity from some app that supports ACTION_SEND for text/plain. Also, if there is only one activity that supports ACTION_SEND for text/plain, the icon for that activity will appear adjacent to the ShareActionProvider share icon, and you would tap the activity icon to share with that activity.
I tried some debugging, but I don't know where the button click is supposed to be handled
That is inside the implementation of ShareActionProvider.
I looked at this doc, but when I try to handle the sharing in the view's onOptionsItemSelected, I get a null pointer exception on the call to createShareIntent.
onOptionsItemSelected() is for regular action items, not action providers.
It turns out I had put android:actionProviderClass="android.widget.ShareActionProvider in a layout XML file instead of a menu one. I don't think the Udacity course I'm taking mentioned the correct location for that (perhaps due to compatibility things).
Related
My android application has the following activity flow
Listing -> Buy -> Sell
From the listing when an item is clicked/selected an id is passed to buy to pull the relevant data from the server. If a user owns a certain item there will be a sell option as well from the buy screen (which doubles as an overview of said item).
The problem I am facing is that when you are on the sell screen and press the android back button you are taken back to the buy screen in it's original state, however when you click the back arrow (up) from the activity in the toolbar, it seems that it is actually trying to launch a new activity and throws an exception (since the activity cannot know what the id was). The buy activity is listed as a parent to the sell activity in the manifest.
I need to somehow make the up button to act the same as the back button on this particular activity, or at least pass the id back somehow.
Any suggestions would be appreciated.
If you're using an ActionBar (either platform or AppCompat) override the following method in the activity from which you wish to go back.
#Override
public boolean onOptionsItemSelected(#NonNull final MenuItem item) {
final int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
This should be safe to use in a simple navigation hierarchy such as yours.
Just yesterday i did this for my app. This is what I did:
In manifest file I removed
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
that was part of my DetailsActivity.
In the DetailsActivity I added this line to onCreate
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and in onOptionsItemSelected() method I added these lines
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
Not sure if it is the best way to handle this but oh well it works :)
In your Activity, onOptionsItemSelected() method get the clicked item id, if the id is android.R.id.home then just finish() current activity.
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I This is from https://developers.facebook.com/docs/android/scrumptious/authenticate
This is my code (identical from the tutorial) for the options menu part
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// only add the menu when the selection fragment is showing
if (fragments[SELECTION].isVisible()) {
if (menu.size() == 0) {
settings = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
settings = null;
}
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (item.equals(settings)) {
showFragment(SETTINGS, true);
return true;
}
return false;
}
whats happening is this works most of the time but I found a test case I couldn't debug. When you logout, the menu item still shows and you have to click it to make it disappear.(it shouldn't appear at all). This wasn't a huge concern to me at the beginning but I found that when you logged in again after you clicked the menu item to make it disappear, the options menu doesn't appear at all.
I think I found the problem, I leave it up for others. From When and how often onPrepareOptionsMenu() method is called for ActionBar? ,I got that "On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu, because the menu is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items."
When I logged out, even thought there were no actual menu items, the menu was still open. However when I clicked the menu when it was empty, it messed it up so that when I logged in, show wouldn't work. I don't understand that part yet. I was hoping someone can elaborate what happened during that part
I'm programming an app based on the Weather App from Udacity Courses.
The app is based in a two activies: one general and other for weather detail with a fragment inside.
I've been experimenting with menu creation and I have and issue that I would like to comment with you all.
AFAIK, If I want to share an option between different fragments, it seems a good idea to put that option in the activity menu instead of the fragment menu, so that's what I'm doing with the Share button.
But I've seen that I have two possible situations:
1) If the menu item inside the xml is declared as showAsAction = never I can create the instance of the ShareActionProvider and the intent inside the onOptionsItemSelected, within the corresponding if sentence (and works fine):
public class DetailActivity extends Activity {
ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new DetailFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
if (id == R.id.action_share) {
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "nada");
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
2) If the menu item is declared as showAsAction = always and the Share button is in the action bar, the above doesn't work as the Share button isn't even clickable. I have to declare the above code inside the onCreateOptionsMenu to make things work.
Why is that? Is different the chain of events for creating a menu when is not in the action bar Vs. when it's in it?
Thanks for the answers and excuse me for my english.
In my app, I have an icon in the action bar. When I click on it, I get an option called Settings. However, when I click on this option, nothing happens. I have searched, but I can't find out how to utilize this option. I would like for a dialog box to open when Settingsclicked (or if another Activity opened that would be cool too), which I could then add content to. Does anyone know how I would go about this?
The solution Dave S provided works but I want to share another way you can handle this functionality.
In your Activity you can have the following code to interact with your 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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
displaySettingsDialog();
}
return super.onOptionsItemSelected(item);
}
The key is to make sure you are checking for the right ID you assigned to your menu item.
If you wanted to launch a new activity (SettingsActivity) then you can replace the displaySettingsDialog() function with the following:
Intent intent = new Intent(this, SettingsActivity.class)
startActivity(intent);
Look for main.xml in your res/menu/ folder. (at least for eclipse ADT) There you can specify an onClick attribute which will call a function in your code. from there you can do whatever you want with it.
EDIT
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="#+id/fb_login"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/facebook_login_label"
android:onClick="showFacebookDialog"/>
</menu>
This would define the item for my MainActivity, inside which you define the onClick like so
public void showFacebookDialog(final MenuItem item)
{
...
}
I use this example
http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider
But, i want to now, if its possible change the text "see all" for other text, for example, my gallery app, have the text in spanish "ver todo", if possible to change?
And.. if possible know when the share button in the action bar (in my case actionbarsherlock) are press?
i see "However, if the action provider provides a submenu of actions, then your activity does not receive a call to onOptionsItemSelected() when the user opens the list or selects one of the submenu items" but..no other way to know it? I want that when the user press that "button" make one accion also show the list of app to share.
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater= getSupportMenuInflater();
inflater.inflate(R.menu.menu_resultados, menu);
MenuItem item = menu.findItem(R.id.menu_compartir);
mShareActionProvider =(com.actionbarsherlock.widget.ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent ()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_EMAIL,"TestText");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "TestSubject");
return shareIntent;
}
Try setting setOnMenuItemClickListener for ex:
MenuItem item = menu.findItem(R.id.menu_compartir);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
});
You can change the string at actionbarsherlock 's project, by changing lines at res/values/abs__strings, or you could create a folder res/values-es, copy there abs__strings, and edit the lines there. That way, you would have both english and spanish strings on your code, depending on device's language. The line you're looking for in abs__strings is called abs__activity_chooser_view_see_all.
Just a tip, try using the search function in Eclipse for these string changing next time. I found this out by searching "See all...".
About knowing when the button has been pressed, I couldn't figure it out. I just tested that onOptionsItemSelected() does not work, just as you said...