2

I'm quite new to Android development and I'm trying to build an ActionBar with the ShareActionProvider, using the "stock" ActionBar (i.e. not using ActionBarSherlock), because I want to target ICS+ devices only.

I would like the history function enabled (as it is by default), but I don't want the most used intent icon near the share button, because it takes too much space.
I know that using ActionBarSherlock this is possible, but as I said I'm trying to do it with the stock ActionBar, so I would like to know how to overcome to this problem.

I've found these two related answers:
How to hide the share action (which use most) icon near the share action provider?
Android: ShareActionProvider with no history

but none of them clearly say how to edit the Android sources to have another "version" of the ActionBar that hides the icon.
I mean, how can I combine the two answers and ship my application with the modified ActionBar class that hides the most-used icon?

EDIT:
I'm looking for a brief list of operations that have to be done to extend the three classes ShareActionProvider, ActivityChooserView and ActivityChooserMode to build a modified version of them that doesn't raise errors in Eclipse. AFAIK this is not documented elsewhere on the web (Google doesn't give results).

Community
  • 1
  • 1
virtualdj
  • 413
  • 6
  • 18

2 Answers2

1

but none of them clearly say how to edit the Android sources to have another "version" of the ActionBar that hides the icon.

That is because you do not change the action bar. You change the ShareActionProvider. If you do not like the existing implementation of ShareActionProvider, create your own, perhaps using the existing implementation as a starting point.

For example, that is what the accepted answer on the Android: ShareActionProvider with no history question describes. Neither the question nor the answer reference ActionBarSherlock.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes, you're right, but what I was looking for is an explanation on how to actually "write my own ShareActionProvider by copying the one found in Android source and copy over the ActivityChooserView and the ActivityChooserModel from source". I wasn't able to find any example on this on the web, and trying to simply copying the files from android Sources [here](http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/widget/ShareActionProvider.java/?v=source) generate a lot of errors in Eclipse. – virtualdj Jul 29 '13 at 18:31
  • @virtualdj: The "lot of errors" mostly will come from references to internal resources (the ones from `com.android.internal.R`). You will need to create your own replacements for those resources, then fix up the `R` references. Otherwise, perhaps you are better served having a simple "share" action bar item that launches a chooser dialog, as opposed to using `ShareActionProvider`. I doubt that you will find step-by-step instructions on changing `ShareActionProvider`. – CommonsWare Jul 29 '13 at 21:49
  • A copy & paste of the 3 complete files on the @steemcb [post](http://stackoverflow.com/a/13939466/2631300) would be enough for me to understand, I think. Unfortunately I cannot comment on that answer to ask for the full files, as I don't have enough SE reputation :-( – virtualdj Jul 30 '13 at 11:09
  • Hi, I'm still trying to work on this problem. I've changed the package from the **ShareActionProvider.java** and imported the resources, but how do I solve the _"OnChooseActivityListener cannot be resolved to a type"_ error? I've tried to copy also **ActivityChooserModel.java**, but then I get _"PackageMonitor cannot be resolved to a type"_ there (on the line "import com.android.internal.content.PackageMonitor;" and several following that use that import). Have you got any clues? – virtualdj Aug 03 '13 at 13:11
  • @virtualdj: `OnChooseActivityListener` is three lines of code in `ActivityChooserModel` (not counting comments). Assuming that you do not need the rest of `ActivityChooserModel`, copy those three lines into a file in your project and fix up the import to refer to your copy. – CommonsWare Aug 03 '13 at 17:00
  • Actually the rest of `ActivityChooserModel` is needed, and `ActivityChooserView` is needed too (to hide the icon). What I cannot understand is why some lines remain **undefined** in Eclipse (red underline), such as `mProvider.subUiVisibilityChanged` (in ActivityChooserView) and `context.registerReceiverAsUser` (in PackageMonitor) even though I'm copying the sources from API-17 and I'm targetting SDK version 17. Looks like Eclipse doesn't know them (and the imports are correct of course, otherwise there will be a lot more errors). – virtualdj Aug 03 '13 at 19:10
  • @virtualdj: You may wish to examine ActionBarSherlock's backport of `ShareActionProvider`, to see how Jake did it, even if you will not be using it directly. – CommonsWare Aug 04 '13 at 11:06
0
  1. Copy source files ActivityChooserView.java and ShareActionProvider.java files from https://android.googlesource.com/platform/frameworks/support.git into your project.
  2. Do a Ctrl+Shift+O on eclipse on both the files to fix up any imports. In your ShareActionProvider.java, change the import for ActivityChooserView.java to the new file.
  3. Change import ShareActionProvider in the file in which you have implemented onCreateOptionsMenu() to the location to where you copied this file.
  4. In menu.xml, correct the ShareActionProvider class to the new one.
  5. Look for if( activityCount==1 || activityCount > 1 && historySize > 0 ) in ActivityChooserView.java. change it to if(false)

You're all set!

  • Thanks for the answer, however I think that it's not complete. First I need to copy also **ActivityChooserModel.java** from the link you've provided (thanks for that!). Then, most importantly, how do you fix the R errors such as **R.styleable.ActivityChooserView** (error "abc_activity_chooser_view cannot be resolved or is not a field")? – virtualdj Mar 22 '14 at 13:10
  • Sorry, I was blind and didn't look at the files in the **res** directory on the support.git. However, after copying all the files, I still have problems because the sources use the ActionBarCompat while I'm not using the support library on the application (I'm on ICS+). I will try to manage it... – virtualdj Mar 22 '14 at 14:05