I was working around my old ShareAction on my ActionBar and it was working since I updated my Packages on SDK Manager. I saw this doc from Google which says,
To add a "share" action to your activity, put a ShareActionProvider in
the app bar's menu resource. For example:
And I've added the same without adding any Icons:
<item android:id="#+id/action_share"
android:title="#string/share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
I was using:
app:actionProviderClass="Mypackagename.ShareActionProvider"
With a custom ShareActionProvider with the following code.you can see it here.
I saw a hack or a trick to do that (with ShareActionProvider-v4) and everything was good since I decided to use android.support.v7.widget.ShareActionProvider.
So, Here is my currently code:
<item
android:id="#+id/shareac"
android:title="#string/share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always" />
I didn't use the Icon because here the doc says,
You do not need to specify an icon, since the ShareActionProvider
widget takes care of its own appearance and behavior. However, you do
need to specify a title with android:title, in case the action ends up
in the overflow menu.
And here is what I've done so far:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_details, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.shareac);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Bundle extra = getIntent().getExtras();
String title = extra.getString("title");
Bundle extraurl = getIntent().getExtras();
String url = extraurl.getString("url");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Check this new project from something : " + title + url);
shareIntent.setType("text/plain");
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
So, here what I see right now in Android Studio 1.5.1 Is,
And if run and compile the app:
As you can see, the size of ShareAction is too much. (It's violating the MaterialDesign guideline I guess).
I forgot to say, I've already tried android:icon="#mipmap/ic_share" which that was working with my previous method/trick. But, check this Preview from AndroidStudio:
And here is after compiled:
Nothing changed!
So, my question: is that a bug or what am I doing wrong here?
Intent.createChooser didn't work also: from: https://stackoverflow.com/a/34797718/4409113
Edit:
The most interesting part, i just saw the same design and the same resutls from Google on the following course and on that app which they've called it SunShine app:
Applink
Course:
https://www.udacity.com//course/viewer#!/c-ud855/l-3961788738/m-4294896397
Okay, As ianhanniballake at this post said,
Icons in material design are 24dp x 24dp, as properly reflected by the
SearchView. However, ShareActionProvider has not yet been updated to
material design by default.
By the way, i've just posted a question/report to code.google.com which you can see it here:
http://code.google.com/p/android/issues/detail?id=200335
It seems that default icon is 24dp x 24dp but it should be like this:
But now, it is like this:
Seems to be a bug and still waiting for accepting or answering about it.I'll update this post if they answered.7 days passed!
UPDATE:
Finally, they've assigned the defect on to the development team and they will update this issue with more information as it becomes available.
Big thanks to ssingamc...#google.com at Google for support.
The answer is available here: http://code.google.com/p/android/issues/detail?id=200335#c10
I will update this answer if any updates or any fixes was available.
UPDATE: http://code.google.com/p/android/issues/detail?id=200335#c12
Hi, The development team has fixed the issue that you have reported
and it will be available in a future build. Thanks
Icons in material design are 24dp x 24dp, as properly reflected by the SearchView. However, ShareActionProvider has not yet been updated to material design by default.
You can set actionModeShareDrawable in your theme to set the share icon in the ShareActionProvider:
<item name="actionModeShareDrawable">#drawable/share_icon</item>
Note that ShareActionProvider is not found anywhere in the material design guidelines and with Android M's Direct Share capability (which requires you use a standard share intent at this time), it is unclear on whether the ShareActionProvider is a suggested pattern any more.
For more detail Visit Here.
AppCompat ShareActionProvider icon is too big compared to other icons
Change the icon Pragmatically
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_details, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.shareac);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Bundle extra = getIntent().getExtras();
String title = extra.getString("title");
Bundle extraurl = getIntent().getExtras();
String url = extraurl.getString("url");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Check this new project from something : " + title + url);
shareIntent.setType("text/plain");
mShareActionProvider.setShareIntent(shareIntent);
//Here
item.setIcon(getResources().getDrawable(R.drawable.ic_share));
return true;
}
or Theme
<item name="actionModeShareDrawable">#drawable/ic_share</item>
If you don't need custom icon for share menu item then try android resource for share menuitem as below:
<item android:id="#+id/action_share"
android:title="#string/share"
app:showAsAction="ifRoom"
android:icon="#android:drawable/ic_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
See here
Related
I am implementing "Read Aloud" or "Talkback" for an app. Everything is working with contentDescription text, but with option menu, I found nothing related to contentDescription, I want system read "Menu "+ item's name.
EX: My menu has 2 items: "Create New Folder" and "Delete current folder", currently, when I focus a menu item (Support trackball and bluetooth key), system can talk exactly the menu's text. But I want it talks more like "1: Menu Create New Folder" and "2: Menu Delete current folder".
So, How can I change the read text? How can I get the focused menu item when bluetooth keyboard press UP/DOWN key?
MenuItemCompat in the v4 support libraries has a
android.support.v4.view.MenuItemCompat.setContentDescription(MenuItem menuItem, CharSequence contentDescription)
method for backwards compatibility on pre-Oreo devices.
For AndroidX see this answer:
https://stackoverflow.com/a/57950952/1236327
As my investigation, in Android internal source code, class ActionMenuItemView.java method setTitle(CharSequence title), the source code also sets setContentDescription(title), so Android will read your MenuItem's text as default. I don't know why the core has so inflexible in this case.
Updated:
Thanks for #sofakingforever answer.
Seem Google just added the setContentDescription(CharSequence contentDescription) method to the MenuItem class on API 26 (Android O).
Updated:
Thanks for new #tim.paetz answer .
Look like all versions are now supported setContentDescription for menu item using android support v4 libraries.
this answer post AndroidX
androidx.core.view.MenuItemCompat.setContentDescription(menuItem, contentDescription)
It seems they just added the setContentDescription(CharSequence contentDescription) method to the MenuItem class on API 26 (Android O)
Full sample:
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.client_menu_close, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem closeMenu = menu.findItem(R.id.client_menu_close_action);
androidx.core.view.MenuItemCompat.setContentDescription(closeMenu, R.string.str_accessibility_client_screen_close);
}
I'm trying to implement the share functionality within my app. So far it works fine and I can share text to all other apps. The problem is the way it's shown.
I want something like just the share icon visible and then when user taps on it, it opens the OS dialog and lets user choose the app they want to share content to.
var share_article = menu.FindItem (Resource.Id.action_share);
var share_article_provider = (Android.Support.V7.Widget.ShareActionProvider) Android.Support.V4.View.MenuItemCompat.GetActionProvider (share_article);
share_article_provider.SetShareIntent (CreateIntent ());
and the xml:
<item
android:id="#+id/action_share"
myapp:showAsAction="ifRoom"
android:title="share"
myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
My app currently looks like this:
There's also a white border around it that I don't like.
Is there any way to change the icon??
How do I fix it??
You just want to turn off your share history.There is no official API to do this, but you can make your own ShareActionProvider. Actually there are two similar question on SO:
How do you turn off share history when using ShareActionProvider?
How to hide the share action (which use most) icon near the share action provider?
Wish these could help you.
As mentioned here when using support library this can be fixed really easily. This method won't turn off the share history but will hide the icons from actionbar.
I just needed to subclass the Android.Support.V7.Widget.ShareActionProvider like the following: (C# using Xamarin)
public class MyShareActionProvider : Android.Support.V7.Widget.ShareActionProvider
{
public SingleArticleShareActionProvider (Context context) : base (context)
{}
public override View OnCreateActionView ()
{
return null;
}
}
and then inside OnCreateOptionsMenu use the MyShareActionProvider like:
var share_article = menu.FindItem (Resource.Id.action_share);
var share = new SingleArticleShareActionProvider (globalContext);
Android.Support.V4.View.MenuItemCompat.SetActionProvider (share_article, share);
share_article.SetIcon (Resource.Drawable.abc_ic_menu_share_mtrl_alpha);
share.SetShareIntent (CreateIntent ());
You can use any icon you like with the method SetIcon.
I enabled the ShareActionProvider and I get this error:
java.lang.NoSuchMethodError: android.view.MenuItem.getActionProvider
But the way I use this class is like this:
// SHARING ONLY ENABLED in SDK 14 which is Ice Cream Sandwich
try
{
if ( android.os.Build.VERSION.SDK_INT >= 14 )
{
Button share = (Button)findViewById(R.id.share_button);
share.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
openOptionsMenu();
}
});
}
else
{
// HIDE THE TWO PAGE ELEMENTS
Button share = (Button)findViewById(R.id.share_button);
TextView share_prompt = (TextView)findViewById(R.id.share_prompt);
share.setVisibility(View.GONE);
share_prompt.setVisibility(View.GONE);
}
}
catch ( Exception e )
{
}
So I thought that I would not show the share button for earlier sdk's and I would be ok. But I am getting a lot of crashes.
I can not really test this because I don't have a phone with an earlier version of the SDK. But does it mean that these pages crash for everyone who has the earlier SDK version? Or just people who click share? How do I prevent this crashing?
And I have these methods in the class. Should I just not run them if sdk is less than 14?
#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,
"Some text");
return shareIntent;
}
// Somewhere in the application.
public void doShare(Intent shareIntent)
{
// When you want to share set the share intent.
myShareActionProvider.setShareIntent(shareIntent);
}
Thanks.
Alex
ShareActionProvider is available only for API 14+ which means in earlier versions you can't use it. If you want to add share button in your app and support old API levels I can suggest you to use ActionBarSherlock - a library which gives you the opportunity to use ActionBar in older versions of Android. Using this library you can do something like this to add share button :
MenuItem 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);
actionProvider.setShareIntent(createShareIntent());
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
}
Which will share an image file. If you don't want to support older API levels I would suggest you just check for API level and depending on that use ShareActionProvider.
Hope this help.
Yes, you need to have those same checks for your menu items. See the docs for getActionProvider. It's only supported on 14+. To test this kind of thing without a device, you can try on an emulator that uses an older version of Android.
In documentation you can see that MenuItem.getActionProvifer() from 14 API, of course you will catch java.lang.NoSuchMethodError on older versions (that errors you will catch every time when use methods or classes from new API).
Using try-catch can't save you:) You need to use different implementations with workarounds hanler-classes for different critical API versions.
If you will use ActionBarSherlock, you can use that method in Android API >= 8. Try to save functional for maximem API, it will be great!
A little late....
ShareActionProvider and in general ActionProviders are added from Android 4.0 (API Level 14) to Android Framework.
But the AndroidSupportLibrary helps us to support this feature in previous versions as well. But generally we do a mistake importing the ShareActionProvider from the Android SDK framework but not the SupportLibrary.
CODE:
-->In the main.xml:
<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="#string/menuitem_detail_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Note: Observe that the 'app' namespace is used and not 'android' namespace'
WHY? => This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.
-->In the Activity's onCreateOptionsMenu() :
ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
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, "The Text you want to share);
mShareActionProvider.setShareIntent(shareIntent);
Note: ShareActionProvider is imported from "import android.support.v7.widget.ShareActionProvider" and observe the usage of "MenuItemCompat.getActionProvider()" to get the ActionProvider instance.
You need to add condition to your when you support lower APIs while still using higher API calls. IT will fail in such case as there's simply no such method yet. And I'd suggest to slightly improve readability for your code, by replacing lines like:
if ( android.os.Build.VERSION.SDK_INT >= 14 )
with
if ( android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWITCH )
I am using the following code on a API 15 (Galaxy Nexus) avdevice
The menu_share.xml
<item
android:id="#+id/menu_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="Share"/>
<item
android:id="#+id/item1"
android:orderInCategory="20"
android:showAsAction="always|collapseActionView"
android:title="Refresh">
</item>
The main activity looks like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// Get the ActionProvider
provider = (ShareActionProvider) menu.findItem(R.id.menu_share)
.getActionProvider();
// Initialize the share intent
intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
intent.setType("text/plain");
provider.setShareIntent(intent);
return true;
}
When I run it on a API 15 (Galaxy Nexus) avd it shows me only the following icon that I cannot click (although it should show me at least the messageing app).
When I run the same code on a API 17 tablet avd I can start the Messaging app offered by the SharedActionProvider - it looks like this
On the tablet how can I make the Messageing app to show in the dropdown list instead like this and how to make the shared action provider on the API15 AVD clickable (first image)?
Thanks.
This is known Android bug. Check related bug report in ActionBarSherlock and android bug itself.
Bug is 1.5 years old and still not fixed.
This is a picture of the facebook app from the play store. I would like to copy the part that says "New Stories 10+" in my app. How do I make that? Is that part of the action bar?
I read through the action bar documentation here but couldn't find anything about it. I'm having trouble doing a google search for it too because I don't know what it's called.
Also, if you open the facebook app when you're in airplane a very similar type of message comes up that says "No Internet Connection". How is that message created?
Looking a little more deeply at the Facebook app, the bar just appears to be a LinearLayout containing two TextViews. This layout is then just embedded in the news feed fragment and hidden/shown as needed. In other words, it's not a part of the action bar; it's just a normal view within the fragment.
Check out this library by Cyril Mottier. it was designed to do exactly what you are asking for.
Yes it is part of the ActionBar but only if the device or emulator is on API 3.0 and higher. if the device or emulator has a lower API level, the actions will appear on the menu button.
This is just a dummy but it will give you a rough idea.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
int groupid = 1;
menu.add(groupid, 1, 1, "Search").setIcon(R.drawable.embassy_default);
// menu.add(groupid, , ,"Back").setIcon(R.drawable.hospital_default);
menu.add(groupid, 1, 1, "Exit").setIcon(R.drawable.government_default);
return true;
}
Read more here
http://developer.android.com/guide/topics/ui/menus.html