Android Studio ShareActionProvider - android

I added a ShareActionProvider menu item to my add, but when I press the "Share" button, the onOptionsItemSelected function doesn't capture the event, and I don't succeed to capture the changes in the text (the shareContent is initialized with the onCreate data through the onCreateOptionsMenu function).
I found in old posts that in older versions (support.v7), the "MenuItemCompat" needed to be used, but it's not recognized in android.widget.ShareActionProvider class.
This is part of my code:
menu.xml:
<item
android:id="#+id/action_share"
android:title="#string/action_title_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="always" />
mainActivity:
#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, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
setShareIntent(createShareIntent());
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
switch (item.getItemId() )
{
case R.id.action_share:
//share note content
noButtonWasPressed = false;
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
setShareIntent(createShareIntent());
return true;
Any suggestions?

Related

How to add CheckBox to Toolbar?

I've tried using the following code but no checkbox appear, only text :
<item
android:id="#+id/menuShowDue"
android:actionViewClass="android.widget.CheckBox"
android:title="#string/string_due"
android:checkable="true"
app:showAsAction="ifRoom" />
Is it not possible to add it via menu ? Should I do something else ?
I'm using android.support.v7.widget.Toolbar as Toolbar.
Please make changes as shown below to app:actionViewClass and app:showAsAction and it should work for you.
<item
android:id="#+id/menuShowDue"
android:checkable="true"
android:title="#string/string_due"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom|withText" />
Also make the relevant changes to onCreateOptionsMenu(). Sample text pasted below;
#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);
CheckBox checkBox = (CheckBox) menu.findItem(R.id.menuShowDue).getActionView();
checkBox.setText("Sample Text");
return true;
}
You can set the title at OnCreateOptionsMenu. Use MenuItem should be sufficient.
#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);
MenuItem checkBoxMenuItem = menu.findItem(R.id.menuShowDue);
checkBoxMenuItem.setTitle("Sample Text");
return true;
}
In OptionsItemSelected, check if checkbox selected or not:
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuShowDue:
if(item.isChecked())
{
item.setChecked(false);
}else{
item.setChecked(true);
}
break;
}

Share Button on ActionBar Appears Twice

I've created a share button on my Action Bar - but it seems to appear twice.
The menu XML file is below:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_share"
android:title="#string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>
</menu>
And it is instantiated in the onCreateOptionsMenu in the view.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_detail, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if(mShareActionProvider != null && !mForecastString.isEmpty()){
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else{
Log.d(LOG_TAG, "Share Action provider is null?");
}
super.onCreateOptionsMenu(menu,inflater);
}
How could the share button appear twice if it is defined, inflated, and instantiated only once?
You are inflating Menu twice, both in the Activity and the Fragment.
Removing one inflation should fix the problem.
Just before inflating menu options use menu.clear();
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_detail, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if(mShareActionProvider != null && !mForecastString.isEmpty()){
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else{
Log.d(LOG_TAG, "Share Action provider is null?");
}
super.onCreateOptionsMenu(menu,inflater);
}
This is happening due to repetitive inflation of menu.
use menu.clear() before inflation.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.my_menu_layout, menu);
}

Using Android action bar share intent

I'm using a menu item on the action bar and I want to share my app by clicking the share icon. When I click the share icon it doesn't work. Also, I want to add text saying "install this app" when shared.
Here is my code:
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
Mainpage.xml menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:icon="#drawable/ic_store"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
If you want a static share Intent (i.e., it never changes), then you update your onCreateOptionsMenu to be
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" +
getPackageName();
String yourShareText = "Install this app " + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
you can get the official tutorial here http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider
In the snappet you paste, you forget to call
mShareActionProvider.setShareIntent(intent);

android - Action bar sherlock add unnecessary button

i use action bar sherlock and i set custom layout in the menu:
<item
android:id="#+id/action_menu"
android:actionLayout="#layout/menu_item"
android:icon="#drawable/b_menu"
android:orderInCategory="1"
android:showAsAction="always"/>
<item
android:id="#+id/action_audio"
android:actionLayout="#layout/audio_item"
android:icon="#drawable/b_audio"
android:orderInCategory="2"
android:showAsAction="always"/>
<item
android:id="#+id/action_mySong_details"
android:actionLayout="#layout/my_song_properties"
android:icon="#drawable/b_mysong"
android:orderInCategory="2"
android:showAsAction="always"/>
to control those items i use:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Create an option menu from res/menu/items.xml */
getSherlock().getMenuInflater().inflate(R.menu.main_tab, menu);
/** Get the action view of the menu item whose id is search */
View v = (View) menu.findItem(R.id.action_menu).getActionView();
Button b = (Button) v.findViewById(R.id.btnMenu);
return super.onCreateOptionsMenu(menu);
}
for some reason the action bar add unnecessary item, does anybody knows how this case happene?
You are using Action bar sherlock. So use SupportMenuInflater.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_menu:
// Intent i=new Intent(A.this,B.class);
//startActivity(i);
return true;
}
return false;
}
I found the mistake: i remove the android:orderInCategory from all the items and it's ok now, i this case i don't need this option.

Two different MenuItems with different time response

I have two different Menu items (Flipping and Sharing) that share a single activity. Each one of them, works perfectly and smooth by its own, but when I put them together, the flipping action takes too long to respond. What can I do? Thanks for your help.
Flipping action:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Add either a "photo" or "finish" button to the action bar, depending on which page
// is currently selected.
MenuItem item = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE,
mShowingBack
? R.string.action_photo
: R.string.action_info);
item.setIcon(mShowingBack
? R.drawable.ic_action_photo
: R.drawable.ic_action_info);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Share Action
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onPrepareOptionsMenu(menu);
}
I left this question open, and I forgot about it. However, I took a look again and remember how I solve it. I was doing it all wrong. I had to call super.onPrepareOptionsMenu(menu) before everything like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
getMenuInflater().inflate(R.menu.share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return true;
}

Categories

Resources